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