55 isReactive ,
66 isReadonly ,
77 markNonReactive ,
8- markReadonly ,
9- lock ,
10- unlock ,
118 effect ,
129 ref ,
1310 shallowReadonly
@@ -91,22 +88,7 @@ describe('reactivity/readonly', () => {
9188 ) . toHaveBeenWarnedLast ( )
9289 } )
9390
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' , ( ) => {
11092 const observed : any = readonly ( { a : 1 } )
11193 let dummy
11294 effect ( ( ) => {
@@ -118,20 +100,6 @@ describe('reactivity/readonly', () => {
118100 expect ( dummy ) . toBe ( 1 )
119101 expect ( `target is readonly` ) . toHaveBeenWarned ( )
120102 } )
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- } )
135103 } )
136104
137105 describe ( 'Array' , ( ) => {
@@ -183,23 +151,7 @@ describe('reactivity/readonly', () => {
183151 expect ( `target is readonly.` ) . toHaveBeenWarnedTimes ( 5 )
184152 } )
185153
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' , ( ) => {
203155 const observed : any = readonly ( [ { a : 1 } ] )
204156 let dummy
205157 effect ( ( ) => {
@@ -215,30 +167,6 @@ describe('reactivity/readonly', () => {
215167 expect ( dummy ) . toBe ( 1 )
216168 expect ( `target is readonly` ) . toHaveBeenWarnedTimes ( 2 )
217169 } )
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- } )
242170 } )
243171
244172 const maps = [ Map , WeakMap ]
@@ -276,23 +204,6 @@ describe('reactivity/readonly', () => {
276204 ) . toHaveBeenWarned ( )
277205 } )
278206
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-
296207 if ( Collection === Map ) {
297208 test ( 'should retrieve readonly values on iteration' , ( ) => {
298209 const key1 = { }
@@ -347,22 +258,6 @@ describe('reactivity/readonly', () => {
347258 ) . toHaveBeenWarned ( )
348259 } )
349260
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-
366261 if ( Collection === Set ) {
367262 test ( 'should retrieve readonly values on iteration' , ( ) => {
368263 const original = new Collection ( [ { } , { } ] )
@@ -401,6 +296,19 @@ describe('reactivity/readonly', () => {
401296 expect ( toRaw ( a ) ) . toBe ( toRaw ( b ) )
402297 } )
403298
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+
404312 test ( 'observing already observed value should return same Proxy' , ( ) => {
405313 const original = { foo : 1 }
406314 const observed = readonly ( original )
@@ -424,17 +332,6 @@ describe('reactivity/readonly', () => {
424332 expect ( isReactive ( obj . bar ) ) . toBe ( false )
425333 } )
426334
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-
438335 test ( 'should make ref readonly' , ( ) => {
439336 const n : any = readonly ( ref ( 1 ) )
440337 n . value = 2
@@ -470,13 +367,5 @@ describe('reactivity/readonly', () => {
470367 `Set operation on key "foo" failed: target is readonly.`
471368 ) . not . toHaveBeenWarned ( )
472369 } )
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- } )
481370 } )
482371} )
0 commit comments