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

utils.test.ts

154 lines4.1 KB

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