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

utils.test.ts

155 lines4.2 KB

1import { describe, expect, test } from 'vitest'
2import { reactive, ref } from 'vue-demi'
3import { cloneDeep, cloneDeepUnref, updateState } from '../utils'
4​
5describe('utils', () => {
6 describe('updateState', () => {
7 test('should update first object with values from the second one', () => {
8 const origin = { option1: 'a', option2: 'b', option3: 'c' }
9 const update = { option1: 'x', option2: 'y', option3: 'z' }
10 const expected = { option1: 'x', option2: 'y', option3: 'z' }
11​
12 updateState(origin, update)
13 expect(origin).toEqual(expected)
14 })
15​
16 test('should update only existing keys', () => {
17 const origin = { option1: 'a', option2: 'b' }
18 const update = { option1: 'x', option2: 'y', option3: 'z' }
19 const expected = { option1: 'x', option2: 'y' }
20​
21 updateState(origin, update)
22 expect(origin).toEqual(expected)
23 })
24​
25 test('should remove non existing keys', () => {
26 const origin = { option1: 'a', option2: 'b', option3: 'c' }
27 const update = { option1: 'x', option2: 'y' }
28 const expected = { option1: 'x', option2: 'y' }
29​
30 updateState(origin, update)
31 expect(origin).toEqual(expected)
32 })
33 })
34​
35 describe('cloneDeep', () => {
36 test('should copy primitives and functions AS-IS', () => {
37 expect(cloneDeep(3456)).toBe(3456)
38 expect(cloneDeep('theString')).toBe('theString')
39 expect(cloneDeep(null)).toBe(null)
40 })
41​
42 test('should copy Maps and Sets AS-IS', () => {
43 const setVal = new Set([3, 4, 5])
44 const setValCopy = cloneDeep(setVal)
45 expect(setValCopy).toBe(setVal)
46 expect(setValCopy).toStrictEqual(new Set([3, 4, 5]))
47​
48 const mapVal = new Map([
49 ['a', 'aVal'],
50 ['b', 'bVal'],
51 ])
52 const mapValCopy = cloneDeep(mapVal)
53 expect(mapValCopy).toBe(mapVal)
54 expect(mapValCopy).toStrictEqual(
55 new Map([
56 ['a', 'aVal'],
57 ['b', 'bVal'],
58 ]),
59 )
60 })
61​
62 test('should deeply copy arrays', () => {
63 const val = [
64 25,
65 'str',
66 null,
67 new Set([3, 4]),
68 [5, 6, { a: 1 }],
69 undefined,
70 ]
71 const cp = cloneDeep(val)
72 expect(cp).toStrictEqual([
73 25,
74 'str',
75 null,
76 new Set([3, 4]),
77 [5, 6, { a: 1 }],
78 undefined,
79 ])
80 expect(cp).not.toBe(val)
81 expect(cp[3]).toBe(val[3]) // Set([3, 4])
82 expect(cp[4]).not.toBe(val[4]) // [5, 6, { a: 1 }]
83 expect((cp[4] as Array<number>)[2]).not.toBe((val[4] as Array<number>)[2]) // { a : 1 }
84 })
85​
86 test('should deeply copy object', () => {
87 const val = reactive({
88 a: 25,
89 b: 'str',
90 c: null,
91 d: undefined,
92 e: new Set([5, 6]),
93 f: [3, 4],
94 g: { fa: 26 },
95 })
96 const cp = cloneDeep(val)
97​
98 expect(cp).toStrictEqual({
99 a: 25,
100 b: 'str',
101 c: null,
102 d: undefined,
103 e: new Set([5, 6]),
104 f: [3, 4],
105 g: { fa: 26 },
106 })
107​
108 expect(cp.e).toBe(val.e) // Set
109 expect(cp.f).not.toBe(val.f) // []
110 expect(cp.g).not.toBe(val.g) // {}
111 })
112 })
113​
114 describe('cloneDeepUnref', () => {
115 test('should unref primitives', () => {
116 expect(cloneDeepUnref(ref(34))).toBe(34)
117 expect(cloneDeepUnref(ref('myStr'))).toBe('myStr')
118 })
119​
120 test('should deeply unref arrays', () => {
121 const val = ref([2, 3, ref(4), ref('5'), { a: ref(6) }, [ref(7)]])
122 const cp = cloneDeepUnref(val)
123 expect(cp).toStrictEqual([2, 3, 4, '5', { a: 6 }, [7]])
124 })
125​
126 test('should deeply unref objects', () => {
127 const val = ref({
128 a: 1,
129 b: ref(2),
130 c: [ref('c1'), ref(['c2'])],
131 d: {
132 e: ref('e'),
133 },
134 })
135 const cp = cloneDeepUnref(val)
136​
137 expect(cp).toEqual({
138 a: 1,
139 b: 2,
140 c: ['c1', ['c2']],
141 d: { e: 'e' },
142 })
143 })
144​
145 test('should clone getters returning values in queryKey', () => {
146 const val = ref({ queryKey: [1, 2, () => '3'] })
147 const cp = cloneDeepUnref(val)
148 expect(cp).toStrictEqual({ queryKey: [1, 2, '3'] })
149 })
150​
151 test('should unref undefined', () => {
152 expect(cloneDeepUnref(ref(undefined))).toBe(undefined)
153 })
154 })
155})