5
5
isReactive ,
6
6
isReadonly ,
7
7
markNonReactive ,
8
- lock ,
9
- unlock ,
10
8
effect ,
11
9
ref ,
12
10
shallowReadonly
@@ -90,22 +88,7 @@ describe('reactivity/readonly', () => {
90
88
) . toHaveBeenWarnedLast ( )
91
89
} )
92
90
93
- it ( 'should allow mutation when unlocked' , ( ) => {
94
- const observed : any = readonly ( { foo : 1 , bar : { baz : 2 } } )
95
- unlock ( )
96
- observed . prop = 2
97
- observed . bar . qux = 3
98
- delete observed . bar . baz
99
- delete observed . foo
100
- lock ( )
101
- expect ( observed . prop ) . toBe ( 2 )
102
- expect ( observed . foo ) . toBeUndefined ( )
103
- expect ( observed . bar . qux ) . toBe ( 3 )
104
- expect ( 'baz' in observed . bar ) . toBe ( false )
105
- expect ( `target is readonly` ) . not . toHaveBeenWarned ( )
106
- } )
107
-
108
- it ( 'should not trigger effects when locked' , ( ) => {
91
+ it ( 'should not trigger effects' , ( ) => {
109
92
const observed : any = readonly ( { a : 1 } )
110
93
let dummy
111
94
effect ( ( ) => {
@@ -117,20 +100,6 @@ describe('reactivity/readonly', () => {
117
100
expect ( dummy ) . toBe ( 1 )
118
101
expect ( `target is readonly` ) . toHaveBeenWarned ( )
119
102
} )
120
-
121
- it ( 'should trigger effects when unlocked' , ( ) => {
122
- const observed : any = readonly ( { a : 1 } )
123
- let dummy
124
- effect ( ( ) => {
125
- dummy = observed . a
126
- } )
127
- expect ( dummy ) . toBe ( 1 )
128
- unlock ( )
129
- observed . a = 2
130
- lock ( )
131
- expect ( observed . a ) . toBe ( 2 )
132
- expect ( dummy ) . toBe ( 2 )
133
- } )
134
103
} )
135
104
136
105
describe ( 'Array' , ( ) => {
@@ -182,23 +151,7 @@ describe('reactivity/readonly', () => {
182
151
expect ( `target is readonly.` ) . toHaveBeenWarnedTimes ( 5 )
183
152
} )
184
153
185
- it ( 'should allow mutation when unlocked' , ( ) => {
186
- const observed : any = readonly ( [ { foo : 1 , bar : { baz : 2 } } ] )
187
- unlock ( )
188
- observed [ 1 ] = 2
189
- observed . push ( 3 )
190
- observed [ 0 ] . foo = 2
191
- observed [ 0 ] . bar . baz = 3
192
- lock ( )
193
- expect ( observed . length ) . toBe ( 3 )
194
- expect ( observed [ 1 ] ) . toBe ( 2 )
195
- expect ( observed [ 2 ] ) . toBe ( 3 )
196
- expect ( observed [ 0 ] . foo ) . toBe ( 2 )
197
- expect ( observed [ 0 ] . bar . baz ) . toBe ( 3 )
198
- expect ( `target is readonly` ) . not . toHaveBeenWarned ( )
199
- } )
200
-
201
- it ( 'should not trigger effects when locked' , ( ) => {
154
+ it ( 'should not trigger effects' , ( ) => {
202
155
const observed : any = readonly ( [ { a : 1 } ] )
203
156
let dummy
204
157
effect ( ( ) => {
@@ -214,30 +167,6 @@ describe('reactivity/readonly', () => {
214
167
expect ( dummy ) . toBe ( 1 )
215
168
expect ( `target is readonly` ) . toHaveBeenWarnedTimes ( 2 )
216
169
} )
217
-
218
- it ( 'should trigger effects when unlocked' , ( ) => {
219
- const observed : any = readonly ( [ { a : 1 } ] )
220
- let dummy
221
- effect ( ( ) => {
222
- dummy = observed [ 0 ] . a
223
- } )
224
- expect ( dummy ) . toBe ( 1 )
225
-
226
- unlock ( )
227
-
228
- observed [ 0 ] . a = 2
229
- expect ( observed [ 0 ] . a ) . toBe ( 2 )
230
- expect ( dummy ) . toBe ( 2 )
231
-
232
- observed [ 0 ] = { a : 3 }
233
- expect ( observed [ 0 ] . a ) . toBe ( 3 )
234
- expect ( dummy ) . toBe ( 3 )
235
-
236
- observed . unshift ( { a : 4 } )
237
- expect ( observed [ 0 ] . a ) . toBe ( 4 )
238
- expect ( dummy ) . toBe ( 4 )
239
- lock ( )
240
- } )
241
170
} )
242
171
243
172
const maps = [ Map , WeakMap ]
@@ -275,23 +204,6 @@ describe('reactivity/readonly', () => {
275
204
) . toHaveBeenWarned ( )
276
205
} )
277
206
278
- test ( 'should allow mutation & trigger effect when unlocked' , ( ) => {
279
- const map = readonly ( new Collection ( ) )
280
- const isWeak = Collection === WeakMap
281
- const key = { }
282
- let dummy
283
- effect ( ( ) => {
284
- dummy = map . get ( key ) + ( isWeak ? 0 : map . size )
285
- } )
286
- expect ( dummy ) . toBeNaN ( )
287
- unlock ( )
288
- map . set ( key , 1 )
289
- lock ( )
290
- expect ( dummy ) . toBe ( isWeak ? 1 : 2 )
291
- expect ( map . get ( key ) ) . toBe ( 1 )
292
- expect ( `target is readonly` ) . not . toHaveBeenWarned ( )
293
- } )
294
-
295
207
if ( Collection === Map ) {
296
208
test ( 'should retrieve readonly values on iteration' , ( ) => {
297
209
const key1 = { }
@@ -346,22 +258,6 @@ describe('reactivity/readonly', () => {
346
258
) . toHaveBeenWarned ( )
347
259
} )
348
260
349
- test ( 'should allow mutation & trigger effect when unlocked' , ( ) => {
350
- const set = readonly ( new Collection ( ) )
351
- const key = { }
352
- let dummy
353
- effect ( ( ) => {
354
- dummy = set . has ( key )
355
- } )
356
- expect ( dummy ) . toBe ( false )
357
- unlock ( )
358
- set . add ( key )
359
- lock ( )
360
- expect ( dummy ) . toBe ( true )
361
- expect ( set . has ( key ) ) . toBe ( true )
362
- expect ( `target is readonly` ) . not . toHaveBeenWarned ( )
363
- } )
364
-
365
261
if ( Collection === Set ) {
366
262
test ( 'should retrieve readonly values on iteration' , ( ) => {
367
263
const original = new Collection ( [ { } , { } ] )
@@ -400,6 +296,19 @@ describe('reactivity/readonly', () => {
400
296
expect ( toRaw ( a ) ) . toBe ( toRaw ( b ) )
401
297
} )
402
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
+
403
312
test ( 'observing already observed value should return same Proxy' , ( ) => {
404
313
const original = { foo : 1 }
405
314
const observed = readonly ( original )
@@ -458,13 +367,5 @@ describe('reactivity/readonly', () => {
458
367
`Set operation on key "foo" failed: target is readonly.`
459
368
) . not . toHaveBeenWarned ( )
460
369
} )
461
-
462
- test ( 'should keep reactive properties reactive' , ( ) => {
463
- const props : any = shallowReadonly ( { n : reactive ( { foo : 1 } ) } )
464
- unlock ( )
465
- props . n = reactive ( { foo : 2 } )
466
- lock ( )
467
- expect ( isReactive ( props . n ) ) . toBe ( true )
468
- } )
469
370
} )
470
371
} )
0 commit comments