File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 77 TriggerOpTypes ,
88 DebuggerEvent ,
99 markRaw ,
10- shallowReactive
10+ shallowReactive ,
11+ readonly
1112} from '../src/index'
1213import { ITERATE_KEY } from '../src/effect'
1314
@@ -832,4 +833,32 @@ describe('reactivity/effect', () => {
832833 observed2 . obj2 = obj2
833834 expect ( fnSpy2 ) . toHaveBeenCalledTimes ( 1 )
834835 } )
836+
837+ describe ( 'readonly + reactive for Map' , ( ) => {
838+ test ( 'should work with readonly(reactive(Map))' , ( ) => {
839+ const m = reactive ( new Map ( ) )
840+ const roM = readonly ( m )
841+ const fnSpy = jest . fn ( ( ) => roM . get ( 1 ) )
842+
843+ effect ( fnSpy )
844+ expect ( fnSpy ) . toHaveBeenCalledTimes ( 1 )
845+ m . set ( 1 , 1 )
846+ expect ( fnSpy ) . toHaveBeenCalledTimes ( 2 )
847+ } )
848+
849+ test ( 'should work with observed value as key' , ( ) => {
850+ const key = reactive ( { } )
851+ const m = reactive ( new Map ( ) )
852+ m . set ( key , 1 )
853+ const roM = readonly ( m )
854+ const fnSpy = jest . fn ( ( ) => roM . get ( key ) )
855+
856+ effect ( fnSpy )
857+ expect ( fnSpy ) . toHaveBeenCalledTimes ( 1 )
858+ m . set ( key , 1 )
859+ expect ( fnSpy ) . toHaveBeenCalledTimes ( 1 )
860+ m . set ( key , 2 )
861+ expect ( fnSpy ) . toHaveBeenCalledTimes ( 2 )
862+ } )
863+ } )
835864} )
Original file line number Diff line number Diff line change @@ -49,6 +49,10 @@ function get(
4949 return wrap ( target . get ( key ) )
5050 } else if ( has . call ( rawTarget , rawKey ) ) {
5151 return wrap ( target . get ( rawKey ) )
52+ } else if ( target !== rawTarget ) {
53+ // #3602 readonly(reactive(Map))
54+ // ensure that the nested reactive `Map` can do tracking for itself
55+ target . get ( key )
5256 }
5357}
5458
You can’t perform that action at this time.
0 commit comments