55 isReactive ,
66 isReadonly ,
77 markNonReactive ,
8- lock ,
9- unlock ,
108 effect ,
119 ref ,
1210 shallowReadonly
@@ -90,22 +88,7 @@ describe('reactivity/readonly', () => {
9088 ) . toHaveBeenWarnedLast ( )
9189 } )
9290
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' , ( ) => {
10992 const observed : any = readonly ( { a : 1 } )
11093 let dummy
11194 effect ( ( ) => {
@@ -117,20 +100,6 @@ describe('reactivity/readonly', () => {
117100 expect ( dummy ) . toBe ( 1 )
118101 expect ( `target is readonly` ) . toHaveBeenWarned ( )
119102 } )
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- } )
134103 } )
135104
136105 describe ( 'Array' , ( ) => {
@@ -182,23 +151,7 @@ describe('reactivity/readonly', () => {
182151 expect ( `target is readonly.` ) . toHaveBeenWarnedTimes ( 5 )
183152 } )
184153
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' , ( ) => {
202155 const observed : any = readonly ( [ { a : 1 } ] )
203156 let dummy
204157 effect ( ( ) => {
@@ -214,30 +167,6 @@ describe('reactivity/readonly', () => {
214167 expect ( dummy ) . toBe ( 1 )
215168 expect ( `target is readonly` ) . toHaveBeenWarnedTimes ( 2 )
216169 } )
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- } )
241170 } )
242171
243172 const maps = [ Map , WeakMap ]
@@ -275,23 +204,6 @@ describe('reactivity/readonly', () => {
275204 ) . toHaveBeenWarned ( )
276205 } )
277206
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-
295207 if ( Collection === Map ) {
296208 test ( 'should retrieve readonly values on iteration' , ( ) => {
297209 const key1 = { }
@@ -346,22 +258,6 @@ describe('reactivity/readonly', () => {
346258 ) . toHaveBeenWarned ( )
347259 } )
348260
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-
365261 if ( Collection === Set ) {
366262 test ( 'should retrieve readonly values on iteration' , ( ) => {
367263 const original = new Collection ( [ { } , { } ] )
@@ -400,6 +296,19 @@ describe('reactivity/readonly', () => {
400296 expect ( toRaw ( a ) ) . toBe ( toRaw ( b ) )
401297 } )
402298
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+
403312 test ( 'observing already observed value should return same Proxy' , ( ) => {
404313 const original = { foo : 1 }
405314 const observed = readonly ( original )
@@ -458,13 +367,5 @@ describe('reactivity/readonly', () => {
458367 `Set operation on key "foo" failed: target is readonly.`
459368 ) . not . toHaveBeenWarned ( )
460369 } )
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- } )
469370 } )
470371} )
0 commit comments