fix(runtime-core): fix beforeUpdate call timing to allow state mutation

fix #1899
This commit is contained in:
Evan You
2020-08-19 17:57:51 -04:00
parent 24041b7ac1
commit 1eb6067a85
2 changed files with 54 additions and 16 deletions

View File

@@ -74,6 +74,36 @@ describe('api: lifecycle hooks', () => {
count.value++
await nextTick()
expect(fn).toHaveBeenCalledTimes(1)
expect(serializeInner(root)).toBe(`<div>1</div>`)
})
it('state mutation in onBeforeUpdate', async () => {
const count = ref(0)
const root = nodeOps.createElement('div')
const fn = jest.fn(() => {
// should be called before inner div is updated
expect(serializeInner(root)).toBe(`<div>0</div>`)
count.value++
})
const renderSpy = jest.fn()
const Comp = {
setup() {
onBeforeUpdate(fn)
return () => {
renderSpy()
return h('div', count.value)
}
}
}
render(h(Comp), root)
expect(renderSpy).toHaveBeenCalledTimes(1)
count.value++
await nextTick()
expect(fn).toHaveBeenCalledTimes(1)
expect(renderSpy).toHaveBeenCalledTimes(2)
expect(serializeInner(root)).toBe(`<div>2</div>`)
})
it('onUpdated', async () => {