-
Notifications
You must be signed in to change notification settings - Fork 0
/
coercions.test.ts
104 lines (87 loc) · 3.46 KB
/
coercions.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { describe, it, expect } from 'vitest'
import * as z from 'zod'
import { coerceValue } from './coercions'
const supportedShapes = [
z.boolean(),
z.number(),
z.date(),
z.string(),
z.enum(['one', 'two']),
]
describe('coerceValue', () => {
it('behaves like identity when shape is undefined', () => {
expect(coerceValue(null)).toEqual(null)
expect(coerceValue('some text')).toEqual('some text')
expect(coerceValue(new File([], 'empty-file.txt'))).toBeInstanceOf(File)
})
it.each(supportedShapes)(
'returns null when value is missing and shape %s is nullable',
(shape) => {
expect(coerceValue(null, shape.nullable())).toEqual(null)
},
)
it.each(supportedShapes)(
'returns null when value is missing and shape %s is nullable and optional',
(shape) => {
expect(coerceValue(null, shape.nullable().optional())).toEqual(null)
},
)
it.each(supportedShapes)(
'returns undefined when value is missing and shape %s is optional',
(shape) => {
expect(coerceValue(null, shape.optional())).toEqual(undefined)
},
)
it('returns NaN when trying to coerce things that do not make sense into numbers', () => {
expect(coerceValue('not a number', z.number())).toEqual(NaN)
expect(coerceValue(new File([], 'empty-file.txt'), z.number())).toEqual(NaN)
})
it('returns number when trying to coerce strings that can be read as numbers into numbers', () => {
expect(coerceValue('0', z.number())).toEqual(0)
expect(coerceValue('999999.999', z.number())).toEqual(999999.999)
})
it('coerces numbers to null when value is empty', () => {
expect(coerceValue('', z.number())).toEqual(null)
expect(coerceValue(null, z.number())).toEqual(null)
})
it('coerces booleans to true when value is not empty', () => {
expect(coerceValue('not a boolean', z.boolean())).toEqual(true)
expect(coerceValue('false', z.boolean())).toEqual(true)
expect(coerceValue('true', z.boolean())).toEqual(true)
})
it('coerces booleans to false when value is empty', () => {
expect(coerceValue('', z.boolean())).toEqual(false)
expect(coerceValue(null, z.boolean())).toEqual(false)
})
it('coerces dates to null when value is empty or is a file', () => {
expect(coerceValue('', z.date())).toEqual(null)
expect(coerceValue(null, z.date())).toEqual(null)
expect(
coerceValue(new File([], 'definitely-not-a-date.txt'), z.date()),
).toEqual(null)
})
it('coerces dates to Invalid Date when value cannot be read as date', () => {
expect(String(coerceValue('not a date', z.date()))).toEqual('Invalid Date')
})
it('coerces dates to a valid Date when value can be read as date', () => {
expect(coerceValue('2001-12-31', z.date())).toEqual(new Date(2001, 11, 31))
})
it('coerces strings to empty when value is empty', () => {
expect(coerceValue('', z.string())).toEqual('')
expect(coerceValue(null, z.string())).toEqual('')
})
it('coerces strings to [object Blob] when value is a file', () => {
expect(
coerceValue(new File([], 'some-empty-file.txt'), z.string()),
).toEqual('[object Blob]')
})
it('coerces enums to empty when value is empty', () => {
expect(coerceValue('', z.enum(['test']))).toEqual('')
expect(coerceValue(null, z.enum(['test']))).toEqual('')
})
it('coerces enums to [object Blob] when value is a file', () => {
expect(
coerceValue(new File([], 'some-empty-file.txt'), z.enum(['test'])),
).toEqual('[object Blob]')
})
})