Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): fix track hasOwnProperty #2621

Closed
wants to merge 12 commits into from
34 changes: 34 additions & 0 deletions packages/reactivity/__tests__/effect.spec.ts
Expand Up @@ -900,6 +900,40 @@ describe('reactivity/effect', () => {
expect(record).toBeUndefined()
})

it('should track hasOwnProperty when obj call it itself', () => {
const obj: any = reactive({})
const has = ref(false)
const fnSpy = jest.fn()

effect(() => {
fnSpy()
has.value = obj.hasOwnProperty('foo')
})
expect(fnSpy).toHaveBeenCalledTimes(1)
expect(has.value).toBe(false)

obj.foo = 1
expect(fnSpy).toHaveBeenCalledTimes(2)
expect(has.value).toBe(true)
})

it('should not track hasOwnProperty when Object.prototype.hasOwnProperty.call', () => {
const obj: any = reactive({})
const has = ref(false)
const fnSpy = jest.fn()

effect(() => {
fnSpy()
has.value = Object.prototype.hasOwnProperty.call(obj, 'foo')
})
expect(fnSpy).toHaveBeenCalledTimes(1)
expect(has.value).toBe(false)

obj.foo = 1
expect(fnSpy).toHaveBeenCalledTimes(1)
expect(has.value).toBe(false)
})

it('should not be triggered when set with the same proxy', () => {
const obj = reactive({ foo: 1 })
const observed: any = reactive({ obj })
Expand Down
7 changes: 6 additions & 1 deletion packages/reactivity/src/baseHandlers.ts
Expand Up @@ -121,7 +121,12 @@ function createGetter(isReadonly = false, shallow = false) {
}

if (!isReadonly) {
track(target, TrackOpTypes.GET, key)
track(
target,
TrackOpTypes.GET,
// #2619
key === 'hasOwnProperty' ? ITERATE_KEY : key
)
}

if (shallow) {
Expand Down