-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
subscribe.test.tsx
182 lines (134 loc) 路 4.09 KB
/
subscribe.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { proxy, ref, subscribe } from 'valtio'
import { subscribeKey } from 'valtio/utils'
const consoleWarn = console.warn
beforeEach(() => {
console.warn = jest.fn((message) => {
if (message === 'Please use proxy object') {
return
}
consoleWarn(message)
})
})
afterEach(() => {
console.warn = consoleWarn
})
describe('subscribe', () => {
it('should call subscription', async () => {
const obj = proxy({ count: 0 })
const handler = jest.fn()
subscribe(obj, handler)
obj.count += 1
await Promise.resolve()
expect(handler).toBeCalledTimes(1)
})
it('should be able to unsubscribe', async () => {
const obj = proxy({ count: 0 })
const handler = jest.fn()
const unsubscribe = subscribe(obj, handler)
unsubscribe()
obj.count += 1
await Promise.resolve()
expect(handler).toBeCalledTimes(0)
})
it('should call subscription of object property', async () => {
const obj = proxy({ nested: { count: 0 } })
const handler = jest.fn()
subscribe(obj.nested, handler)
obj.nested.count += 1
await Promise.resolve()
expect(handler).toBeCalledTimes(1)
})
it('should thow if subscribing to primitive property', async () => {
const obj = proxy({ count: 0 })
const handler = jest.fn()
expect(() => subscribe(obj.count as any, handler)).toThrow()
})
it('should not re-run subscription if no change', async () => {
const obj = proxy({ count: 0 })
const handler = jest.fn()
subscribe(obj, handler)
obj.count = 0
await Promise.resolve()
expect(handler).toBeCalledTimes(0)
})
it('should not cause infinite loop', async () => {
const obj = proxy({ count: 0 })
const handler = () => {
// Reset count if above 5
if (obj.count > 5) {
obj.count = 0
}
}
subscribe(obj, handler)
obj.count = 10
})
it('should batch updates', async () => {
const obj = proxy({ count1: 0, count2: 0 })
const handler = jest.fn()
subscribe(obj, handler)
obj.count1 += 1
obj.count2 += 1
await Promise.resolve()
expect(handler).toBeCalledTimes(1)
})
it('should not call subscription for objects wrapped in ref', async () => {
const obj = proxy({ nested: ref({ count: 0 }) })
const handler = jest.fn()
subscribe(obj, handler)
obj.nested.count += 1
await Promise.resolve()
expect(handler).toBeCalledTimes(0)
})
it('should notify ops', async () => {
const obj = proxy<{ count1: number; count2?: number }>({
count1: 0,
count2: 0,
})
const handler = jest.fn()
subscribe(obj, handler)
obj.count1 += 1
obj.count2 = 2
await Promise.resolve()
expect(handler).toBeCalledTimes(1)
expect(handler).lastCalledWith([
['set', ['count1'], 1, 0],
['set', ['count2'], 2, 0],
])
delete obj.count2
await Promise.resolve()
expect(handler).toBeCalledTimes(2)
expect(handler).lastCalledWith([['delete', ['count2'], 2]])
})
it('should notify nested ops', async () => {
const obj = proxy<{ nested: { count?: number } }>({ nested: { count: 0 } })
const handler = jest.fn()
subscribe(obj, handler)
obj.nested.count = 1
await Promise.resolve()
expect(handler).toBeCalledTimes(1)
expect(handler).lastCalledWith([['set', ['nested', 'count'], 1, 0]])
delete obj.nested.count
await Promise.resolve()
expect(handler).toBeCalledTimes(2)
expect(handler).lastCalledWith([['delete', ['nested', 'count'], 1]])
})
})
describe('subscribeKey', () => {
it('should call subscription', async () => {
const obj = proxy({ count1: 0, count2: 0 })
const handler1 = jest.fn()
const handler2 = jest.fn()
subscribeKey(obj, 'count1', handler1)
subscribeKey(obj, 'count2', handler2)
obj.count1 += 10
await Promise.resolve()
expect(handler1).toBeCalledTimes(1)
expect(handler1).lastCalledWith(10)
expect(handler2).toBeCalledTimes(0)
obj.count2 += 20
await Promise.resolve()
expect(handler1).toBeCalledTimes(1)
expect(handler2).toBeCalledTimes(1)
expect(handler2).lastCalledWith(20)
})
})