import { t, typeAsserts } from '../../index'; describe('literal', () => { it('valid', () => { expect.hasAssertions(); expect(t.literal(1).validate(1)).toBe(true); expect(() => { typeAsserts(t.literal(1), 1); }).not.toThrow(); expect(t.literal('str').validate('str')).toBe(true); expect(() => { typeAsserts(t.literal('str'), 'str'); }).not.toThrow(); expect(t.literal(true).validate(true)).toBe(true); expect(() => { typeAsserts(t.literal(true), true); }).not.toThrow(); expect(t.literal(false).validate(false)).toBe(true); expect(() => { typeAsserts(t.literal(false), false); }).not.toThrow(); expect(t.literal(undefined).validate(undefined)).toBe(true); expect(() => { typeAsserts(t.literal(undefined), undefined); }).not.toThrow(); expect(t.literal(null).validate(null)).toBe(true); expect(() => { typeAsserts(t.literal(null), null); }).not.toThrow(); }); it('invalid', () => { expect.hasAssertions(); expect(t.literal(1).validate(123)).toBe(false); expect(() => { typeAsserts(t.literal(1), 123); }).toThrow('param should be 1, but got 123'); expect(t.literal('str').validate('string')).toBe(false); expect(() => { typeAsserts(t.literal('str'), 'string'); }).toThrow('param should be "str", but got "string"'); expect(t.literal(true).validate(false)).toBe(false); expect(() => { typeAsserts(t.literal(true), false); }).toThrow('param should be true, but got false'); expect(t.literal(false).validate(true)).toBe(false); expect(() => { typeAsserts(t.literal(false), true); }).toThrow('param should be false, but got true'); expect(t.literal(undefined).validate(null)).toBe(false); expect(() => { typeAsserts(t.literal(undefined), null); }).toThrow('param should be undefined, but got null'); expect(t.literal(null).validate(undefined)).toBe(false); expect(() => { typeAsserts(t.literal(null), undefined); }).toThrow('param should be null, but got undefined'); }); it('optional valid', () => { expect.hasAssertions(); expect(t.literal(1).optional().validate(undefined)).toBe(true); expect(() => { typeAsserts(t.literal(1).optional(), undefined); }).not.toThrow(); expect(t.literal(1).optional().validate(undefined)).toBe(true); expect(() => { typeAsserts(t.literal(1).optional(), undefined); }).not.toThrow(); expect(t.literal('str').optional().validate(undefined)).toBe(true); expect(() => { typeAsserts(t.literal('str').optional(), undefined); }).not.toThrow(); expect(t.literal(true).optional().validate(undefined)).toBe(true); expect(() => { typeAsserts(t.literal(true).optional(), undefined); }).not.toThrow(); expect(t.literal(false).optional().validate(undefined)).toBe(true); expect(() => { typeAsserts(t.literal(false).optional(), undefined); }).not.toThrow(); expect(t.literal(undefined).optional().validate(undefined)).toBe(true); expect(() => { typeAsserts(t.literal(undefined).optional(), undefined); }).not.toThrow(); }); it('optional invalid', () => { expect.hasAssertions(); expect(t.literal(1).optional().validate(123)).toBe(false); expect(() => { typeAsserts(t.literal(1).optional(), 123); }).toThrow('param should be 1 or undefined, but got 123'); expect(t.literal('str').optional().validate(123)).toBe(false); expect(() => { typeAsserts(t.literal('str').optional(), 123); }).toThrow('param should be "str" or undefined, but got 123'); expect(t.literal(true).optional().validate(false)).toBe(false); expect(() => { typeAsserts(t.literal(true).optional(), false); }).toThrow('param should be true or undefined, but got false'); expect(t.literal(false).optional().validate(true)).toBe(false); expect(() => { typeAsserts(t.literal(false).optional(), true); }).toThrow('param should be false or undefined, but got true'); expect(t.literal(undefined).optional().validate(123)).toBe(false); expect(() => { typeAsserts(t.literal(undefined).optional(), 123); }).toThrow('param should be undefined, but got 123'); }); });