Skip to content

Commit

Permalink
test: add activated and deactivated for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhonghe committed Nov 8, 2022
1 parent 00c7223 commit b08e601
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions packages/runtime-core/__tests__/hmr.spec.ts
Expand Up @@ -156,6 +156,8 @@ describe('hot module replacement', () => {
const childId = 'test-child-keep-alive'
const unmountSpy = jest.fn()
const mountSpy = jest.fn()
const activeSpy = jest.fn()
const deactiveSpy = jest.fn()

const Child: ComponentOptions = {
__hmrId: childId,
Expand All @@ -169,24 +171,50 @@ describe('hot module replacement', () => {

const Parent: ComponentOptions = {
components: { Child },
render: compileToFunction(`<KeepAlive><Child/></KeepAlive>`)
data() {
return { toggle: true }
},
render: compileToFunction(
`<button @click="toggle = !toggle"></button><KeepAlive><Child v-if="toggle" /></KeepAlive>`
)
}

render(h(Parent), root)
expect(serializeInner(root)).toBe(`<div>0</div>`)
expect(serializeInner(root)).toBe(`<button></button><div>0</div>`)

reload(childId, {
__hmrId: childId,
data() {
return { count: 1 }
},
mounted: mountSpy,
unmounted: unmountSpy,
activated: activeSpy,
deactivated: deactiveSpy,
render: compileToFunction(`<div>{{ count }}</div>`)
})
await nextTick()
expect(serializeInner(root)).toBe(`<div>1</div>`)
expect(serializeInner(root)).toBe(`<button></button><div>1</div>`)
expect(unmountSpy).toHaveBeenCalledTimes(1)
expect(mountSpy).toHaveBeenCalledTimes(1)
expect(activeSpy).toHaveBeenCalledTimes(1)
expect(deactiveSpy).toHaveBeenCalledTimes(0)

// should not unmount when toggling
triggerEvent(root.children[1] as TestElement, 'click')
await nextTick()
expect(unmountSpy).toHaveBeenCalledTimes(1)
expect(mountSpy).toHaveBeenCalledTimes(1)
expect(activeSpy).toHaveBeenCalledTimes(1)
expect(deactiveSpy).toHaveBeenCalledTimes(1)

// should not mount when toggling
triggerEvent(root.children[1] as TestElement, 'click')
await nextTick()
expect(unmountSpy).toHaveBeenCalledTimes(1)
expect(mountSpy).toHaveBeenCalledTimes(1)
expect(activeSpy).toHaveBeenCalledTimes(2)
expect(deactiveSpy).toHaveBeenCalledTimes(1)
})

test('reload class component', async () => {
Expand Down

0 comments on commit b08e601

Please sign in to comment.