Skip to content

Commit ed6face

Browse files
committed
refactor(tests): port keepalive e2e tests to unit test
1 parent 6d8f944 commit ed6face

File tree

8 files changed

+109
-142
lines changed

8 files changed

+109
-142
lines changed

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

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<a href="/interop/">VDOM / Vapor interop</a>
22
<a href="/todomvc/">Vapor TodoMVC</a>
3-
<a href="/keepalive/">Vapor KeepAlive</a>
43
<a href="/transition/">Vapor Transition</a>
54
<a href="/transition-group/">Vapor TransitionGroup</a>
65

@@ -9,4 +8,4 @@
98
display: block;
109
margin: 10px;
1110
}
12-
</style>
11+
</style>

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

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

packages-private/vapor-e2e-test/keepalive/components/VdomComp.vue

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

packages-private/vapor-e2e-test/keepalive/index.html

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

packages-private/vapor-e2e-test/keepalive/main.ts

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ 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'),
1817
transition: resolve(import.meta.dirname, 'transition/index.html'),
1918
transitionGroup: resolve(
2019
import.meta.dirname,

packages/runtime-vapor/__tests__/components/KeepAlive.spec.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
h,
23
nextTick,
34
onActivated,
45
onBeforeMount,
@@ -8,6 +9,8 @@ import {
89
reactive,
910
ref,
1011
shallowRef,
12+
vModelText,
13+
withDirectives,
1114
} from 'vue'
1215
import type { LooseRawProps, VaporComponent } from '../../src/component'
1316
import { makeRender } from '../_utils'
@@ -18,10 +21,12 @@ import {
1821
createDynamicComponent,
1922
createIf,
2023
createTemplateRefSetter,
24+
createVaporApp,
2125
defineVaporComponent,
2226
renderEffect,
2327
setText,
2428
template,
29+
vaporInteropPlugin,
2530
} from '../../src'
2631

2732
const define = makeRender()
@@ -1186,4 +1191,107 @@ describe('VaporKeepAlive', () => {
11861191
expect(deactivatedHome).toHaveBeenCalledTimes(0)
11871192
expect(unmountedHome).toHaveBeenCalledTimes(1)
11881193
})
1194+
1195+
describe('vdom interop', () => {
1196+
test('render vdom component', async () => {
1197+
const VdomComp = {
1198+
setup() {
1199+
const msg = ref('vdom')
1200+
onBeforeMount(() => oneHooks.beforeMount())
1201+
onMounted(() => oneHooks.mounted())
1202+
onActivated(() => oneHooks.activated())
1203+
onDeactivated(() => oneHooks.deactivated())
1204+
onUnmounted(() => oneHooks.unmounted())
1205+
return () => {
1206+
return withDirectives(
1207+
h(
1208+
'input',
1209+
{
1210+
type: 'text',
1211+
'onUpdate:modelValue': ($event: any) => (msg.value = $event),
1212+
},
1213+
[],
1214+
),
1215+
[[vModelText, msg.value]],
1216+
)
1217+
}
1218+
},
1219+
}
1220+
1221+
const show = ref(true)
1222+
const toggle = ref(true)
1223+
1224+
const App = defineVaporComponent({
1225+
setup() {
1226+
const n0 = createIf(
1227+
() => show.value,
1228+
() => {
1229+
const n5 = createComponent(
1230+
VaporKeepAlive,
1231+
null,
1232+
{
1233+
default: () => {
1234+
const n2 = createIf(
1235+
() => toggle.value,
1236+
() => {
1237+
const n4 = createComponent(VdomComp)
1238+
return n4
1239+
},
1240+
)
1241+
return n2
1242+
},
1243+
},
1244+
true,
1245+
)
1246+
return n5
1247+
},
1248+
)
1249+
return n0
1250+
},
1251+
})
1252+
1253+
const container = document.createElement('div')
1254+
document.body.appendChild(container)
1255+
const app = createVaporApp(App)
1256+
app.use(vaporInteropPlugin)
1257+
app.mount(container)
1258+
1259+
expect(container.innerHTML).toBe(`<input type="text"><!--if--><!--if-->`)
1260+
assertHookCalls(oneHooks, [1, 1, 1, 0, 0])
1261+
1262+
let inputEl = container.firstChild as HTMLInputElement
1263+
expect(inputEl.value).toBe('vdom')
1264+
1265+
inputEl.value = 'changed'
1266+
inputEl.dispatchEvent(new Event('input'))
1267+
await nextTick()
1268+
1269+
// deactivate
1270+
toggle.value = false
1271+
await nextTick()
1272+
expect(container.innerHTML).toBe(`<!--if--><!--if-->`)
1273+
assertHookCalls(oneHooks, [1, 1, 1, 1, 0])
1274+
1275+
// activate
1276+
toggle.value = true
1277+
await nextTick()
1278+
expect(container.innerHTML).toBe(`<input type="text"><!--if--><!--if-->`)
1279+
assertHookCalls(oneHooks, [1, 1, 2, 1, 0])
1280+
expect(inputEl.value).toBe('changed')
1281+
1282+
// unmount keepalive
1283+
show.value = false
1284+
await nextTick()
1285+
expect(container.innerHTML).toBe(`<!--if-->`)
1286+
assertHookCalls(oneHooks, [1, 1, 2, 2, 1])
1287+
1288+
// mount keepalive
1289+
show.value = true
1290+
await nextTick()
1291+
expect(container.innerHTML).toBe(`<input type="text"><!--if--><!--if-->`)
1292+
assertHookCalls(oneHooks, [2, 2, 3, 2, 1])
1293+
inputEl = container.firstChild as HTMLInputElement
1294+
expect(inputEl.value).toBe('vdom')
1295+
})
1296+
})
11891297
})

0 commit comments

Comments
 (0)