Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

utils.spec.ts

131 lines3.9 KB

1import { describe, expect, it } from 'vitest'
2import { deleteBy, getBy, makePathArray, setBy } from '../src/index'
3​
4describe('getBy', () => {
5 const structure = {
6 name: 'Marc',
7 kids: [
8 { name: 'Stephen', age: 10, hobbies: ['soccer', 'reading'] },
9 { name: 'Taylor', age: 15, hobbies: ['swimming', 'gaming'] },
10 ],
11 mother: {
12 name: 'Lisa',
13 },
14 }
15​
16 it('should get subfields by path', () => {
17 expect(getBy(structure, 'name')).toBe(structure.name)
18 expect(getBy(structure, 'mother.name')).toBe(structure.mother.name)
19 })
20​
21 it('should get array subfields by path', () => {
22 expect(getBy(structure, 'kids[0].name')).toBe(structure.kids[0]!.name)
23 expect(getBy(structure, 'kids[0].age')).toBe(structure.kids[0]!.age)
24 })
25​
26 it('should get nested array subfields by path', () => {
27 expect(getBy(structure, 'kids[0].hobbies[0]')).toBe(
28 structure.kids[0]!.hobbies[0],
29 )
30 expect(getBy(structure, 'kids[0].hobbies[1]')).toBe(
31 structure.kids[0]!.hobbies[1],
32 )
33 })
34})
35​
36describe('setBy', () => {
37 const structure = {
38 name: 'Marc',
39 kids: [
40 { name: 'Stephen', age: 10, hobbies: ['soccer', 'reading'] },
41 { name: 'Taylor', age: 15, hobbies: ['swimming', 'gaming'] },
42 ],
43 mother: {
44 name: 'Lisa',
45 },
46 }
47​
48 it('should set subfields by path', () => {
49 expect(setBy(structure, 'name', 'Lisa').name).toBe('Lisa')
50 expect(setBy(structure, 'mother.name', 'Tina').mother.name).toBe('Tina')
51 })
52​
53 it('should set array subfields by path', () => {
54 expect(setBy(structure, 'kids[0].name', 'Taylor').kids[0].name).toBe(
55 'Taylor',
56 )
57 expect(setBy(structure, 'kids[0].age', 20).kids[0].age).toBe(20)
58 })
59​
60 it('should set nested array subfields by path', () => {
61 expect(
62 setBy(structure, 'kids[0].hobbies[0]', 'swimming').kids[0].hobbies[0],
63 ).toBe('swimming')
64 expect(
65 setBy(structure, 'kids[0].hobbies[1]', 'gaming').kids[0].hobbies[1],
66 ).toBe('gaming')
67 })
68})
69​
70describe('deleteBy', () => {
71 const structure = {
72 name: 'Marc',
73 kids: [
74 { name: 'Stephen', age: 10, hobbies: ['soccer', 'reading'] },
75 { name: 'Taylor', age: 15, hobbies: ['swimming', 'gaming'] },
76 ],
77 mother: {
78 name: 'Lisa',
79 },
80 }
81​
82 it('should delete subfields by path', () => {
83 expect(deleteBy(structure, 'name').name).not.toBeDefined()
84 expect(deleteBy(structure, 'mother.name').mother.name).not.toBeDefined()
85 })
86​
87 it('should delete array subfields by path', () => {
88 expect(deleteBy(structure, 'kids[0].name').kids[0].name).not.toBeDefined()
89 expect(deleteBy(structure, 'kids[0].age').kids[0].age).not.toBeDefined()
90 })
91​
92 it('should delete nested array subfields by path', () => {
93 expect(deleteBy(structure, 'kids[0].hobbies[0]').kids[0].hobbies[0]).toBe(
94 'reading',
95 )
96 expect(
97 deleteBy(structure, 'kids[0].hobbies[1]').kids[0].hobbies[1],
98 ).not.toBeDefined()
99 })
100​
101 it('should delete non-existent paths like a noop', () => {
102 expect(deleteBy(structure, 'nonexistent')).toEqual(structure)
103 expect(deleteBy(structure, 'nonexistent.nonexistent')).toEqual(structure)
104 expect(deleteBy(structure, 'kids[3].name')).toEqual(structure)
105 expect(deleteBy(structure, 'nonexistent[3].nonexistent')).toEqual(structure)
106 })
107})
108​
109describe('makePathArray', () => {
110 it('should convert dot notation to array', () => {
111 expect(makePathArray('name')).toEqual(['name'])
112 expect(makePathArray('mother.name')).toEqual(['mother', 'name'])
113 expect(makePathArray('kids[0].name')).toEqual(['kids', 0, 'name'])
114 expect(makePathArray('kids[0].name[1]')).toEqual(['kids', 0, 'name', 1])
115 expect(makePathArray('kids[0].name[1].age')).toEqual([
116 'kids',
117 0,
118 'name',
119 1,
120 'age',
121 ])
122 expect(makePathArray('kids[0].name[1].age[2]')).toEqual([
123 'kids',
124 0,
125 'name',
126 1,
127 'age',
128 2,
129 ])
130 })
131})