Skip to content

Commit efdba1e

Browse files
committed
test: add activated and deactivated for testing
1 parent 00c7223 commit efdba1e

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

packages/runtime-core/__tests__/hmr.spec.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ describe('hot module replacement', () => {
156156
const childId = 'test-child-keep-alive'
157157
const unmountSpy = jest.fn()
158158
const mountSpy = jest.fn()
159+
const activeSpy = jest.fn()
160+
const deactiveSpy = jest.fn()
159161

160162
const Child: ComponentOptions = {
161163
__hmrId: childId,
@@ -169,24 +171,51 @@ describe('hot module replacement', () => {
169171

170172
const Parent: ComponentOptions = {
171173
components: { Child },
172-
render: compileToFunction(`<KeepAlive><Child/></KeepAlive>`)
174+
data() {
175+
return { toggle: true }
176+
},
177+
render: compileToFunction(
178+
`<button @click="toggle = !toggle"></button><KeepAlive><Child v-if="toggle" /></KeepAlive>`
179+
)
173180
}
174181

175182
render(h(Parent), root)
176-
expect(serializeInner(root)).toBe(`<div>0</div>`)
183+
expect(serializeInner(root)).toBe(`<button></button><div>0</div>`)
177184

178185
reload(childId, {
179186
__hmrId: childId,
180187
data() {
181188
return { count: 1 }
182189
},
183190
mounted: mountSpy,
191+
unmounted: unmountSpy,
192+
activated: activeSpy,
193+
deactivated: deactiveSpy,
184194
render: compileToFunction(`<div>{{ count }}</div>`)
185195
})
186196
await nextTick()
187-
expect(serializeInner(root)).toBe(`<div>1</div>`)
197+
expect(serializeInner(root)).toBe(`<button></button><div>1</div>`)
198+
expect(unmountSpy).toHaveBeenCalledTimes(1)
199+
expect(mountSpy).toHaveBeenCalledTimes(1)
200+
expect(activeSpy).toHaveBeenCalledTimes(1)
201+
expect(deactiveSpy).toHaveBeenCalledTimes(0)
202+
203+
204+
// should not unmount when toggling
205+
triggerEvent(root.children[1] as TestElement, 'click')
206+
await nextTick()
207+
expect(unmountSpy).toHaveBeenCalledTimes(1)
208+
expect(mountSpy).toHaveBeenCalledTimes(1)
209+
expect(activeSpy).toHaveBeenCalledTimes(1)
210+
expect(deactiveSpy).toHaveBeenCalledTimes(1)
211+
212+
// should not mount when toggling
213+
triggerEvent(root.children[1] as TestElement, 'click')
214+
await nextTick()
188215
expect(unmountSpy).toHaveBeenCalledTimes(1)
189216
expect(mountSpy).toHaveBeenCalledTimes(1)
217+
expect(activeSpy).toHaveBeenCalledTimes(2)
218+
expect(deactiveSpy).toHaveBeenCalledTimes(1)
190219
})
191220

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

0 commit comments

Comments
 (0)