forked from vuejs/composition-api
/
ref.spec.ts
410 lines (355 loc) · 8.45 KB
/
ref.spec.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import {
ref,
customRef,
reactive,
isRef,
toRef,
toRefs,
Ref,
computed,
triggerRef,
watchEffect,
unref,
isReactive,
shallowRef,
proxyRefs,
ShallowUnwrapRef,
} from '../../../src'
import { RefKey } from '../../../src/utils/symbols'
describe('reactivity/ref', () => {
it('should hold a value', () => {
const a = ref(1)
expect(a.value).toBe(1)
a.value = 2
expect(a.value).toBe(2)
})
it('should be reactive', () => {
const a = ref(1)
let dummy
let calls = 0
watchEffect(
() => {
calls++
dummy = a.value
},
{ flush: 'sync' }
)
expect(calls).toBe(1)
expect(dummy).toBe(1)
a.value = 2
expect(calls).toBe(2)
expect(dummy).toBe(2)
// same value should not trigger
a.value = 2
expect(calls).toBe(2)
expect(dummy).toBe(2)
})
it('should make nested properties reactive', () => {
const a = ref({
count: 1,
})
let dummy
watchEffect(
() => {
dummy = a.value.count
},
{ flush: 'sync' }
)
expect(dummy).toBe(1)
a.value.count = 2
expect(dummy).toBe(2)
})
it('should work without initial value', () => {
const a = ref()
let dummy
watchEffect(
() => {
dummy = a.value
},
{ flush: 'sync' }
)
expect(dummy).toBe(undefined)
a.value = 2
expect(dummy).toBe(2)
})
it('should work like a normal property when nested in a reactive object', () => {
const a = ref(1)
const obj = reactive({
a,
b: {
c: a,
},
})
let dummy1: number
let dummy2: number
watchEffect(
() => {
dummy1 = obj.a
dummy2 = obj.b.c
},
{ flush: 'sync' }
)
const assertDummiesEqualTo = (val: number) =>
[dummy1, dummy2].forEach((dummy) => expect(dummy).toBe(val))
assertDummiesEqualTo(1)
a.value++
assertDummiesEqualTo(2)
obj.a++
assertDummiesEqualTo(3)
obj.b.c++
assertDummiesEqualTo(4)
})
it('should unwrap nested ref in types', () => {
const a = ref(0)
const b = ref(a)
expect(typeof (b.value + 1)).toBe('number')
})
it('should unwrap nested values in types', () => {
const a = {
b: ref(0),
}
const c = ref(a)
expect(typeof (c.value.b + 1)).toBe('number')
})
it('should NOT unwrap ref types nested inside arrays', () => {
const arr = ref([1, ref(1)]).value
;(arr[0] as number)++
;(arr[1] as Ref<number>).value++
const arr2 = ref([1, new Map<string, any>(), ref('1')]).value
const value = arr2[0]
if (isRef(value)) {
value + 'foo'
} else if (typeof value === 'number') {
value + 1
} else {
// should narrow down to Map type
// and not contain any Ref type
value.has('foo')
}
})
it('should keep tuple types', () => {
const tuple: [number, string, { a: number }, () => number, Ref<number>] = [
0,
'1',
{ a: 1 },
() => 0,
ref(0),
]
const tupleRef = ref(tuple)
tupleRef.value[0]++
expect(tupleRef.value[0]).toBe(1)
tupleRef.value[1] += '1'
expect(tupleRef.value[1]).toBe('11')
tupleRef.value[2].a++
expect(tupleRef.value[2].a).toBe(2)
expect(tupleRef.value[3]()).toBe(0)
tupleRef.value[4].value++
expect(tupleRef.value[4].value).toBe(1)
})
it('should keep symbols', () => {
const customSymbol = Symbol()
const obj = {
[Symbol.asyncIterator]: ref(1),
[Symbol.hasInstance]: { a: ref('a') },
[Symbol.isConcatSpreadable]: { b: ref(true) },
[Symbol.iterator]: [ref(1)],
[Symbol.match]: new Set<Ref<number>>(),
[Symbol.matchAll]: new Map<number, Ref<string>>(),
[Symbol.replace]: { arr: [ref('a')] },
[Symbol.search]: { set: new Set<Ref<number>>() },
[Symbol.species]: { map: new Map<number, Ref<string>>() },
[Symbol.split]: new WeakSet<Ref<boolean>>(),
[Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
[Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
[Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
[customSymbol]: { arr: [ref(1)] },
}
const objRef = ref(obj)
const keys: (keyof typeof obj)[] = [
Symbol.asyncIterator,
Symbol.hasInstance,
Symbol.isConcatSpreadable,
Symbol.iterator,
Symbol.match,
Symbol.matchAll,
Symbol.replace,
Symbol.search,
Symbol.species,
Symbol.split,
Symbol.toPrimitive,
Symbol.toStringTag,
Symbol.unscopables,
customSymbol,
]
keys.forEach((key) => {
expect(objRef.value[key]).toStrictEqual(obj[key])
})
})
test('unref', () => {
expect(unref(1)).toBe(1)
expect(unref(ref(1))).toBe(1)
})
test('shallowRef', () => {
const sref = shallowRef({ a: 1 })
expect(isReactive(sref.value)).toBe(false)
let dummy
watchEffect(
() => {
dummy = sref.value.a
},
{ flush: 'sync' }
)
expect(dummy).toBe(1)
sref.value = { a: 2 }
expect(isReactive(sref.value)).toBe(false)
expect(dummy).toBe(2)
sref.value.a = 3
expect(dummy).toBe(2)
})
test('shallowRef force trigger', () => {
const sref = shallowRef({ a: 1 })
let dummy
watchEffect(
() => {
dummy = sref.value.a
},
{ flush: 'sync' }
)
expect(dummy).toBe(1)
sref.value.a = 2
expect(dummy).toBe(1) // should not trigger yet
// force trigger
// sref.value = sref.value;
triggerRef(sref)
expect(dummy).toBe(2)
})
test('isRef', () => {
expect(isRef(ref(1))).toBe(true)
expect(isRef(computed(() => 1))).toBe(true)
expect(isRef(0)).toBe(false)
expect(isRef(1)).toBe(false)
// an object that looks like a ref isn't necessarily a ref
expect(isRef({ value: 0 })).toBe(false)
})
test('toRef', () => {
const a = reactive({
x: 1,
})
const x = toRef(a, 'x')
expect(isRef(x)).toBe(true)
expect(x.value).toBe(1)
// source -> proxy
a.x = 2
expect(x.value).toBe(2)
// proxy -> source
x.value = 3
expect(a.x).toBe(3)
// reactivity
let dummyX
watchEffect(
() => {
dummyX = x.value
},
{ flush: 'sync' }
)
expect(dummyX).toBe(x.value)
// mutating source should trigger watchEffect using the proxy refs
a.x = 4
expect(dummyX).toBe(4)
})
test('toRefs', () => {
const a = reactive({
x: 1,
y: 2,
})
const { x, y } = toRefs(a)
expect(isRef(x)).toBe(true)
expect(isRef(y)).toBe(true)
expect(x.value).toBe(1)
expect(y.value).toBe(2)
// source -> proxy
a.x = 2
a.y = 3
expect(x.value).toBe(2)
expect(y.value).toBe(3)
// proxy -> source
x.value = 3
y.value = 4
expect(a.x).toBe(3)
expect(a.y).toBe(4)
// reactivity
let dummyX, dummyY
watchEffect(
() => {
dummyX = x.value
dummyY = y.value
},
{ flush: 'sync' }
)
expect(dummyX).toBe(x.value)
expect(dummyY).toBe(y.value)
// mutating source should trigger watchEffect using the proxy refs
a.x = 4
a.y = 5
expect(dummyX).toBe(4)
expect(dummyY).toBe(5)
})
test('customRef', () => {
let value = 1
let _trigger: () => void
const custom = customRef((track, trigger) => ({
get() {
track()
return value
},
set(newValue: number) {
value = newValue
_trigger = trigger
},
}))
expect(isRef(custom)).toBe(true)
let dummy
watchEffect(
() => {
dummy = custom.value
},
{ flush: 'sync' }
)
expect(dummy).toBe(1)
custom.value = 2
// should not trigger yet
expect(dummy).toBe(1)
_trigger!()
expect(dummy).toBe(2)
})
test('proxyRefs', () => {
const a = {
x: ref(1),
obj: {
y: ref('foo'),
},
}
const reactiveA = {
[RefKey]: a,
...a,
}
const p = proxyRefs(a) as ShallowUnwrapRef<typeof reactiveA>
expect(p.x).toBe(1)
expect(p.obj.y).toBe('foo')
expect(p[RefKey]).toBe(a)
expect(Object.keys(p)).toStrictEqual(['x', 'obj'])
// @ts-expect-error
p.obj.y = 'bar'
p.x = 2
expect(a.x).toBe(2)
expect(a.obj.y).toBe('bar')
expect(p[RefKey]).toBe(a)
expect(Object.keys(p)).toStrictEqual(['x', 'obj'])
const r = reactive({ k: 'v' })
const s = proxyRefs(r)
expect(s.k).toBe('v')
expect(s[RefKey])
r.k = 'k'
expect(s.k).toBe('k')
})
})