Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2β
3import {
4 callUpdater,
5 concatenateFieldNames,
6 evaluate,
7 getBy,
8 isNil,
9 isNotNil,
10 isPromiseLike,
11 normalizeToArray,
12 resolveFieldUpdateOptions,
13 setBy,
14} from '../src/utils.lib'
15β
16describe('utils', () => {
17 it('resolves field update defaults from the triggering event', () => {
18 expect(resolveFieldUpdateOptions(undefined, 'change')).toMatchObject({
19 causeValidation: true,
20 markAsBlurred: false,
21 markAsDirty: true,
22 markAsTouched: true,
23 _skipFieldCreation: false,
24 fieldApiOverride: null,
25 doPropagate: true,
26 })
27β
28 expect(resolveFieldUpdateOptions(undefined, 'blur')).toMatchObject({
29 markAsBlurred: true,
30 markAsDirty: false,
31 markAsTouched: false,
32 })
33β
34 expect(resolveFieldUpdateOptions(undefined, 'submit')).toMatchObject({
35 markAsBlurred: false,
36 markAsDirty: false,
37 markAsTouched: true,
38 })
39β
40 expect(resolveFieldUpdateOptions(undefined, 'noEvent')).toMatchObject({
41 markAsBlurred: false,
42 markAsDirty: false,
43 markAsTouched: false,
44 _skipFieldCreation: true,
45 })
46 })
47β
48 it('honors explicit field update options', () => {
49 const fieldApiOverride = {} as never
50β
51 expect(
52 resolveFieldUpdateOptions(
53 {
54 causeValidation: false,
55 markAsBlurred: true,
56 markAsDirty: false,
57 markAsTouched: false,
58 _skipFieldCreation: false,
59 fieldApiOverride,
60 doPropagate: false,
61 },
62 'change',
63 ),
64 ).toEqual({
65 causeValidation: false,
66 markAsBlurred: true,
67 markAsDirty: false,
68 markAsTouched: false,
69 _skipFieldCreation: false,
70 fieldApiOverride,
71 doPropagate: false,
72 })
73 })
74β
75 it('reads nested values through object and array paths', () => {
76 const source = { user: { names: ['Ada'] } }
77β
78 expect(getBy(source, 'user.names[0]')).toBe('Ada')
79 expect(getBy(source, 'user.missing.name')).toBeUndefined()
80 expect(getBy({ user: null }, 'user')).toBeNull()
81 expect(getBy({ user: null }, 'user.name')).toBeUndefined()
82 })
83β
84 it('sets nested object values immutably', () => {
85 const source = { user: null as null | { name: string } }
86β
87 expect(setBy(source, 'user.name', 'Ada')).toEqual({
88 user: { name: 'Ada' },
89 })
90 expect(source.user).toBeNull()
91 })
92β
93 it('sets array values and preserves sparse indexes', () => {
94 expect(setBy({}, 'items[2].name', 'Ada')).toEqual({
95 items: { 2: { name: 'Ada' } },
96 })
97β
98 expect(setBy({ items: [] }, 'items[2].name', 'Ada')).toEqual({
99 items: [undefined, undefined, { name: 'Ada' }],
100 })
101β
102 expect(setBy(['first'], '[2]', 'third')).toEqual(['first', 'third'])
103 expect(setBy([], '[2]', 'third')).toEqual([undefined, undefined, 'third'])
104 expect(setBy(undefined, [undefined], 'third')).toEqual([undefined, 'third'])
105 })
106β
107 it('calls updater functions with the current value', () => {
108 expect(callUpdater((value: number) => value + 1, 1)).toBe(2)
109 expect(setBy({ count: 1 }, 'count', (value: number) => value + 1)).toEqual({
110 count: 2,
111 })
112 })
113β
114 it('normalizes optional single-or-many values to arrays', () => {
115 expect(normalizeToArray(null)).toEqual([])
116 expect(normalizeToArray(undefined)).toEqual([])
117 expect(normalizeToArray('one')).toEqual(['one'])
118 expect(normalizeToArray(['one', 'two'])).toEqual(['one', 'two'])
119 })
120β
121 it('compares supported value shapes deeply', () => {
122 expect(evaluate(NaN, NaN)).toBe(true)
123 expect(evaluate(1, '1' as never)).toBe(false)
124 expect(evaluate(new Date(1), new Date(1))).toBe(true)
125 expect(evaluate(new Date(1), new Date(2))).toBe(false)
126 expect(
127 evaluate(
128 new File(['a'], 'a.txt', { lastModified: 1 }),
129 new File(['a'], 'a.txt', { lastModified: 1 }),
130 ),
131 ).toBe(true)
132 expect(
133 evaluate(
134 new File(['a'], 'a.txt', { lastModified: 1 }),
135 new File(['b'], 'b.txt', { lastModified: 1 }),
136 ),
137 ).toBe(false)
138 expect(
139 evaluate(
140 new File(['a'], 'a.txt', { lastModified: 1 }),
141 new File(['a'], 'a.txt', { lastModified: 2 }),
142 ),
143 ).toBe(false)
144 expect(evaluate(new Map([['a', 1]]), new Map([['a', 1]]))).toBe(true)
145 expect(evaluate(new Map([['a', 1]]), new Map([['a', 2]]))).toBe(false)
146 expect(evaluate(new Map([['a', 1]]), new Map())).toBe(false)
147 expect(evaluate(new Set(['a']), new Set(['a']))).toBe(true)
148 expect(evaluate(new Set(['a']), new Set(['b']))).toBe(false)
149 expect(evaluate(new Set(['a']), new Set())).toBe(false)
150 expect(evaluate({ a: { b: 1 } }, { a: { b: 1 } })).toBe(true)
151 expect(evaluate({ a: { b: 1 } }, { a: { b: 2 } })).toBe(false)
152 expect(evaluate({ a: 1 }, { a: 1, b: 2 })).toBe(false)
153 })
154β
155 it('concatenates field names with dot and bracket notation', () => {
156 expect(concatenateFieldNames('', 'name')).toBe('name')
157 expect(concatenateFieldNames('user', '')).toBe('user')
158 expect(concatenateFieldNames('items', '[0]')).toBe('items[0]')
159 expect(concatenateFieldNames('user', 'name')).toBe('user.name')
160 })
161β
162 it('checks nil and promise-like values', () => {
163 expect(isNotNil('value')).toBe(true)
164 expect(isNotNil(null)).toBe(false)
165 expect(isNil(undefined)).toBe(true)
166 expect(isNil('value')).toBe(false)
167 expect(isPromiseLike(Promise.resolve())).toBe(true)
168 expect(isPromiseLike({ then: () => undefined })).toBe(true)
169 expect(isPromiseLike({ then: true })).toBe(false)
170 expect(isPromiseLike(null)).toBe(false)
171 })
172})