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): vnode.el is null in watcher after rerendering (fix #2170) #2295

Merged
merged 1 commit into from
Oct 5, 2020
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
79 changes: 79 additions & 0 deletions packages/runtime-core/__tests__/rendererComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,83 @@ describe('renderer: component', () => {
await nextTick()
expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
})

// #2170
test('should have access to instance’s “$el” property in watcher when setting instance data', async () => {
function returnThis(this: any) {
return this
}
const dataWatchSpy = jest.fn(returnThis)
let instance: any
const Comp = {
data() {
return {
testData: undefined
}
},

watch: {
testData() {
// @ts-ignore
dataWatchSpy(this.$el)
}
},

created() {
instance = this
},

render() {
return h('div')
}
}

const root = nodeOps.createElement('div')
render(h(Comp), root)

expect(dataWatchSpy).not.toHaveBeenCalled()
instance.testData = 'data'

await nextTick()
expect(dataWatchSpy).toHaveBeenCalledWith(instance.$el)
})

// #2170
test('should have access to instance’s “$el” property in watcher when rendereing with watched prop', async () => {
function returnThis(this: any) {
return this
}
const propWatchSpy = jest.fn(returnThis)
let instance: any
const Comp = {
props: {
testProp: String
},

watch: {
testProp() {
// @ts-ignore
propWatchSpy(this.$el)
}
},

created() {
instance = this
},

render() {
return h('div')
}
}

const root = nodeOps.createElement('div')

render(h(Comp), root)
await nextTick()
expect(propWatchSpy).not.toHaveBeenCalled()

render(h(Comp, { testProp: 'prop ' }), root)
await nextTick()
expect(propWatchSpy).toHaveBeenCalledWith(instance.$el)
})
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1425,11 +1425,11 @@ function baseCreateRenderer(
}

if (next) {
next.el = vnode.el
updateComponentPreRender(instance, next, optimized)
} else {
next = vnode
}
next.el = vnode.el

// beforeUpdate hook
if (bu) {
Expand Down