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(runtime-core): allow spying on proxy methods #4216

Merged
merged 1 commit into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions packages/runtime-core/__tests__/componentPublicInstance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,74 @@ describe('component: proxy', () => {
])
})

test('allow updating proxy with Object.defineProperty', () => {
let instanceProxy: any
const Comp = {
render() {},
setup() {
return {
isDisplayed: true
}
},
mounted() {
instanceProxy = this
}
}

const app = createApp(Comp)

app.mount(nodeOps.createElement('div'))

Object.defineProperty(instanceProxy, 'isDisplayed', { value: false })

expect(instanceProxy.isDisplayed).toBe(false)

Object.defineProperty(instanceProxy, 'isDisplayed', { value: true })

expect(instanceProxy.isDisplayed).toBe(true)

Object.defineProperty(instanceProxy, 'isDisplayed', {
get() {
return false
}
})

expect(instanceProxy.isDisplayed).toBe(false)

Object.defineProperty(instanceProxy, 'isDisplayed', {
get() {
return true
}
})

expect(instanceProxy.isDisplayed).toBe(true)
})

test('allow spying on proxy methods', () => {
let instanceProxy: any
const Comp = {
render() {},
setup() {
return {
toggle() {}
}
},
mounted() {
instanceProxy = this
}
}

const app = createApp(Comp)

app.mount(nodeOps.createElement('div'))

const spy = jest.spyOn(instanceProxy, 'toggle')

instanceProxy.toggle()

expect(spy).toHaveBeenCalled()
})

// #864
test('should not warn declared but absent props', () => {
const Comp = {
Expand Down
15 changes: 15 additions & 0 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,10 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
const { data, setupState, ctx } = instance
if (setupState !== EMPTY_OBJ && hasOwn(setupState, key)) {
setupState[key] = value
return true
} else if (data !== EMPTY_OBJ && hasOwn(data, key)) {
data[key] = value
return true
} else if (hasOwn(instance.props, key)) {
__DEV__ &&
warn(
Expand Down Expand Up @@ -445,6 +447,19 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
hasOwn(publicPropertiesMap, key) ||
hasOwn(appContext.config.globalProperties, key)
)
},

defineProperty(
target: ComponentRenderContext,
key: string,
descriptor: PropertyDescriptor
) {
if (descriptor.get != null) {
this.set!(target, key, descriptor.get(), null)
} else if (descriptor.value != null) {
this.set!(target, key, descriptor.value, null)
}
return Reflect.defineProperty(target, key, descriptor)
}
}

Expand Down