-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathshallow.test.tsx
116 lines (91 loc) · 2.86 KB
/
shallow.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { describe, expect, it } from 'vitest'
import { shallow } from 'zustand/vanilla/shallow'
describe('shallow', () => {
it('compares primitive values', () => {
expect(shallow(true, true)).toBe(true)
expect(shallow(true, false)).toBe(false)
expect(shallow(1, 1)).toBe(true)
expect(shallow(1, 2)).toBe(false)
expect(shallow('zustand', 'zustand')).toBe(true)
expect(shallow('zustand', 'redux')).toBe(false)
})
it('compares objects', () => {
expect(shallow({ foo: 'bar', asd: 123 }, { foo: 'bar', asd: 123 })).toBe(
true,
)
expect(
shallow({ foo: 'bar', asd: 123 }, { foo: 'bar', foobar: true }),
).toBe(false)
expect(
shallow({ foo: 'bar', asd: 123 }, { foo: 'bar', asd: 123, foobar: true }),
).toBe(false)
})
it('compares arrays', () => {
expect(shallow([1, 2, 3], [1, 2, 3])).toBe(true)
expect(shallow([1, 2, 3], [2, 3, 4])).toBe(false)
expect(
shallow([{ foo: 'bar' }, { asd: 123 }], [{ foo: 'bar' }, { asd: 123 }]),
).toBe(false)
expect(shallow([{ foo: 'bar' }], [{ foo: 'bar', asd: 123 }])).toBe(false)
})
it('compares Maps', () => {
function createMap<T extends object>(obj: T) {
return new Map(Object.entries(obj))
}
expect(
shallow(
createMap({ foo: 'bar', asd: 123 }),
createMap({ foo: 'bar', asd: 123 }),
),
).toBe(true)
expect(
shallow(
createMap({ foo: 'bar', asd: 123 }),
createMap({ foo: 'bar', foobar: true }),
),
).toBe(false)
expect(
shallow(
createMap({ foo: 'bar', asd: 123 }),
createMap({ foo: 'bar', asd: 123, foobar: true }),
),
).toBe(false)
})
it('compares Sets', () => {
expect(shallow(new Set(['bar', 123]), new Set(['bar', 123]))).toBe(true)
expect(shallow(new Set(['bar', 123]), new Set(['bar', 2]))).toBe(false)
expect(shallow(new Set(['bar', 123]), new Set(['bar', 123, true]))).toBe(
false,
)
})
it('compares functions', () => {
function firstFnCompare() {
return { foo: 'bar' }
}
function secondFnCompare() {
return { foo: 'bar' }
}
expect(shallow(firstFnCompare, firstFnCompare)).toBe(true)
expect(shallow(secondFnCompare, secondFnCompare)).toBe(true)
expect(shallow(firstFnCompare, secondFnCompare)).toBe(false)
})
it('compares URLSearchParams', () => {
const a = new URLSearchParams({ hello: 'world' })
const b = new URLSearchParams({ zustand: 'shallow' })
expect(shallow(a, b)).toBe(false)
})
it('should work with nested arrays (#2794)', () => {
const arr = [1, 2]
expect(shallow([arr, 1], [arr, 1])).toBe(true)
})
})
describe('unsupported cases', () => {
it('date', () => {
expect(
shallow(
new Date('2022-07-19T00:00:00.000Z'),
new Date('2022-07-20T00:00:00.000Z'),
),
).not.toBe(false)
})
})