UNPKG

type-fns

Version:

A set of types, type checks, and type guards for simpler, safer, and easier to read code.

78 lines 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const test_fns_1 = require("test-fns"); const isPresent_1 = require("./isPresent"); describe('isPresent', () => { (0, test_fns_1.given)('direct calls should assess by default', () => { (0, test_fns_1.when)('called directly', () => { (0, test_fns_1.then)('it should return true if is some number, for example', () => { const input = 21; expect((0, isPresent_1.isPresent)(input)).toEqual(true); if (!(0, isPresent_1.isPresent)(input)) { const shouldBeNever = input; } }); (0, test_fns_1.then)('it should return false if is null', () => { const input = null; expect((0, isPresent_1.isPresent)(input)).toEqual(false); if ((0, isPresent_1.isPresent)(input)) { const shouldBeNever = input; } }); (0, test_fns_1.then)('it should return false if is undefined', () => { const input = undefined; expect((0, isPresent_1.isPresent)(input)).toEqual(false); if ((0, isPresent_1.isPresent)(input)) { const shouldBeNever = input; } }); (0, test_fns_1.then)('it should return true if is falsy', () => { expect((0, isPresent_1.isPresent)(false)).toEqual(true); expect((0, isPresent_1.isPresent)(0)).toEqual(true); expect((0, isPresent_1.isPresent)('')).toEqual(true); }); (0, test_fns_1.then)('it should be useful in having correct types after filtering', () => { const stringsOrNulls = ['hello', '', null, 'world', undefined]; // type = `(string | null | undefined)[]` const strings = stringsOrNulls.filter(isPresent_1.isPresent); // type = string[] // check that it did what we expected in runtime expect(strings).toEqual(['hello', '', 'world']); // check that it successfully informed typescript of this too strings.map((string) => string.toUpperCase()); // This should not throw a type error, because the filter should have told typescript it took out the nulls }); }); }); (0, test_fns_1.given)('it has an assess', () => { (0, test_fns_1.when)('.assess is called', () => { (0, test_fns_1.then)('it should assess correctly', () => { expect(isPresent_1.isPresent.assess(21)).toEqual(true); expect(isPresent_1.isPresent.assess(null)).toEqual(false); expect(isPresent_1.isPresent.assess(undefined)).toEqual(false); }); }); }); (0, test_fns_1.given)('it has an assure', () => { (0, test_fns_1.when)('.assure is called', () => { (0, test_fns_1.then)('it should assure correctly, for a number, for example', () => { const input = 21; const shouldWork = isPresent_1.isPresent.assure(input); // @ts-expect-error // input could be null or undefined const shouldFail = input; }); (0, test_fns_1.then)('it should assure correctly, for null', () => { const input = null; const error = (0, test_fns_1.getError)(() => { const shouldWork = isPresent_1.isPresent.assure(input); }); expect(error.message).toContain(`assure.rejection: input does not satisfy type.check 'isPresent'`); }); (0, test_fns_1.then)('it should assure correctly, for undefined', () => { const input = undefined; const error = (0, test_fns_1.getError)(() => { const shouldWork = isPresent_1.isPresent.assure(input); }); expect(error.message).toContain(`assure.rejection: input does not satisfy type.check 'isPresent'`); }); }); }); }); //# sourceMappingURL=isPresent.test.js.map