5
5
isReactive ,
6
6
isReadonly ,
7
7
markNonReactive ,
8
- markReadonly ,
9
- lock ,
10
- unlock ,
11
8
effect ,
12
9
ref ,
13
10
shallowReadonly
@@ -91,22 +88,7 @@ describe('reactivity/readonly', () => {
91
88
) . toHaveBeenWarnedLast ( )
92
89
} )
93
90
94
- it ( 'should allow mutation when unlocked' , ( ) => {
95
- const observed : any = readonly ( { foo : 1 , bar : { baz : 2 } } )
96
- unlock ( )
97
- observed . prop = 2
98
- observed . bar . qux = 3
99
- delete observed . bar . baz
100
- delete observed . foo
101
- lock ( )
102
- expect ( observed . prop ) . toBe ( 2 )
103
- expect ( observed . foo ) . toBeUndefined ( )
104
- expect ( observed . bar . qux ) . toBe ( 3 )
105
- expect ( 'baz' in observed . bar ) . toBe ( false )
106
- expect ( `target is readonly` ) . not . toHaveBeenWarned ( )
107
- } )
108
-
109
- it ( 'should not trigger effects when locked' , ( ) => {
91
+ it ( 'should not trigger effects' , ( ) => {
110
92
const observed : any = readonly ( { a : 1 } )
111
93
let dummy
112
94
effect ( ( ) => {
@@ -118,20 +100,6 @@ describe('reactivity/readonly', () => {
118
100
expect ( dummy ) . toBe ( 1 )
119
101
expect ( `target is readonly` ) . toHaveBeenWarned ( )
120
102
} )
121
-
122
- it ( 'should trigger effects when unlocked' , ( ) => {
123
- const observed : any = readonly ( { a : 1 } )
124
- let dummy
125
- effect ( ( ) => {
126
- dummy = observed . a
127
- } )
128
- expect ( dummy ) . toBe ( 1 )
129
- unlock ( )
130
- observed . a = 2
131
- lock ( )
132
- expect ( observed . a ) . toBe ( 2 )
133
- expect ( dummy ) . toBe ( 2 )
134
- } )
135
103
} )
136
104
137
105
describe ( 'Array' , ( ) => {
@@ -183,23 +151,7 @@ describe('reactivity/readonly', () => {
183
151
expect ( `target is readonly.` ) . toHaveBeenWarnedTimes ( 5 )
184
152
} )
185
153
186
- it ( 'should allow mutation when unlocked' , ( ) => {
187
- const observed : any = readonly ( [ { foo : 1 , bar : { baz : 2 } } ] )
188
- unlock ( )
189
- observed [ 1 ] = 2
190
- observed . push ( 3 )
191
- observed [ 0 ] . foo = 2
192
- observed [ 0 ] . bar . baz = 3
193
- lock ( )
194
- expect ( observed . length ) . toBe ( 3 )
195
- expect ( observed [ 1 ] ) . toBe ( 2 )
196
- expect ( observed [ 2 ] ) . toBe ( 3 )
197
- expect ( observed [ 0 ] . foo ) . toBe ( 2 )
198
- expect ( observed [ 0 ] . bar . baz ) . toBe ( 3 )
199
- expect ( `target is readonly` ) . not . toHaveBeenWarned ( )
200
- } )
201
-
202
- it ( 'should not trigger effects when locked' , ( ) => {
154
+ it ( 'should not trigger effects' , ( ) => {
203
155
const observed : any = readonly ( [ { a : 1 } ] )
204
156
let dummy
205
157
effect ( ( ) => {
@@ -215,30 +167,6 @@ describe('reactivity/readonly', () => {
215
167
expect ( dummy ) . toBe ( 1 )
216
168
expect ( `target is readonly` ) . toHaveBeenWarnedTimes ( 2 )
217
169
} )
218
-
219
- it ( 'should trigger effects when unlocked' , ( ) => {
220
- const observed : any = readonly ( [ { a : 1 } ] )
221
- let dummy
222
- effect ( ( ) => {
223
- dummy = observed [ 0 ] . a
224
- } )
225
- expect ( dummy ) . toBe ( 1 )
226
-
227
- unlock ( )
228
-
229
- observed [ 0 ] . a = 2
230
- expect ( observed [ 0 ] . a ) . toBe ( 2 )
231
- expect ( dummy ) . toBe ( 2 )
232
-
233
- observed [ 0 ] = { a : 3 }
234
- expect ( observed [ 0 ] . a ) . toBe ( 3 )
235
- expect ( dummy ) . toBe ( 3 )
236
-
237
- observed . unshift ( { a : 4 } )
238
- expect ( observed [ 0 ] . a ) . toBe ( 4 )
239
- expect ( dummy ) . toBe ( 4 )
240
- lock ( )
241
- } )
242
170
} )
243
171
244
172
const maps = [ Map , WeakMap ]
@@ -276,23 +204,6 @@ describe('reactivity/readonly', () => {
276
204
) . toHaveBeenWarned ( )
277
205
} )
278
206
279
- test ( 'should allow mutation & trigger effect when unlocked' , ( ) => {
280
- const map = readonly ( new Collection ( ) )
281
- const isWeak = Collection === WeakMap
282
- const key = { }
283
- let dummy
284
- effect ( ( ) => {
285
- dummy = map . get ( key ) + ( isWeak ? 0 : map . size )
286
- } )
287
- expect ( dummy ) . toBeNaN ( )
288
- unlock ( )
289
- map . set ( key , 1 )
290
- lock ( )
291
- expect ( dummy ) . toBe ( isWeak ? 1 : 2 )
292
- expect ( map . get ( key ) ) . toBe ( 1 )
293
- expect ( `target is readonly` ) . not . toHaveBeenWarned ( )
294
- } )
295
-
296
207
if ( Collection === Map ) {
297
208
test ( 'should retrieve readonly values on iteration' , ( ) => {
298
209
const key1 = { }
@@ -347,22 +258,6 @@ describe('reactivity/readonly', () => {
347
258
) . toHaveBeenWarned ( )
348
259
} )
349
260
350
- test ( 'should allow mutation & trigger effect when unlocked' , ( ) => {
351
- const set = readonly ( new Collection ( ) )
352
- const key = { }
353
- let dummy
354
- effect ( ( ) => {
355
- dummy = set . has ( key )
356
- } )
357
- expect ( dummy ) . toBe ( false )
358
- unlock ( )
359
- set . add ( key )
360
- lock ( )
361
- expect ( dummy ) . toBe ( true )
362
- expect ( set . has ( key ) ) . toBe ( true )
363
- expect ( `target is readonly` ) . not . toHaveBeenWarned ( )
364
- } )
365
-
366
261
if ( Collection === Set ) {
367
262
test ( 'should retrieve readonly values on iteration' , ( ) => {
368
263
const original = new Collection ( [ { } , { } ] )
@@ -401,6 +296,19 @@ describe('reactivity/readonly', () => {
401
296
expect ( toRaw ( a ) ) . toBe ( toRaw ( b ) )
402
297
} )
403
298
299
+ test ( 'readonly should track and trigger if wrapping reactive original' , ( ) => {
300
+ const a = reactive ( { n : 1 } )
301
+ const b = readonly ( a )
302
+ let dummy
303
+ effect ( ( ) => {
304
+ dummy = b . n
305
+ } )
306
+ expect ( dummy ) . toBe ( 1 )
307
+ a . n ++
308
+ expect ( b . n ) . toBe ( 2 )
309
+ expect ( dummy ) . toBe ( 2 )
310
+ } )
311
+
404
312
test ( 'observing already observed value should return same Proxy' , ( ) => {
405
313
const original = { foo : 1 }
406
314
const observed = readonly ( original )
@@ -424,17 +332,6 @@ describe('reactivity/readonly', () => {
424
332
expect ( isReactive ( obj . bar ) ) . toBe ( false )
425
333
} )
426
334
427
- test ( 'markReadonly' , ( ) => {
428
- const obj = reactive ( {
429
- foo : { a : 1 } ,
430
- bar : markReadonly ( { b : 2 } )
431
- } )
432
- expect ( isReactive ( obj . foo ) ) . toBe ( true )
433
- expect ( isReactive ( obj . bar ) ) . toBe ( true )
434
- expect ( isReadonly ( obj . foo ) ) . toBe ( false )
435
- expect ( isReadonly ( obj . bar ) ) . toBe ( true )
436
- } )
437
-
438
335
test ( 'should make ref readonly' , ( ) => {
439
336
const n : any = readonly ( ref ( 1 ) )
440
337
n . value = 2
@@ -470,13 +367,5 @@ describe('reactivity/readonly', () => {
470
367
`Set operation on key "foo" failed: target is readonly.`
471
368
) . not . toHaveBeenWarned ( )
472
369
} )
473
-
474
- test ( 'should keep reactive properties reactive' , ( ) => {
475
- const props : any = shallowReadonly ( { n : reactive ( { foo : 1 } ) } )
476
- unlock ( )
477
- props . n = reactive ( { foo : 2 } )
478
- lock ( )
479
- expect ( isReactive ( props . n ) ) . toBe ( true )
480
- } )
481
370
} )
482
371
} )
0 commit comments