Skip to content

Commit 3e4b55f

Browse files
committed
refactor(tests): port async component and keepalive tests
1 parent ed6face commit 3e4b55f

File tree

5 files changed

+144
-144
lines changed

5 files changed

+144
-144
lines changed

packages-private/vapor-e2e-test/__tests__/vdomInterop.spec.ts

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const {
1111
text,
1212
enterValue,
1313
html,
14-
value,
1514
transitionStart,
1615
waitForElement,
1716
nextFrame,
@@ -98,81 +97,6 @@ describe('vdom / vapor interop', () => {
9897
E2E_TIMEOUT,
9998
)
10099

101-
describe('async component', () => {
102-
const container = '.async-component-interop'
103-
test(
104-
'with-vdom-inner-component',
105-
async () => {
106-
const testContainer = `${container} .with-vdom-component`
107-
expect(await html(testContainer)).toBe('<span>loading...</span>')
108-
109-
await timeout(duration)
110-
expect(await html(testContainer)).toBe('<div>foo</div>')
111-
},
112-
E2E_TIMEOUT,
113-
)
114-
})
115-
116-
describe('keepalive', () => {
117-
test(
118-
'render vapor component',
119-
async () => {
120-
const testSelector = '.render-vapor-component'
121-
const btnShow = `${testSelector} .btn-show`
122-
const btnToggle = `${testSelector} .btn-toggle`
123-
const container = `${testSelector} > div`
124-
const inputSelector = `${testSelector} input`
125-
126-
let calls = await page().evaluate(() => {
127-
return (window as any).getCalls()
128-
})
129-
expect(calls).toStrictEqual(['mounted', 'activated'])
130-
131-
expect(await html(container)).toBe('<input type="text">')
132-
expect(await value(inputSelector)).toBe('vapor')
133-
134-
// change input value
135-
await enterValue(inputSelector, 'changed')
136-
expect(await value(inputSelector)).toBe('changed')
137-
138-
// deactivate
139-
await click(btnToggle)
140-
expect(await html(container)).toBe('<!---->')
141-
calls = await page().evaluate(() => {
142-
return (window as any).getCalls()
143-
})
144-
expect(calls).toStrictEqual(['deactivated'])
145-
146-
// activate
147-
await click(btnToggle)
148-
expect(await html(container)).toBe('<input type="text">')
149-
expect(await value(inputSelector)).toBe('changed')
150-
calls = await page().evaluate(() => {
151-
return (window as any).getCalls()
152-
})
153-
expect(calls).toStrictEqual(['activated'])
154-
155-
// unmount keepalive
156-
await click(btnShow)
157-
expect(await html(container)).toBe('<!---->')
158-
calls = await page().evaluate(() => {
159-
return (window as any).getCalls()
160-
})
161-
expect(calls).toStrictEqual(['deactivated', 'unmounted'])
162-
163-
// mount keepalive
164-
await click(btnShow)
165-
expect(await html(container)).toBe('<input type="text">')
166-
expect(await value(inputSelector)).toBe('vapor')
167-
calls = await page().evaluate(() => {
168-
return (window as any).getCalls()
169-
})
170-
expect(calls).toStrictEqual(['mounted', 'activated'])
171-
},
172-
E2E_TIMEOUT,
173-
)
174-
})
175-
176100
describe('vdom transition', () => {
177101
test(
178102
'render vapor component',

packages-private/vapor-e2e-test/interop/App.vue

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
11
<script setup lang="ts">
2-
import { ref, defineVaporAsyncComponent, h, shallowRef } from 'vue'
2+
import { ref, shallowRef } from 'vue'
33
import VaporComp from './components/VaporComp.vue'
4-
import VdomFoo from './components/VdomFoo.vue'
5-
import SimpleVaporComp from './components/SimpleVaporComp.vue'
64
import VaporCompA from '../transition/components/VaporCompA.vue'
75
import VdomComp from '../transition/components/VdomComp.vue'
86
import VaporSlot from '../transition/components/VaporSlot.vue'
97
108
const msg = ref('hello')
119
const passSlot = ref(true)
1210
13-
const duration = typeof process !== 'undefined' && process.env.CI ? 200 : 50
14-
15-
const AsyncVDomFoo = defineVaporAsyncComponent({
16-
loader: () => {
17-
return new Promise(r => {
18-
setTimeout(() => {
19-
r(VdomFoo as any)
20-
}, duration)
21-
})
22-
},
23-
loadingComponent: () => h('span', 'loading...'),
24-
})
25-
;(window as any).calls = []
26-
;(window as any).getCalls = () => {
27-
const ret = (window as any).calls.slice()
28-
;(window as any).calls = []
29-
return ret
30-
}
31-
32-
const show = ref(true)
33-
const toggle = ref(true)
3411
const toggleVapor = ref(true)
3512
const interopComponent = shallowRef(VdomComp)
3613
function toggleInteropComponent() {
@@ -55,25 +32,6 @@ const enterClick = () => items.value.push('d', 'e')
5532

5633
<template #test v-if="passSlot">A test slot</template>
5734
</VaporComp>
58-
59-
<!-- async component -->
60-
<div class="async-component-interop">
61-
<div class="with-vdom-component">
62-
<AsyncVDomFoo />
63-
</div>
64-
</div>
65-
<!-- async component end -->
66-
<!-- keepalive -->
67-
<div class="render-vapor-component">
68-
<button class="btn-show" @click="show = !show">show</button>
69-
<button class="btn-toggle" @click="toggle = !toggle">toggle</button>
70-
<div>
71-
<KeepAlive v-if="show">
72-
<SimpleVaporComp v-if="toggle" />
73-
</KeepAlive>
74-
</div>
75-
</div>
76-
<!-- keepalive end -->
7735
<!-- transition interop -->
7836
<div>
7937
<div class="trans-vapor">

packages-private/vapor-e2e-test/interop/components/SimpleVaporComp.vue

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages-private/vapor-e2e-test/interop/components/VdomFoo.vue

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/runtime-vapor/__tests__/vdomInterop.spec.ts

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import {
2+
KeepAlive,
23
createVNode,
34
defineComponent,
45
h,
56
nextTick,
7+
onActivated,
8+
onBeforeMount,
9+
onDeactivated,
10+
onMounted,
11+
onUnmounted,
612
ref,
713
renderSlot,
814
toDisplayString,
@@ -14,6 +20,7 @@ import {
1420
applyVShow,
1521
child,
1622
createComponent,
23+
defineVaporAsyncComponent,
1724
defineVaporComponent,
1825
renderEffect,
1926
setText,
@@ -275,4 +282,140 @@ describe('vdomInterop', () => {
275282
expect(fn).toHaveBeenCalledTimes(1)
276283
})
277284
})
285+
286+
describe('async component', () => {
287+
const duration = 5
288+
test('render vapor async component', async () => {
289+
const VdomChild = {
290+
setup() {
291+
return () => h('div', 'foo')
292+
},
293+
}
294+
const VaporAsyncChild = defineVaporAsyncComponent({
295+
loader: () => {
296+
return new Promise(r => {
297+
setTimeout(() => {
298+
r(VdomChild as any)
299+
}, duration)
300+
})
301+
},
302+
loadingComponent: () => h('span', 'loading...'),
303+
})
304+
305+
const { html } = define({
306+
setup() {
307+
return () => h(VaporAsyncChild as any)
308+
},
309+
}).render()
310+
311+
expect(html()).toBe('<span>loading...</span><!--async component-->')
312+
313+
await new Promise(r => setTimeout(r, duration))
314+
await nextTick()
315+
expect(html()).toBe('<div>foo</div><!--async component-->')
316+
})
317+
})
318+
319+
describe('keepalive', () => {
320+
function assertHookCalls(
321+
hooks: {
322+
beforeMount: any
323+
mounted: any
324+
activated: any
325+
deactivated: any
326+
unmounted: any
327+
},
328+
callCounts: number[],
329+
) {
330+
expect([
331+
hooks.beforeMount.mock.calls.length,
332+
hooks.mounted.mock.calls.length,
333+
hooks.activated.mock.calls.length,
334+
hooks.deactivated.mock.calls.length,
335+
hooks.unmounted.mock.calls.length,
336+
]).toEqual(callCounts)
337+
}
338+
339+
let hooks: any
340+
beforeEach(() => {
341+
hooks = {
342+
beforeMount: vi.fn(),
343+
mounted: vi.fn(),
344+
activated: vi.fn(),
345+
deactivated: vi.fn(),
346+
unmounted: vi.fn(),
347+
}
348+
})
349+
350+
test('render vapor component', async () => {
351+
const VaporChild = defineVaporComponent({
352+
setup() {
353+
const msg = ref('vapor')
354+
onBeforeMount(() => hooks.beforeMount())
355+
onMounted(() => hooks.mounted())
356+
onActivated(() => hooks.activated())
357+
onDeactivated(() => hooks.deactivated())
358+
onUnmounted(() => hooks.unmounted())
359+
360+
const n0 = template('<input type="text">', true)() as any
361+
applyTextModel(
362+
n0,
363+
() => msg.value,
364+
_value => (msg.value = _value),
365+
)
366+
return n0
367+
},
368+
})
369+
370+
const show = ref(true)
371+
const toggle = ref(true)
372+
const { html, host } = define({
373+
setup() {
374+
return () =>
375+
show.value
376+
? h(KeepAlive, null, {
377+
default: () => (toggle.value ? h(VaporChild as any) : null),
378+
})
379+
: null
380+
},
381+
}).render()
382+
383+
expect(html()).toBe('<input type="text">')
384+
let inputEl = host.firstChild as HTMLInputElement
385+
expect(inputEl.value).toBe('vapor')
386+
assertHookCalls(hooks, [1, 1, 1, 0, 0])
387+
388+
// change input value
389+
inputEl.value = 'changed'
390+
inputEl.dispatchEvent(new Event('input'))
391+
await nextTick()
392+
393+
// deactivate
394+
toggle.value = false
395+
await nextTick()
396+
expect(html()).toBe('<!---->')
397+
assertHookCalls(hooks, [1, 1, 1, 1, 0])
398+
399+
// activate
400+
toggle.value = true
401+
await nextTick()
402+
expect(html()).toBe('<input type="text">')
403+
inputEl = host.firstChild as HTMLInputElement
404+
expect(inputEl.value).toBe('changed')
405+
assertHookCalls(hooks, [1, 1, 2, 1, 0])
406+
407+
// unmount keepalive
408+
show.value = false
409+
await nextTick()
410+
expect(html()).toBe('<!---->')
411+
assertHookCalls(hooks, [1, 1, 2, 2, 1])
412+
413+
// mount keepalive
414+
show.value = true
415+
await nextTick()
416+
inputEl = host.firstChild as HTMLInputElement
417+
expect(inputEl.value).toBe('vapor')
418+
assertHookCalls(hooks, [2, 2, 3, 2, 1])
419+
})
420+
})
278421
})

0 commit comments

Comments
 (0)