diff --git a/paseri-lib/src/schemas/date.test.ts b/paseri-lib/src/schemas/date.test.ts index 89d82a0..be469cd 100644 --- a/paseri-lib/src/schemas/date.test.ts +++ b/paseri-lib/src/schemas/date.test.ts @@ -9,18 +9,15 @@ test('Valid type', () => { const schema = p.date(); fc.assert( - fc.property( - fc.integer().filter((value) => !Number.isNaN(new Date(value).getTime())), - (data) => { - const result = schema.safeParse(new Date(data)); - if (result.ok) { - expectTypeOf(result.value).toEqualTypeOf; - expect(result.value.getTime()).toBe(data); - } else { - expect(result.ok).toBeTruthy(); - } - }, - ), + fc.property(fc.date(), (data) => { + const result = schema.safeParse(data); + if (result.ok) { + expectTypeOf(result.value).toEqualTypeOf; + expect(result.value).toBe(data); + } else { + expect(result.ok).toBeTruthy(); + } + }), ); }); @@ -28,8 +25,7 @@ test('Invalid type', () => { const schema = p.date(); fc.assert( - // fast-check does not support `Date`s yet, so no need to filter. - fc.property(fc.anything(), (data) => { + fc.property(fc.anything({ withDate: false }), (data) => { const result = schema.safeParse(data); if (!result.ok) { expect(result.messages()).toEqual([{ path: [], message: 'Invalid type. Expected Date.' }]); @@ -55,20 +51,15 @@ test('Valid min', () => { const schema = p.date().min(new Date(2020, 0, 1)); fc.assert( - fc.property( - fc - .integer({ min: new Date(2020, 0, 1).getTime(), max: Number.MAX_SAFE_INTEGER }) - .filter((value) => !Number.isNaN(new Date(value).getTime())), - (data) => { - const result = schema.safeParse(new Date(data)); - if (result.ok) { - expectTypeOf(result.value).toEqualTypeOf; - expect(result.value.getTime()).toBe(data); - } else { - expect(result.ok).toBeTruthy(); - } - }, - ), + fc.property(fc.date({ min: new Date(2020, 0, 1) }), (data) => { + const result = schema.safeParse(data); + if (result.ok) { + expectTypeOf(result.value).toEqualTypeOf; + expect(result.value).toBe(data); + } else { + expect(result.ok).toBeTruthy(); + } + }), ); }); @@ -76,19 +67,14 @@ test('Invalid min', () => { const schema = p.date().min(new Date(2020, 0, 1)); fc.assert( - fc.property( - fc - .integer({ min: -Number.MAX_SAFE_INTEGER, max: new Date(2020, 0, 1).getTime() - 1 }) - .filter((value) => !Number.isNaN(new Date(value).getTime())), - (data) => { - const result = schema.safeParse(new Date(data)); - if (!result.ok) { - expect(result.messages()).toEqual([{ path: [], message: 'Too dated.' }]); - } else { - expect(result.ok).toBeFalsy(); - } - }, - ), + fc.property(fc.date({ max: new Date(2019, 11, 31) }), (data) => { + const result = schema.safeParse(data); + if (!result.ok) { + expect(result.messages()).toEqual([{ path: [], message: 'Too dated.' }]); + } else { + expect(result.ok).toBeFalsy(); + } + }), ); }); @@ -96,20 +82,15 @@ test('Valid max', () => { const schema = p.date().max(new Date(2020, 0, 1)); fc.assert( - fc.property( - fc - .integer({ min: -Number.MAX_SAFE_INTEGER, max: new Date(2020, 0, 1).getTime() }) - .filter((value) => !Number.isNaN(new Date(value).getTime())), - (data) => { - const result = schema.safeParse(new Date(data)); - if (result.ok) { - expectTypeOf(result.value).toEqualTypeOf; - expect(result.value.getTime()).toBe(data); - } else { - expect(result.ok).toBeTruthy(); - } - }, - ), + fc.property(fc.date({ max: new Date(2020, 0, 1) }), (data) => { + const result = schema.safeParse(data); + if (result.ok) { + expectTypeOf(result.value).toEqualTypeOf; + expect(result.value).toBe(data); + } else { + expect(result.ok).toBeTruthy(); + } + }), ); }); @@ -117,19 +98,14 @@ test('Invalid max', () => { const schema = p.date().max(new Date(2020, 0, 1)); fc.assert( - fc.property( - fc - .integer({ min: new Date(2020, 0, 1).getTime() + 1, max: Number.MAX_SAFE_INTEGER }) - .filter((value) => !Number.isNaN(new Date(value).getTime())), - (data) => { - const result = schema.safeParse(new Date(data)); - if (!result.ok) { - expect(result.messages()).toEqual([{ path: [], message: 'Too recent.' }]); - } else { - expect(result.ok).toBeFalsy(); - } - }, - ), + fc.property(fc.date({ min: new Date(2020, 0, 2) }), (data) => { + const result = schema.safeParse(data); + if (!result.ok) { + expect(result.messages()).toEqual([{ path: [], message: 'Too recent.' }]); + } else { + expect(result.ok).toBeFalsy(); + } + }), ); }); @@ -137,21 +113,15 @@ test('Optional', () => { const schema = p.date().optional(); fc.assert( - fc.property( - fc.option( - fc.integer().filter((value) => !Number.isNaN(new Date(value).getTime())), - { nil: undefined }, - ), - (data) => { - const result = schema.safeParse(data === undefined ? data : new Date(data)); - if (result.ok) { - expectTypeOf(result.value).toEqualTypeOf; - expect(result.value === undefined ? result.value : result.value.getTime()).toEqual(data); - } else { - expect(result.ok).toBeTruthy(); - } - }, - ), + fc.property(fc.option(fc.date(), { nil: undefined }), (data) => { + const result = schema.safeParse(data); + if (result.ok) { + expectTypeOf(result.value).toEqualTypeOf; + expect(result.value).toEqual(data); + } else { + expect(result.ok).toBeTruthy(); + } + }), ); }); @@ -159,21 +129,15 @@ test('Nullable', () => { const schema = p.date().nullable(); fc.assert( - fc.property( - fc.option( - fc.integer().filter((value) => !Number.isNaN(new Date(value).getTime())), - { nil: null }, - ), - (data) => { - const result = schema.safeParse(data === null ? data : new Date(data)); - if (result.ok) { - expectTypeOf(result.value).toEqualTypeOf; - expect(result.value === null ? result.value : result.value.getTime()).toEqual(data); - } else { - expect(result.ok).toBeTruthy(); - } - }, - ), + fc.property(fc.option(fc.date(), { nil: null }), (data) => { + const result = schema.safeParse(data); + if (result.ok) { + expectTypeOf(result.value).toEqualTypeOf; + expect(result.value).toEqual(data); + } else { + expect(result.ok).toBeTruthy(); + } + }), ); });