-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
readonly.spec.ts
522 lines (471 loc) · 15.4 KB
/
readonly.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import {
computed,
effect,
isProxy,
isReactive,
isReadonly,
markRaw,
reactive,
readonly,
ref,
toRaw,
} from '../src'
/**
* @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html
*/
type Writable<T> = { -readonly [P in keyof T]: T[P] }
describe('reactivity/readonly', () => {
describe('Object', () => {
it('should make nested values readonly', () => {
const original = { foo: 1, bar: { baz: 2 } }
const wrapped = readonly(original)
expect(wrapped).not.toBe(original)
expect(isProxy(wrapped)).toBe(true)
expect(isReactive(wrapped)).toBe(false)
expect(isReadonly(wrapped)).toBe(true)
expect(isReactive(original)).toBe(false)
expect(isReadonly(original)).toBe(false)
expect(isReactive(wrapped.bar)).toBe(false)
expect(isReadonly(wrapped.bar)).toBe(true)
expect(isReactive(original.bar)).toBe(false)
expect(isReadonly(original.bar)).toBe(false)
// get
expect(wrapped.foo).toBe(1)
// has
expect('foo' in wrapped).toBe(true)
// ownKeys
expect(Object.keys(wrapped)).toEqual(['foo', 'bar'])
})
it('should not allow mutation', () => {
const qux = Symbol('qux')
const original = {
foo: 1,
bar: {
baz: 2,
},
[qux]: 3,
}
const wrapped: Writable<typeof original> = readonly(original)
wrapped.foo = 2
expect(wrapped.foo).toBe(1)
expect(
`Set operation on key "foo" failed: target is readonly.`,
).toHaveBeenWarnedLast()
wrapped.bar.baz = 3
expect(wrapped.bar.baz).toBe(2)
expect(
`Set operation on key "baz" failed: target is readonly.`,
).toHaveBeenWarnedLast()
wrapped[qux] = 4
expect(wrapped[qux]).toBe(3)
expect(
`Set operation on key "Symbol(qux)" failed: target is readonly.`,
).toHaveBeenWarnedLast()
// @ts-expect-error
delete wrapped.foo
expect(wrapped.foo).toBe(1)
expect(
`Delete operation on key "foo" failed: target is readonly.`,
).toHaveBeenWarnedLast()
// @ts-expect-error
delete wrapped.bar.baz
expect(wrapped.bar.baz).toBe(2)
expect(
`Delete operation on key "baz" failed: target is readonly.`,
).toHaveBeenWarnedLast()
// @ts-expect-error
delete wrapped[qux]
expect(wrapped[qux]).toBe(3)
expect(
`Delete operation on key "Symbol(qux)" failed: target is readonly.`,
).toHaveBeenWarnedLast()
})
it('should not trigger effects', () => {
const wrapped: any = readonly({ a: 1 })
let dummy
effect(() => {
dummy = wrapped.a
})
expect(dummy).toBe(1)
wrapped.a = 2
expect(wrapped.a).toBe(1)
expect(dummy).toBe(1)
expect(`target is readonly`).toHaveBeenWarned()
})
})
describe('Array', () => {
it('should make nested values readonly', () => {
const original = [{ foo: 1 }]
const wrapped = readonly(original)
expect(wrapped).not.toBe(original)
expect(isProxy(wrapped)).toBe(true)
expect(isReactive(wrapped)).toBe(false)
expect(isReadonly(wrapped)).toBe(true)
expect(isReactive(original)).toBe(false)
expect(isReadonly(original)).toBe(false)
expect(isReactive(wrapped[0])).toBe(false)
expect(isReadonly(wrapped[0])).toBe(true)
expect(isReactive(original[0])).toBe(false)
expect(isReadonly(original[0])).toBe(false)
// get
expect(wrapped[0].foo).toBe(1)
// has
expect(0 in wrapped).toBe(true)
// ownKeys
expect(Object.keys(wrapped)).toEqual(['0'])
})
it('should not allow mutation', () => {
const wrapped: any = readonly([{ foo: 1 }])
wrapped[0] = 1
expect(wrapped[0]).not.toBe(1)
expect(
`Set operation on key "0" failed: target is readonly.`,
).toHaveBeenWarned()
wrapped[0].foo = 2
expect(wrapped[0].foo).toBe(1)
expect(
`Set operation on key "foo" failed: target is readonly.`,
).toHaveBeenWarned()
// should block length mutation
wrapped.length = 0
expect(wrapped.length).toBe(1)
expect(wrapped[0].foo).toBe(1)
expect(
`Set operation on key "length" failed: target is readonly.`,
).toHaveBeenWarned()
// mutation methods invoke set/length internally and thus are blocked as well
wrapped.push(2)
expect(wrapped.length).toBe(1)
// push triggers two warnings on [1] and .length
expect(`target is readonly.`).toHaveBeenWarnedTimes(5)
})
it('should not trigger effects', () => {
const wrapped: any = readonly([{ a: 1 }])
let dummy
effect(() => {
dummy = wrapped[0].a
})
expect(dummy).toBe(1)
wrapped[0].a = 2
expect(wrapped[0].a).toBe(1)
expect(dummy).toBe(1)
expect(`target is readonly`).toHaveBeenWarnedTimes(1)
wrapped[0] = { a: 2 }
expect(wrapped[0].a).toBe(1)
expect(dummy).toBe(1)
expect(`target is readonly`).toHaveBeenWarnedTimes(2)
})
})
const maps = [Map, WeakMap]
maps.forEach((Collection: any) => {
describe(Collection.name, () => {
test('should make nested values readonly', () => {
const key1 = {}
const key2 = {}
const original = new Collection([
[key1, {}],
[key2, {}],
])
const wrapped = readonly(original)
expect(wrapped).not.toBe(original)
expect(isProxy(wrapped)).toBe(true)
expect(isReactive(wrapped)).toBe(false)
expect(isReadonly(wrapped)).toBe(true)
expect(isReactive(original)).toBe(false)
expect(isReadonly(original)).toBe(false)
expect(isReactive(wrapped.get(key1))).toBe(false)
expect(isReadonly(wrapped.get(key1))).toBe(true)
expect(isReactive(original.get(key1))).toBe(false)
expect(isReadonly(original.get(key1))).toBe(false)
})
test('should not allow mutation & not trigger effect', () => {
const map = readonly(new Collection())
const key = {}
let dummy
effect(() => {
dummy = map.get(key)
})
expect(dummy).toBeUndefined()
map.set(key, 1)
expect(dummy).toBeUndefined()
expect(map.has(key)).toBe(false)
expect(
`Set operation on key "${key}" failed: target is readonly.`,
).toHaveBeenWarned()
})
// #1772
test('readonly + reactive should make get() value also readonly + reactive', () => {
const map = reactive(new Collection())
const roMap = readonly(map)
const key = {}
map.set(key, {})
const item = map.get(key)
expect(isReactive(item)).toBe(true)
expect(isReadonly(item)).toBe(false)
const roItem = roMap.get(key)
expect(isReactive(roItem)).toBe(true)
expect(isReadonly(roItem)).toBe(true)
})
if (Collection === Map) {
test('should retrieve readonly values on iteration', () => {
const key1 = {}
const key2 = {}
const original = new Map([
[key1, {}],
[key2, {}],
])
const wrapped: any = readonly(original)
expect(wrapped.size).toBe(2)
for (const [key, value] of wrapped) {
expect(isReadonly(key)).toBe(true)
expect(isReadonly(value)).toBe(true)
}
wrapped.forEach((value: any) => {
expect(isReadonly(value)).toBe(true)
})
for (const value of wrapped.values()) {
expect(isReadonly(value)).toBe(true)
}
})
test('should retrieve reactive + readonly values on iteration', () => {
const key1 = {}
const key2 = {}
const original = reactive(
new Map([
[key1, {}],
[key2, {}],
]),
)
const wrapped: any = readonly(original)
expect(wrapped.size).toBe(2)
for (const [key, value] of wrapped) {
expect(isReadonly(key)).toBe(true)
expect(isReadonly(value)).toBe(true)
expect(isReactive(key)).toBe(true)
expect(isReactive(value)).toBe(true)
}
wrapped.forEach((value: any) => {
expect(isReadonly(value)).toBe(true)
expect(isReactive(value)).toBe(true)
})
for (const value of wrapped.values()) {
expect(isReadonly(value)).toBe(true)
expect(isReactive(value)).toBe(true)
}
})
test('should return undefined from Map.clear() call', () => {
const wrapped = readonly(new Collection())
expect(wrapped.clear()).toBeUndefined()
expect(
`Clear operation failed: target is readonly.`,
).toHaveBeenWarned()
})
}
})
})
const sets = [Set, WeakSet]
sets.forEach((Collection: any) => {
describe(Collection.name, () => {
test('should make nested values readonly', () => {
const key1 = {}
const key2 = {}
const original = new Collection([key1, key2])
const wrapped = readonly(original)
expect(wrapped).not.toBe(original)
expect(isProxy(wrapped)).toBe(true)
expect(isReactive(wrapped)).toBe(false)
expect(isReadonly(wrapped)).toBe(true)
expect(isReactive(original)).toBe(false)
expect(isReadonly(original)).toBe(false)
expect(wrapped.has(reactive(key1))).toBe(true)
expect(original.has(reactive(key1))).toBe(false)
})
test('should not allow mutation & not trigger effect', () => {
const set = readonly(new Collection())
const key = {}
let dummy
effect(() => {
dummy = set.has(key)
})
expect(dummy).toBe(false)
set.add(key)
expect(dummy).toBe(false)
expect(set.has(key)).toBe(false)
expect(
`Add operation on key "${key}" failed: target is readonly.`,
).toHaveBeenWarned()
})
if (Collection === Set) {
test('should retrieve readonly values on iteration', () => {
const original = new Collection([{}, {}])
const wrapped: any = readonly(original)
expect(wrapped.size).toBe(2)
for (const value of wrapped) {
expect(isReadonly(value)).toBe(true)
}
wrapped.forEach((value: any) => {
expect(isReadonly(value)).toBe(true)
})
for (const value of wrapped.values()) {
expect(isReadonly(value)).toBe(true)
}
for (const [v1, v2] of wrapped.entries()) {
expect(isReadonly(v1)).toBe(true)
expect(isReadonly(v2)).toBe(true)
}
})
test('should return undefined from Set.clear() call', () => {
const wrapped = readonly(new Collection())
expect(wrapped.clear()).toBeUndefined()
expect(
`Clear operation failed: target is readonly.`,
).toHaveBeenWarned()
})
}
})
})
test('calling reactive on an readonly should return readonly', () => {
const a = readonly({})
const b = reactive(a)
expect(isReadonly(b)).toBe(true)
// should point to same original
expect(toRaw(a)).toBe(toRaw(b))
})
test('calling readonly on a reactive object should return readonly', () => {
const a = reactive({})
const b = readonly(a)
expect(isReadonly(b)).toBe(true)
// should point to same original
expect(toRaw(a)).toBe(toRaw(b))
})
test('readonly should track and trigger if wrapping reactive original', () => {
const a = reactive({ n: 1 })
const b = readonly(a)
// should return true since it's wrapping a reactive source
expect(isReactive(b)).toBe(true)
let dummy
effect(() => {
dummy = b.n
})
expect(dummy).toBe(1)
a.n++
expect(b.n).toBe(2)
expect(dummy).toBe(2)
})
test('readonly collection should not track', () => {
const map = new Map()
map.set('foo', 1)
const reMap = reactive(map)
const roMap = readonly(map)
let dummy
effect(() => {
dummy = roMap.get('foo')
})
expect(dummy).toBe(1)
reMap.set('foo', 2)
expect(roMap.get('foo')).toBe(2)
// should not trigger
expect(dummy).toBe(1)
})
test('readonly array should not track', () => {
const arr = [1]
const roArr = readonly(arr)
const eff = effect(() => {
roArr.includes(2)
})
expect(eff.effect.deps).toBeUndefined()
})
test('readonly should track and trigger if wrapping reactive original (collection)', () => {
const a = reactive(new Map())
const b = readonly(a)
// should return true since it's wrapping a reactive source
expect(isReactive(b)).toBe(true)
a.set('foo', 1)
let dummy
effect(() => {
dummy = b.get('foo')
})
expect(dummy).toBe(1)
a.set('foo', 2)
expect(b.get('foo')).toBe(2)
expect(dummy).toBe(2)
})
test('wrapping already wrapped value should return same Proxy', () => {
const original = { foo: 1 }
const wrapped = readonly(original)
const wrapped2 = readonly(wrapped)
expect(wrapped2).toBe(wrapped)
})
test('wrapping the same value multiple times should return same Proxy', () => {
const original = { foo: 1 }
const wrapped = readonly(original)
const wrapped2 = readonly(original)
expect(wrapped2).toBe(wrapped)
})
test('markRaw', () => {
const obj = readonly({
foo: { a: 1 },
bar: markRaw({ b: 2 }),
})
expect(isReadonly(obj.foo)).toBe(true)
expect(isReadonly(obj.bar)).toBe(false)
})
test('should make ref readonly', () => {
const n = readonly(ref(1))
// @ts-expect-error
n.value = 2
expect(n.value).toBe(1)
expect(
`Set operation on key "value" failed: target is readonly.`,
).toHaveBeenWarned()
})
// https://github.com/vuejs/core/issues/3376
test('calling readonly on computed should allow computed to set its private properties', () => {
const r = ref<boolean>(false)
const c = computed(() => r.value)
const rC = readonly(c)
r.value = true
expect(rC.value).toBe(true)
expect(
'Set operation on key "_dirty" failed: target is readonly.',
).not.toHaveBeenWarned()
// @ts-expect-error - non-existent property
rC.randomProperty = true
expect(
'Set operation on key "randomProperty" failed: target is readonly.',
).toHaveBeenWarned()
})
// #4986
test('setting a readonly object as a property of a reactive object should retain readonly proxy', () => {
const r = readonly({})
const rr = reactive({}) as any
rr.foo = r
expect(rr.foo).toBe(r)
expect(isReadonly(rr.foo)).toBe(true)
})
test('attempting to write plain value to a readonly ref nested in a reactive object should fail', () => {
const r = ref(false)
const ror = readonly(r)
const obj = reactive({ ror })
expect(() => {
obj.ror = true
}).toThrow()
expect(obj.ror).toBe(false)
})
test('replacing a readonly ref nested in a reactive object with a new ref', () => {
const r = ref(false)
const ror = readonly(r)
const obj = reactive({ ror })
obj.ror = ref(true) as unknown as boolean
expect(obj.ror).toBe(true)
expect(toRaw(obj).ror).not.toBe(ror) // ref successfully replaced
})
test('setting readonly object to writable nested ref', () => {
const r = ref<any>()
const obj = reactive({ r })
const ro = readonly({})
obj.r = ro
expect(obj.r).toBe(ro)
expect(r.value).toBe(ro)
})
})