File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed
Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -964,5 +964,31 @@ describe('reactivity/effect', () => {
964964 m . set ( key , 2 )
965965 expect ( fnSpy ) . toHaveBeenCalledTimes ( 2 )
966966 } )
967+
968+ test ( 'should track hasOwnProperty' , ( ) => {
969+ const obj : any = reactive ( { } )
970+ let has = false
971+ const fnSpy = jest . fn ( )
972+
973+ effect ( ( ) => {
974+ fnSpy ( )
975+ has = obj . hasOwnProperty ( 'foo' )
976+ } )
977+ expect ( fnSpy ) . toHaveBeenCalledTimes ( 1 )
978+ expect ( has ) . toBe ( false )
979+
980+ obj . foo = 1
981+ expect ( fnSpy ) . toHaveBeenCalledTimes ( 2 )
982+ expect ( has ) . toBe ( true )
983+
984+ delete obj . foo
985+ expect ( fnSpy ) . toHaveBeenCalledTimes ( 3 )
986+ expect ( has ) . toBe ( false )
987+
988+ // should not trigger on unrelated key
989+ obj . bar = 2
990+ expect ( fnSpy ) . toHaveBeenCalledTimes ( 3 )
991+ expect ( has ) . toBe ( false )
992+ } )
967993 } )
968994} )
Original file line number Diff line number Diff line change @@ -85,6 +85,13 @@ function createArrayInstrumentations() {
8585 return instrumentations
8686}
8787
88+ function hasOwnProperty ( key : string ) {
89+ // @ts -ignore
90+ const obj = toRaw ( this )
91+ track ( obj , TrackOpTypes . HAS , key )
92+ return obj . hasOwnProperty ( key )
93+ }
94+
8895function createGetter ( isReadonly = false , shallow = false ) {
8996 return function get ( target : Target , key : string | symbol , receiver : object ) {
9097 if ( key === ReactiveFlags . IS_REACTIVE ) {
@@ -110,8 +117,13 @@ function createGetter(isReadonly = false, shallow = false) {
110117
111118 const targetIsArray = isArray ( target )
112119
113- if ( ! isReadonly && targetIsArray && hasOwn ( arrayInstrumentations , key ) ) {
114- return Reflect . get ( arrayInstrumentations , key , receiver )
120+ if ( ! isReadonly ) {
121+ if ( targetIsArray && hasOwn ( arrayInstrumentations , key ) ) {
122+ return Reflect . get ( arrayInstrumentations , key , receiver )
123+ }
124+ if ( key === 'hasOwnProperty' ) {
125+ return hasOwnProperty
126+ }
115127 }
116128
117129 const res = Reflect . get ( target , key , receiver )
You can’t perform that action at this time.
0 commit comments