@sakuli/commons
Version:
50 lines • 2.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var maybe_1 = require("./maybe");
describe('Maybe', function () {
describe('isPresent', function () {
it('should recognize null to be false', function () { return expect(maybe_1.isPresent(null)).toBeFalsy(); });
it('should recognize undefined to be false', function () { return expect(maybe_1.isPresent(undefined)).toBeFalsy(); });
it('should recognize NaN to be false', function () { return expect(maybe_1.isPresent(NaN)).toBeFalsy(); });
it('should recognize 0 to be true', function () { return expect(maybe_1.isPresent(0)).toBeTruthy(); });
it('should recognize empty string to be true', function () { return expect(maybe_1.isPresent('')).toBeTruthy(); });
it('should recognize empty object to be true', function () { return expect(maybe_1.isPresent({})).toBeTruthy(); });
it('should recognize empty array to be true', function () { return expect(maybe_1.isPresent([])).toBeTruthy(); });
});
describe('ifPresent', function () {
it('should invoke then on present value', function () {
var thenMock = jest.fn();
var otherwiseMock = jest.fn();
maybe_1.ifPresent('test', thenMock, otherwiseMock);
expect(thenMock).toHaveBeenCalledWith('test');
expect(otherwiseMock).toHaveBeenCalledTimes(0);
});
it('should ivoke thne on present value with no other parameter', function () {
var thenMock = jest.fn();
expect(maybe_1.ifPresent('test', thenMock)).toBeUndefined();
expect(thenMock).toBeCalledWith('test');
});
it('should invoke otherwise on absent value', function () {
var thenMock = jest.fn();
var otherwiseMock = jest.fn();
maybe_1.ifPresent(null, thenMock, otherwiseMock);
expect(thenMock).toHaveBeenCalledTimes(0);
expect(otherwiseMock).toHaveBeenCalledTimes(1);
});
it('should return result of otherwise producer', function () {
expect(maybe_1.ifPresent(null, function (v) { return 'Present'; }, function () { return 'Absent'; })).toEqual('Absent');
});
it('should return result of then producer', function () {
expect(maybe_1.ifPresent(2, function (v) { return 2 * 2; }, function () { return 0; })).toEqual(4);
});
});
describe('ensure', function () {
it('should return fallback on absent value', function () {
expect(maybe_1.ensure(null, 'test')).toEqual('test');
});
it('should return fallback on absent value', function () {
expect(maybe_1.ensure("present", 'test')).toEqual('present');
});
});
});
//# sourceMappingURL=maybe.spec.js.map