@formvk/reactive 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +186 -0
- package/dist/index.js +530 -0
- package/dist/index.js.map +1 -0
- package/package.json +21 -3
- package/src/__tests__/autorun.spec.ts +177 -0
- package/src/__tests__/batch.spec.ts +325 -0
- package/src/__tests__/computed.spec.ts +99 -0
- package/src/__tests__/decorator.spec.ts +439 -0
- package/src/__tests__/externals.spec.ts +146 -0
- package/src/__tests__/internals.spec.ts +42 -0
- package/src/__tests__/isReactive.spec.ts +38 -0
- package/src/__tests__/lazy.spec.ts +169 -0
- package/src/__tests__/observable.spec.ts +177 -0
- package/src/__tests__/observe.spec.ts +204 -0
- package/src/__tests__/reactive.spec.ts +201 -0
- package/src/__tests__/shallow.spec.ts +138 -0
- package/src/__tests__/shallowRef.spec.ts +129 -0
- package/src/__tests__/tracker.spec.ts +65 -0
- package/src/__tests__/untracked.spec.ts +56 -0
- package/src/action.ts +30 -0
- package/src/autorun.ts +57 -0
- package/src/batch.ts +120 -0
- package/src/computed.ts +42 -0
- package/src/decorator.ts +61 -0
- package/src/externals.ts +68 -0
- package/src/index.ts +25 -0
- package/src/internals.ts +177 -0
- package/src/observable.ts +49 -0
- package/src/observe.ts +25 -0
- package/src/reactive.ts +33 -0
- package/src/shallow.ts +33 -0
- package/src/shallowRef.ts +28 -0
- package/src/tracker.ts +39 -0
- package/src/types.ts +47 -0
- package/src/untracked.ts +10 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +4 -0
- package/vite.config.ts +39 -0
- package/README.md +0 -3
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { expect, test, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { action, autorun, batch, observable, observe, reactive, untracked } from '../index'
|
|
4
|
+
import type { IChange } from '../types'
|
|
5
|
+
|
|
6
|
+
test('observable 装饰 accessor 字段', () => {
|
|
7
|
+
class Model {
|
|
8
|
+
@observable accessor state = { count: 1 }
|
|
9
|
+
}
|
|
10
|
+
const model = new Model()
|
|
11
|
+
|
|
12
|
+
expect(model.state.count).toBe(1)
|
|
13
|
+
|
|
14
|
+
const events: IChange[] = []
|
|
15
|
+
observe(model.state, change => {
|
|
16
|
+
events.push(change)
|
|
17
|
+
})
|
|
18
|
+
model.state.count = 2
|
|
19
|
+
|
|
20
|
+
expect(events).toHaveLength(1)
|
|
21
|
+
expect(events[0]).toMatchObject({ type: 'set', key: 'count', value: 2, path: [] })
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('observable 装饰 accessor 字段重新赋值后仍可观察', () => {
|
|
25
|
+
class Model {
|
|
26
|
+
@observable accessor state = { count: 1 }
|
|
27
|
+
}
|
|
28
|
+
const model = new Model()
|
|
29
|
+
|
|
30
|
+
model.state = { count: 2 }
|
|
31
|
+
|
|
32
|
+
const events: IChange[] = []
|
|
33
|
+
observe(model.state, change => {
|
|
34
|
+
events.push(change)
|
|
35
|
+
})
|
|
36
|
+
model.state.count = 3
|
|
37
|
+
|
|
38
|
+
expect(events).toHaveLength(1)
|
|
39
|
+
expect(events[0]).toMatchObject({ type: 'set', key: 'count', value: 3, path: [] })
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('observable 装饰普通字段', () => {
|
|
43
|
+
class Model {
|
|
44
|
+
@observable field = { count: 1 }
|
|
45
|
+
}
|
|
46
|
+
const model = new Model()
|
|
47
|
+
|
|
48
|
+
const events: IChange[] = []
|
|
49
|
+
observe(model.field, change => {
|
|
50
|
+
events.push(change)
|
|
51
|
+
})
|
|
52
|
+
model.field.count = 2
|
|
53
|
+
|
|
54
|
+
expect(events).toHaveLength(1)
|
|
55
|
+
expect(events[0]).toMatchObject({ type: 'set', key: 'count', path: [] })
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('observable 装饰方法与取值器时原样透传,不改变行为', () => {
|
|
59
|
+
class Store {
|
|
60
|
+
@observable
|
|
61
|
+
greet() {
|
|
62
|
+
return 'hello'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@observable
|
|
66
|
+
get name() {
|
|
67
|
+
return 'formvk'
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const store = new Store()
|
|
72
|
+
expect(store.greet()).toBe('hello')
|
|
73
|
+
expect(store.name).toBe('formvk')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('observable 装饰赋值器时原样透传,不改变赋值行为', () => {
|
|
77
|
+
class Store {
|
|
78
|
+
private _name = ''
|
|
79
|
+
|
|
80
|
+
@observable
|
|
81
|
+
set name(value: string) {
|
|
82
|
+
this._name = value
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get name() {
|
|
86
|
+
return this._name
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const store = new Store()
|
|
91
|
+
store.name = 'formvk'
|
|
92
|
+
expect(store.name).toBe('formvk')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('batch 装饰取值器:访问期间合并通知,退出时统一刷新', () => {
|
|
96
|
+
const state = reactive({ a: 0, b: 0 })
|
|
97
|
+
const handler = vi.fn()
|
|
98
|
+
autorun(() => handler(state.a + state.b))
|
|
99
|
+
|
|
100
|
+
class Store {
|
|
101
|
+
@batch get snapshot() {
|
|
102
|
+
state.a = 1
|
|
103
|
+
state.b = 2
|
|
104
|
+
return state.a + state.b
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
expect(new Store().snapshot).toBe(3)
|
|
109
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
110
|
+
expect(handler).toHaveBeenLastCalledWith(3)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('batch 装饰赋值器:一次赋值内的多次写入只刷新一次', () => {
|
|
114
|
+
const state = reactive({ a: 0, b: 0 })
|
|
115
|
+
const handler = vi.fn()
|
|
116
|
+
autorun(() => handler(state.a + state.b))
|
|
117
|
+
|
|
118
|
+
class Store {
|
|
119
|
+
@batch set pair(value: number) {
|
|
120
|
+
state.a = value
|
|
121
|
+
state.b = value
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
new Store().pair = 5
|
|
126
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
127
|
+
expect(state.a).toBe(5)
|
|
128
|
+
expect(state.b).toBe(5)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('batch.scope 装饰方法与字段箭头函数时在外层批次结束前刷新', () => {
|
|
132
|
+
const state = reactive({ count: 0 })
|
|
133
|
+
const handler = vi.fn()
|
|
134
|
+
autorun(() => handler(state.count))
|
|
135
|
+
|
|
136
|
+
class Store {
|
|
137
|
+
@batch.scope
|
|
138
|
+
updateMethod() {
|
|
139
|
+
state.count = 2
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@batch.scope updateField = () => {
|
|
143
|
+
state.count = 3
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const store = new Store()
|
|
148
|
+
batch(() => {
|
|
149
|
+
store.updateMethod()
|
|
150
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
151
|
+
store.updateField()
|
|
152
|
+
expect(handler).toHaveBeenCalledTimes(3)
|
|
153
|
+
})
|
|
154
|
+
expect(handler).toHaveBeenCalledTimes(3)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('action 装饰字段箭头函数时合并通知', () => {
|
|
158
|
+
const state = reactive({ a: 0 })
|
|
159
|
+
const handler = vi.fn()
|
|
160
|
+
autorun(() => handler(state.a))
|
|
161
|
+
|
|
162
|
+
class Store {
|
|
163
|
+
@action update = () => {
|
|
164
|
+
state.a = 1
|
|
165
|
+
state.a = 2
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
new Store().update()
|
|
170
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
171
|
+
expect(handler).toHaveBeenLastCalledWith(2)
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
test('action 装饰取值器时内部读取不收集依赖', () => {
|
|
175
|
+
const state = reactive({ shown: 0, hidden: 0 })
|
|
176
|
+
const handler = vi.fn()
|
|
177
|
+
|
|
178
|
+
class Store {
|
|
179
|
+
@action get peek() {
|
|
180
|
+
return state.shown + state.hidden
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const store = new Store()
|
|
185
|
+
autorun(() => handler(store.peek))
|
|
186
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
187
|
+
|
|
188
|
+
state.hidden = 9
|
|
189
|
+
state.shown = 1
|
|
190
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
test('action 装饰赋值器时合并通知并阻止内部依赖收集', () => {
|
|
194
|
+
const state = reactive({ a: 0, b: 0, hidden: 0 })
|
|
195
|
+
const handler = vi.fn()
|
|
196
|
+
autorun(() => handler(state.a))
|
|
197
|
+
|
|
198
|
+
class Store {
|
|
199
|
+
@action set pair(value: number) {
|
|
200
|
+
state.a = value + state.hidden
|
|
201
|
+
state.b = value + state.hidden
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
new Store().pair = 5
|
|
206
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
207
|
+
state.hidden = 10
|
|
208
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test('action.scope 装饰方法时在外层批次结束前刷新且不收集依赖', () => {
|
|
212
|
+
const state = reactive({ count: 0, hidden: 0 })
|
|
213
|
+
const handler = vi.fn()
|
|
214
|
+
autorun(() => handler(state.count))
|
|
215
|
+
|
|
216
|
+
class Store {
|
|
217
|
+
@action.scope
|
|
218
|
+
update() {
|
|
219
|
+
state.count = state.hidden + 1
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const store = new Store()
|
|
224
|
+
batch(() => {
|
|
225
|
+
store.update()
|
|
226
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
227
|
+
})
|
|
228
|
+
state.hidden = 10
|
|
229
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
test('untracked 装饰方法时内部读取不建立依赖', () => {
|
|
233
|
+
const state = reactive({ tracked: 0, hidden: 0 })
|
|
234
|
+
const handler = vi.fn()
|
|
235
|
+
|
|
236
|
+
class Store {
|
|
237
|
+
@untracked readHidden() {
|
|
238
|
+
return state.hidden
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const store = new Store()
|
|
243
|
+
autorun(() => handler(state.tracked + store.readHidden()))
|
|
244
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
245
|
+
|
|
246
|
+
state.hidden = 10
|
|
247
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
248
|
+
|
|
249
|
+
state.tracked = 1
|
|
250
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test('untracked 装饰取值器时读取不建立依赖', () => {
|
|
254
|
+
const state = reactive({ hidden: 0 })
|
|
255
|
+
const handler = vi.fn()
|
|
256
|
+
|
|
257
|
+
class Store {
|
|
258
|
+
@untracked get hidden() {
|
|
259
|
+
return state.hidden
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const store = new Store()
|
|
264
|
+
autorun(() => handler(store.hidden))
|
|
265
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
266
|
+
|
|
267
|
+
state.hidden = 10
|
|
268
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
test('untracked 装饰赋值器时赋值过程中的读取不建立依赖', () => {
|
|
272
|
+
const state = reactive({ hidden: 0, snapshot: 0 })
|
|
273
|
+
const handler = vi.fn()
|
|
274
|
+
|
|
275
|
+
class Store {
|
|
276
|
+
@untracked set mark(value: number) {
|
|
277
|
+
state.snapshot = state.hidden + value
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const store = new Store()
|
|
282
|
+
autorun(() => {
|
|
283
|
+
handler()
|
|
284
|
+
store.mark = 1
|
|
285
|
+
})
|
|
286
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
287
|
+
|
|
288
|
+
state.hidden = 10
|
|
289
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
test('untracked 装饰字段:箭头函数被包装,非函数值原样透传', () => {
|
|
293
|
+
const state = reactive({ hidden: 0 })
|
|
294
|
+
const handler = vi.fn()
|
|
295
|
+
|
|
296
|
+
class Store {
|
|
297
|
+
@untracked read = () => state.hidden
|
|
298
|
+
|
|
299
|
+
@untracked label = 'plain-value'
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const store = new Store()
|
|
303
|
+
expect(store.label).toBe('plain-value')
|
|
304
|
+
|
|
305
|
+
autorun(() => handler(store.read()))
|
|
306
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
307
|
+
|
|
308
|
+
state.hidden = 10
|
|
309
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
// ---------- 值类型矩阵:基础类型与引用类型 ----------
|
|
313
|
+
|
|
314
|
+
test('@observable accessor 装饰基础类型:包盒子后整体赋值可响应', () => {
|
|
315
|
+
const sym = Symbol('sym')
|
|
316
|
+
|
|
317
|
+
class PrimitiveStore {
|
|
318
|
+
@observable accessor num = 1
|
|
319
|
+
@observable accessor str = 'a'
|
|
320
|
+
@observable accessor bool = false
|
|
321
|
+
@observable accessor nil: object | null = null
|
|
322
|
+
@observable accessor symValue: symbol = sym
|
|
323
|
+
@observable accessor big = 10n
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const store = new PrimitiveStore()
|
|
327
|
+
expect(store.num).toBe(1)
|
|
328
|
+
expect(store.str).toBe('a')
|
|
329
|
+
expect(store.bool).toBe(false)
|
|
330
|
+
expect(store.nil).toBe(null)
|
|
331
|
+
expect(store.symValue).toBe(sym)
|
|
332
|
+
expect(store.big).toBe(10n)
|
|
333
|
+
|
|
334
|
+
const handler = vi.fn()
|
|
335
|
+
autorun(() => handler(store.num, store.str))
|
|
336
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
337
|
+
|
|
338
|
+
store.num = 2
|
|
339
|
+
store.str = 'b'
|
|
340
|
+
expect(handler).toHaveBeenCalledTimes(3)
|
|
341
|
+
expect(store.num).toBe(2)
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
test('@observable 普通字段装饰基础类型:原样保留且赋值不具响应性', () => {
|
|
345
|
+
class RawFieldStore {
|
|
346
|
+
@observable num = 1
|
|
347
|
+
@observable str = 'hello'
|
|
348
|
+
@observable flag = true
|
|
349
|
+
@observable nothing = null
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const store = new RawFieldStore()
|
|
353
|
+
const handler = vi.fn()
|
|
354
|
+
autorun(() => handler(store.num))
|
|
355
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
356
|
+
|
|
357
|
+
// 普通字段装饰器只转换初始值,无法拦截后续赋值
|
|
358
|
+
store.num = 99
|
|
359
|
+
expect(store.num).toBe(99)
|
|
360
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
test('@observable accessor 装饰数组:深层变更可响应', () => {
|
|
364
|
+
class ArrayStore {
|
|
365
|
+
@observable accessor list = [1, 2]
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const store = new ArrayStore()
|
|
369
|
+
const lengthHandler = vi.fn()
|
|
370
|
+
const itemHandler = vi.fn()
|
|
371
|
+
autorun(() => lengthHandler(store.list.length))
|
|
372
|
+
autorun(() => itemHandler(store.list[0]))
|
|
373
|
+
expect(lengthHandler).toHaveBeenCalledTimes(1)
|
|
374
|
+
expect(itemHandler).toHaveBeenCalledTimes(1)
|
|
375
|
+
|
|
376
|
+
// push 改变 length,但不影响下标 0 的元素
|
|
377
|
+
store.list.push(3)
|
|
378
|
+
expect(lengthHandler).toHaveBeenCalledTimes(2)
|
|
379
|
+
expect(itemHandler).toHaveBeenCalledTimes(1)
|
|
380
|
+
|
|
381
|
+
store.list[0] = 10
|
|
382
|
+
expect(itemHandler).toHaveBeenCalledTimes(2)
|
|
383
|
+
expect(store.list).toEqual([10, 2, 3])
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
test('@observable 普通字段装饰数组:写入产生可观察变更', () => {
|
|
387
|
+
class FieldArrayStore {
|
|
388
|
+
@observable list = [1]
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const store = new FieldArrayStore()
|
|
392
|
+
const events: IChange[] = []
|
|
393
|
+
observe(store.list, change => events.push(change))
|
|
394
|
+
|
|
395
|
+
store.list.push(2)
|
|
396
|
+
|
|
397
|
+
const addEvent = events.find(event => event.key === '1')
|
|
398
|
+
expect(addEvent).toMatchObject({ type: 'add', key: '1', value: 2, path: [] })
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
test('@observable accessor 装饰 Map 与 Set:集合操作可响应', () => {
|
|
402
|
+
class CollectionStore {
|
|
403
|
+
@observable accessor map = new Map<string, number>()
|
|
404
|
+
@observable accessor set = new Set<number>()
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const store = new CollectionStore()
|
|
408
|
+
expect(store.map).toBeInstanceOf(Map)
|
|
409
|
+
expect(store.set).toBeInstanceOf(Set)
|
|
410
|
+
|
|
411
|
+
const mapHandler = vi.fn()
|
|
412
|
+
autorun(() => mapHandler(store.map.size))
|
|
413
|
+
store.map.set('a', 1)
|
|
414
|
+
expect(mapHandler).toHaveBeenCalledTimes(2)
|
|
415
|
+
expect(store.map.get('a')).toBe(1)
|
|
416
|
+
|
|
417
|
+
const setHandler = vi.fn()
|
|
418
|
+
autorun(() => setHandler(store.set.size))
|
|
419
|
+
store.set.add(1)
|
|
420
|
+
expect(setHandler).toHaveBeenCalledTimes(2)
|
|
421
|
+
expect(store.set.has(1)).toBe(true)
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
test('@observable accessor 装饰 Date 与函数:不可代理的引用类型原样透传', () => {
|
|
425
|
+
const now = new Date()
|
|
426
|
+
function fn() {
|
|
427
|
+
return 1
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
class SpecialStore {
|
|
431
|
+
@observable accessor date = now
|
|
432
|
+
@observable accessor fn = fn
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const store = new SpecialStore()
|
|
436
|
+
expect(store.date).toBe(now)
|
|
437
|
+
expect(store.fn).toBe(fn)
|
|
438
|
+
expect(store.fn()).toBe(1)
|
|
439
|
+
})
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { isReactive } from '@vue/reactivity'
|
|
2
|
+
import { expect, test } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { computed, contains, isObservable, observable, reactive, shallow, shallowRef, toJS } from '../index'
|
|
5
|
+
import { reverseCache } from '../internals'
|
|
6
|
+
|
|
7
|
+
test('isObservable 区分可观察对象与普通值', () => {
|
|
8
|
+
const state = observable({ count: 0 })
|
|
9
|
+
|
|
10
|
+
expect(isObservable(state)).toBe(true)
|
|
11
|
+
expect(isObservable({})).toBe(false)
|
|
12
|
+
expect(isObservable(1)).toBe(false)
|
|
13
|
+
expect(isObservable('x')).toBe(false)
|
|
14
|
+
expect(isObservable(null)).toBe(false)
|
|
15
|
+
expect(isObservable(undefined)).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('isObservable 不认可纯 Vue 响应式对象', () => {
|
|
19
|
+
const state = reactive({ nested: { a: 1 } })
|
|
20
|
+
|
|
21
|
+
expect(isObservable(state)).toBe(false)
|
|
22
|
+
expect(isObservable(state.nested)).toBe(false)
|
|
23
|
+
expect(isObservable(shallow({ a: 1 }))).toBe(false)
|
|
24
|
+
expect(isObservable(shallowRef(1))).toBe(false)
|
|
25
|
+
expect(isObservable(computed(() => 1))).toBe(false)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('toJS 把 reactive 深拷贝为普通对象并断开 Vue 代理', () => {
|
|
29
|
+
const state = reactive({ a: 1, nested: { b: 2 }, list: [1, { c: 3 }] })
|
|
30
|
+
|
|
31
|
+
const result: any = toJS(state)
|
|
32
|
+
|
|
33
|
+
expect(result).not.toBe(state)
|
|
34
|
+
expect(result).toEqual({ a: 1, nested: { b: 2 }, list: [1, { c: 3 }] })
|
|
35
|
+
expect(isReactive(result)).toBe(false)
|
|
36
|
+
expect(isReactive(result.nested)).toBe(false)
|
|
37
|
+
expect(isReactive(result.list)).toBe(false)
|
|
38
|
+
expect(isReactive(result.list[1])).toBe(false)
|
|
39
|
+
// 源对象仍保持响应式,转换不产生副作用
|
|
40
|
+
expect(isReactive(state.nested)).toBe(true)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('toJS 对多层嵌套与 reactive 数组逐层脱代理', () => {
|
|
44
|
+
const state = reactive({ l1: { l2: { l3: { value: 1 } } }, rows: [{ id: 1 }, { id: 2 }] })
|
|
45
|
+
|
|
46
|
+
const result: any = toJS(state)
|
|
47
|
+
|
|
48
|
+
expect(result.l1.l2.l3).toEqual({ value: 1 })
|
|
49
|
+
expect(isReactive(result.l1.l2.l3)).toBe(false)
|
|
50
|
+
expect(result.rows).toEqual([{ id: 1 }, { id: 2 }])
|
|
51
|
+
expect(isReactive(result.rows)).toBe(false)
|
|
52
|
+
expect(isReactive(result.rows[0])).toBe(false)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('toJS 处理 reactive 循环引用时不抛错并断开代理', () => {
|
|
56
|
+
const raw: any = { a: 1 }
|
|
57
|
+
raw.self = raw
|
|
58
|
+
const state = reactive(raw)
|
|
59
|
+
|
|
60
|
+
const result: any = toJS(state)
|
|
61
|
+
|
|
62
|
+
expect(result.a).toBe(1)
|
|
63
|
+
expect(result.self).toBe(raw)
|
|
64
|
+
expect(isReactive(result.self)).toBe(false)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('toJS 对非对象与普通对象原样返回', () => {
|
|
68
|
+
const plain = { a: 1 }
|
|
69
|
+
// 只在响应式根上脱代理,不深入普通对象内部
|
|
70
|
+
const holder = { inner: reactive({ b: 2 }) }
|
|
71
|
+
|
|
72
|
+
expect(toJS(1)).toBe(1)
|
|
73
|
+
expect(toJS(null)).toBe(null)
|
|
74
|
+
expect(toJS(plain)).toBe(plain)
|
|
75
|
+
expect(toJS(holder)).toBe(holder)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('toJS 把 observable 深拷贝为不可观察的普通对象', () => {
|
|
79
|
+
const state = observable({ a: 1, nested: { b: 2 }, list: [1, { c: 3 }] })
|
|
80
|
+
|
|
81
|
+
const result: any = toJS(state)
|
|
82
|
+
|
|
83
|
+
expect(result).toEqual({ a: 1, nested: { b: 2 }, list: [1, { c: 3 }] })
|
|
84
|
+
expect(isObservable(result)).toBe(false)
|
|
85
|
+
expect(isObservable(result.nested)).toBe(false)
|
|
86
|
+
expect(isObservable(result.list[1])).toBe(false)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('toJS 处理循环引用时不抛错并断开代理', () => {
|
|
90
|
+
const raw: any = { a: 1 }
|
|
91
|
+
raw.self = raw
|
|
92
|
+
const state = observable(raw)
|
|
93
|
+
|
|
94
|
+
const result: any = toJS(state)
|
|
95
|
+
|
|
96
|
+
expect(result.a).toBe(1)
|
|
97
|
+
expect(result.self).toBe(raw)
|
|
98
|
+
expect(isObservable(result.self)).toBe(false)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('contains 判断属性是否为目标自身或后代节点', () => {
|
|
102
|
+
const state = observable({ nested: { deep: { value: 1 } } })
|
|
103
|
+
const nested = state.nested
|
|
104
|
+
const deep = nested.deep
|
|
105
|
+
|
|
106
|
+
expect(contains(state, state)).toBe(true)
|
|
107
|
+
expect(contains(state, nested)).toBe(true)
|
|
108
|
+
expect(contains(state, deep)).toBe(true)
|
|
109
|
+
expect(contains(nested, deep)).toBe(true)
|
|
110
|
+
expect(contains(nested, state)).toBe(false)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('contains 对非对象入参与无关节点返回 false', () => {
|
|
114
|
+
const state = observable({ a: 1 })
|
|
115
|
+
|
|
116
|
+
expect(contains(1, 1)).toBe(true)
|
|
117
|
+
expect(contains(state, 1)).toBe(false)
|
|
118
|
+
expect(contains(1, state)).toBe(false)
|
|
119
|
+
|
|
120
|
+
const other = observable({ b: 2 })
|
|
121
|
+
expect(contains(state, other)).toBe(false)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('contains 对内层 Vue 代理与外层代理做归一化匹配', () => {
|
|
125
|
+
const state = observable({ nested: { value: 1 } })
|
|
126
|
+
const nested = state.nested
|
|
127
|
+
const innerProxy = reverseCache.get(nested)
|
|
128
|
+
|
|
129
|
+
expect(contains(innerProxy, nested)).toBe(true)
|
|
130
|
+
expect(contains(nested, innerProxy)).toBe(true)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('contains 在菱形父子关系的 BFS 中跳过已访问节点', () => {
|
|
134
|
+
const shared = { mark: 1 }
|
|
135
|
+
const middle: any = { a: {}, b: {} }
|
|
136
|
+
middle.a.child = shared
|
|
137
|
+
middle.b.child = shared
|
|
138
|
+
const state = observable({ deep: middle })
|
|
139
|
+
|
|
140
|
+
// 触发两条路径上的懒代理与父子登记,shared 拥有两个兄弟父节点
|
|
141
|
+
void state.deep.a.child
|
|
142
|
+
void state.deep.b.child
|
|
143
|
+
|
|
144
|
+
expect(contains(state, state.deep.a.child)).toBe(true)
|
|
145
|
+
expect(contains(state, state.deep.b.child)).toBe(true)
|
|
146
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { reactive, ref, shallowReactive } from '@vue/reactivity'
|
|
2
|
+
import { expect, test, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { canProxy, registerSkip, wrap } from '../internals'
|
|
5
|
+
import { observable } from '../observable'
|
|
6
|
+
import { observe } from '../observe'
|
|
7
|
+
|
|
8
|
+
test('canProxy 拒绝 ref、内置构造器实例与非对象', () => {
|
|
9
|
+
expect(canProxy(ref(1))).toBe(false)
|
|
10
|
+
expect(canProxy(new Date())).toBe(false)
|
|
11
|
+
expect(canProxy(new Map())).toBe(false)
|
|
12
|
+
expect(canProxy(1)).toBe(false)
|
|
13
|
+
expect(canProxy(null)).toBe(false)
|
|
14
|
+
expect(canProxy({})).toBe(true)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test('canProxy 命中自定义 skip 判定时拒绝,wrap 原样返回不代理', () => {
|
|
18
|
+
const dispose = registerSkip(value => (value as any).__custom === true)
|
|
19
|
+
const special = reactive({ __custom: true })
|
|
20
|
+
|
|
21
|
+
expect(canProxy(special)).toBe(false)
|
|
22
|
+
expect(wrap(special)).toBe(special)
|
|
23
|
+
|
|
24
|
+
dispose()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('wrap 的 get 对 ref 只解包不代理', () => {
|
|
28
|
+
const count = ref(1)
|
|
29
|
+
const proxy = wrap(shallowReactive({ count }))
|
|
30
|
+
|
|
31
|
+
expect(proxy.count).toBe(1)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('删除不存在的 key 不触发变更通知', () => {
|
|
35
|
+
const state = observable({ a: 1 }) as any
|
|
36
|
+
const handler = vi.fn()
|
|
37
|
+
observe(state, handler)
|
|
38
|
+
|
|
39
|
+
delete state.nonexistent
|
|
40
|
+
|
|
41
|
+
expect(handler).not.toHaveBeenCalled()
|
|
42
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { toRaw } from '@vue/reactivity'
|
|
2
|
+
import { expect, test } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { computed, isReactive, reactive, shallow, shallowRef } from '../index'
|
|
5
|
+
|
|
6
|
+
test('isReactive 识别 reactive 对象与懒访问产生的嵌套代理', () => {
|
|
7
|
+
const state = reactive({ nested: { a: 1 }, list: [{ id: 1 }] })
|
|
8
|
+
|
|
9
|
+
expect(isReactive(state)).toBe(true)
|
|
10
|
+
expect(isReactive(state.nested)).toBe(true)
|
|
11
|
+
expect(isReactive(state.list)).toBe(true)
|
|
12
|
+
expect(isReactive(state.list[0])).toBe(true)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
test('isReactive 识别 shallowReactive 根对象', () => {
|
|
16
|
+
expect(isReactive(shallow({ a: 1 }))).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
test('isReactive 对普通对象与原始值返回 false', () => {
|
|
20
|
+
expect(isReactive({})).toBe(false)
|
|
21
|
+
expect(isReactive([])).toBe(false)
|
|
22
|
+
expect(isReactive(1)).toBe(false)
|
|
23
|
+
expect(isReactive('x')).toBe(false)
|
|
24
|
+
expect(isReactive(true)).toBe(false)
|
|
25
|
+
expect(isReactive(null)).toBe(false)
|
|
26
|
+
expect(isReactive(undefined)).toBe(false)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('isReactive 不认可 ref / shallowRef / computed 盒子', () => {
|
|
30
|
+
expect(isReactive(shallowRef(1))).toBe(false)
|
|
31
|
+
expect(isReactive(computed(() => 1))).toBe(false)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('isReactive 对 toRaw 还原后的原始对象返回 false', () => {
|
|
35
|
+
const state = reactive({ a: 1 })
|
|
36
|
+
|
|
37
|
+
expect(isReactive(toRaw(state))).toBe(false)
|
|
38
|
+
})
|