Skip to content

Commit 3278078

Browse files
committed
wip: e2e tests
1 parent c9de3fb commit 3278078

File tree

10 files changed

+154
-9
lines changed

10 files changed

+154
-9
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import path from 'node:path'
2+
import {
3+
E2E_TIMEOUT,
4+
setupPuppeteer,
5+
} from '../../../packages/vue/__tests__/e2e/e2eUtils'
6+
import connect from 'connect'
7+
import sirv from 'sirv'
8+
const { page, html, click, value, enterValue } = setupPuppeteer()
9+
10+
describe('vapor keepalive', () => {
11+
let server: any
12+
const port = '8196'
13+
beforeAll(() => {
14+
server = connect()
15+
.use(sirv(path.resolve(import.meta.dirname, '../dist')))
16+
.listen(port)
17+
process.on('SIGTERM', () => server && server.close())
18+
})
19+
20+
beforeEach(async () => {
21+
const baseUrl = `http://localhost:${port}/keepalive/`
22+
await page().goto(baseUrl)
23+
await page().waitForSelector('#app')
24+
})
25+
26+
afterAll(() => {
27+
server.close()
28+
})
29+
30+
test(
31+
'render vdom component',
32+
async () => {
33+
const testSelector = '.render-vdom-component'
34+
const btnShow = `${testSelector} .btn-show`
35+
const btnToggle = `${testSelector} .btn-toggle`
36+
const container = `${testSelector} > div`
37+
const inputSelector = `${testSelector} input`
38+
39+
let calls = await page().evaluate(() => {
40+
return (window as any).getCalls()
41+
})
42+
expect(calls).toStrictEqual(['mounted', 'activated'])
43+
44+
expect(await html(container)).toBe('<input type="text">')
45+
expect(await value(inputSelector)).toBe('vdom')
46+
47+
// change input value
48+
await enterValue(inputSelector, 'changed')
49+
expect(await value(inputSelector)).toBe('changed')
50+
51+
// deactivate
52+
await click(btnToggle)
53+
expect(await html(container)).toBe('')
54+
calls = await page().evaluate(() => {
55+
return (window as any).getCalls()
56+
})
57+
expect(calls).toStrictEqual(['deactivated'])
58+
59+
// activate
60+
await click(btnToggle)
61+
expect(await html(container)).toBe('<input type="text">')
62+
expect(await value(inputSelector)).toBe('changed')
63+
calls = await page().evaluate(() => {
64+
return (window as any).getCalls()
65+
})
66+
expect(calls).toStrictEqual(['activated'])
67+
68+
// unmount keepalive
69+
await click(btnShow)
70+
expect(await html(container)).toBe('')
71+
calls = await page().evaluate(() => {
72+
return (window as any).getCalls()
73+
})
74+
expect(calls).toStrictEqual(['deactivated', 'unmounted'])
75+
76+
// mount keepalive
77+
await click(btnShow)
78+
expect(await html(container)).toBe('<input type="text">')
79+
expect(await value(inputSelector)).toBe('vdom')
80+
calls = await page().evaluate(() => {
81+
return (window as any).getCalls()
82+
})
83+
expect(calls).toStrictEqual(['mounted', 'activated'])
84+
},
85+
E2E_TIMEOUT,
86+
)
87+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<a href="/interop/">VDOM / Vapor interop</a>
22
<a href="/todomvc/">Vapor TodoMVC</a>
3+
<a href="/keepalive/">Vapor KeepAlive</a>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<script vapor>
2+
import { ref } from 'vue'
3+
import VdomComp from './components/VdomComp.vue';
4+
5+
window.calls = []
6+
window.getCalls = () => {
7+
const ret = window.calls.slice()
8+
window.calls = []
9+
return ret
10+
}
11+
12+
const show = ref(true)
13+
const toggle = ref(true)
14+
</script>
15+
16+
<template>
17+
<div class="render-vdom-component">
18+
<button class="btn-show" @click="show = !show">show</button>
19+
<button class="btn-toggle" @click="toggle = !toggle">toggle</button>
20+
<div>
21+
<KeepAlive v-if="show">
22+
<VdomComp v-if="toggle"></VdomComp>
23+
</KeepAlive>
24+
</div>
25+
</div>
26+
</template>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script setup>
2+
import { onActivated, onDeactivated, onMounted, onUnmounted, ref } from 'vue'
3+
const msg = ref('vdom')
4+
5+
onMounted(() => {
6+
window.calls.push('mounted')
7+
})
8+
onActivated(() => {
9+
window.calls.push('activated')
10+
})
11+
onDeactivated(() => {
12+
window.calls.push('deactivated')
13+
})
14+
onUnmounted(() => {
15+
window.calls.push('unmounted')
16+
})
17+
</script>
18+
<template>
19+
<input type="text" v-model="msg" />
20+
</template>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<script type="module" src="./main.ts"></script>
2+
<div id="app"></div>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createVaporApp, vaporInteropPlugin } from 'vue'
2+
import App from './App.vue'
3+
4+
createVaporApp(App).use(vaporInteropPlugin).mount('#app')

packages-private/vapor-e2e-test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite dev",
8-
"build": "vite build"
8+
"build": "vite build && vite preview"
99
},
1010
"devDependencies": {
1111
"@types/connect": "^3.4.38",

packages-private/vapor-e2e-test/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default defineConfig({
1414
input: {
1515
interop: resolve(import.meta.dirname, 'interop/index.html'),
1616
todomvc: resolve(import.meta.dirname, 'todomvc/index.html'),
17+
keepalive: resolve(import.meta.dirname, 'keepalive/index.html'),
1718
},
1819
},
1920
},

packages/runtime-vapor/src/component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ export function createComponent(
174174
)
175175
// TODO: problem is `frag.insert` will be called multiple times
176176
// if used in v-if
177-
if (!isHydrating && _insertionParent) {
178-
insert(frag, _insertionParent, _insertionAnchor)
179-
}
177+
// if (!isHydrating && _insertionParent) {
178+
// insert(frag, _insertionParent, _insertionAnchor)
179+
// }
180180
return frag
181181
}
182182

packages/runtime-vapor/src/components/KeepAlive.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
6666
const storageContainer = createElement('div')
6767
let current: VaporComponentInstance | VaporFragment | undefined
6868

69-
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
70-
;(keepAliveInstance as any).__v_cache = cache
71-
}
69+
// if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
70+
// ;(keepAliveInstance as any).__v_cache = cache
71+
// }
7272

7373
function shouldCache(instance: VaporComponentInstance) {
7474
const { include, exclude } = props
@@ -120,12 +120,16 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
120120
if (current) {
121121
const innerComp = getInnerComponent(current)!
122122
if (innerComp.type === cached.type) {
123-
const da = cached.da
123+
const instance = cached.vapor
124+
? cached
125+
: // vdom interop
126+
(cached as any).component
127+
const da = instance.da
124128
da && queuePostFlushCb(da)
125129
return
126130
}
127131
}
128-
remove(cached, storageContainer)
132+
remove(item, storageContainer)
129133
})
130134
})
131135

0 commit comments

Comments
 (0)