Skip to content

Commit

Permalink
test: Simplify date tests using date arbitrary instead of integer
Browse files Browse the repository at this point in the history
  • Loading branch information
vbudovski committed Jan 1, 2025
1 parent 9644a17 commit 01c93f2
Showing 1 changed file with 62 additions and 98 deletions.
160 changes: 62 additions & 98 deletions paseri-lib/src/schemas/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,23 @@ 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<Date>;
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<Date>;
expect(result.value).toBe(data);
} else {
expect(result.ok).toBeTruthy();
}
}),
);
});

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.' }]);
Expand All @@ -55,125 +51,93 @@ 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<Date>;
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<Date>;
expect(result.value).toBe(data);
} else {
expect(result.ok).toBeTruthy();
}
}),
);
});

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();
}
}),
);
});

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<Date>;
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<Date>;
expect(result.value).toBe(data);
} else {
expect(result.ok).toBeTruthy();
}
}),
);
});

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();
}
}),
);
});

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<Date | undefined>;
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<Date | undefined>;
expect(result.value).toEqual(data);
} else {
expect(result.ok).toBeTruthy();
}
}),
);
});

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<Date | null>;
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<Date | null>;
expect(result.value).toEqual(data);
} else {
expect(result.ok).toBeTruthy();
}
}),
);
});

Expand Down

0 comments on commit 01c93f2

Please sign in to comment.