@formvk/reactive 0.1.0 → 1.0.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/CHANGELOG.md +7 -0
- package/README.md +94 -0
- package/package.json +6 -2
- package/src/__tests__/autorun.spec.ts +0 -177
- package/src/__tests__/batch.spec.ts +0 -325
- package/src/__tests__/computed.spec.ts +0 -99
- package/src/__tests__/decorator.spec.ts +0 -439
- package/src/__tests__/externals.spec.ts +0 -146
- package/src/__tests__/internals.spec.ts +0 -42
- package/src/__tests__/isReactive.spec.ts +0 -38
- package/src/__tests__/lazy.spec.ts +0 -169
- package/src/__tests__/observable.spec.ts +0 -177
- package/src/__tests__/observe.spec.ts +0 -204
- package/src/__tests__/reactive.spec.ts +0 -201
- package/src/__tests__/shallow.spec.ts +0 -138
- package/src/__tests__/shallowRef.spec.ts +0 -129
- package/src/__tests__/tracker.spec.ts +0 -65
- package/src/__tests__/untracked.spec.ts +0 -56
- package/src/action.ts +0 -30
- package/src/autorun.ts +0 -57
- package/src/batch.ts +0 -120
- package/src/computed.ts +0 -42
- package/src/decorator.ts +0 -61
- package/src/externals.ts +0 -68
- package/src/index.ts +0 -25
- package/src/internals.ts +0 -177
- package/src/observable.ts +0 -49
- package/src/observe.ts +0 -25
- package/src/reactive.ts +0 -33
- package/src/shallow.ts +0 -33
- package/src/shallowRef.ts +0 -28
- package/src/tracker.ts +0 -39
- package/src/types.ts +0 -47
- package/src/untracked.ts +0 -10
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -4
- package/vite.config.ts +0 -39
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { expect, test } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { computed } from '../computed'
|
|
4
|
-
import { reactive } from '../reactive'
|
|
5
|
-
|
|
6
|
-
const ERROR_MESSAGE = '[formvk] computed 只支持 getter 装饰器'
|
|
7
|
-
|
|
8
|
-
test('computed() 追踪依赖', () => {
|
|
9
|
-
const state = reactive({ count: 1 })
|
|
10
|
-
const double = computed(() => state.count * 2)
|
|
11
|
-
|
|
12
|
-
expect(double.value).toBe(2)
|
|
13
|
-
|
|
14
|
-
state.count = 2
|
|
15
|
-
expect(double.value).toBe(4)
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
test('computed() 支持可写', () => {
|
|
19
|
-
const state = reactive({ count: 1 })
|
|
20
|
-
const double = computed({
|
|
21
|
-
get: () => state.count * 2,
|
|
22
|
-
set: (value: number) => {
|
|
23
|
-
state.count = value / 2
|
|
24
|
-
},
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
double.value = 10
|
|
28
|
-
|
|
29
|
-
expect(state.count).toBe(5)
|
|
30
|
-
expect(double.value).toBe(10)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test('@computed 装饰 getter', () => {
|
|
34
|
-
class Model {
|
|
35
|
-
@reactive accessor count = 1
|
|
36
|
-
|
|
37
|
-
@computed get double() {
|
|
38
|
-
return this.count * 2
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
const model = new Model()
|
|
42
|
-
|
|
43
|
-
expect(model.double).toBe(2)
|
|
44
|
-
|
|
45
|
-
model.count = 2
|
|
46
|
-
expect(model.double).toBe(4)
|
|
47
|
-
|
|
48
|
-
model.count = 3
|
|
49
|
-
expect(model.double).toBe(6)
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('@computed 按实例缓存', () => {
|
|
53
|
-
class Model {
|
|
54
|
-
@reactive accessor count = 1
|
|
55
|
-
|
|
56
|
-
@computed get double() {
|
|
57
|
-
return this.count * 2
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const first = new Model()
|
|
61
|
-
const second = new Model()
|
|
62
|
-
|
|
63
|
-
first.count = 5
|
|
64
|
-
|
|
65
|
-
expect(first.double).toBe(10)
|
|
66
|
-
expect(second.double).toBe(2)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('computed 只支持 getter 装饰器', () => {
|
|
70
|
-
expect(() => {
|
|
71
|
-
;(computed as any)(() => 1, { kind: 'accessor' })
|
|
72
|
-
}).toThrowError(ERROR_MESSAGE)
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test('@computed 以装饰器语法修饰方法/赋值器/字段时抛错', () => {
|
|
76
|
-
// computed 的类型签名只允许 getter,非法 kind 在运行时抛错;转 any 后用装饰器语法验证
|
|
77
|
-
const computedDecorator = computed as any
|
|
78
|
-
|
|
79
|
-
// 装饰器在类定义期即抛错,用匿名类表达式箭头函数承载以便 expect 捕获
|
|
80
|
-
function defineBadMethod() {
|
|
81
|
-
return class {
|
|
82
|
-
@computedDecorator method() {}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
function defineBadSetter() {
|
|
86
|
-
return class {
|
|
87
|
-
@computedDecorator set value(v: number) {}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
function defineBadField() {
|
|
91
|
-
return class {
|
|
92
|
-
@computedDecorator field = 1
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
expect(defineBadMethod).toThrowError(ERROR_MESSAGE)
|
|
97
|
-
expect(defineBadSetter).toThrowError(ERROR_MESSAGE)
|
|
98
|
-
expect(defineBadField).toThrowError(ERROR_MESSAGE)
|
|
99
|
-
})
|
|
@@ -1,439 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,146 +0,0 @@
|
|
|
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
|
-
})
|