Skip to content

Commit 68fe37a

Browse files
authored
test(runtime-vapor): add vdomInterop tests (#13563)
1 parent bb4ae25 commit 68fe37a

File tree

3 files changed

+114
-49
lines changed

3 files changed

+114
-49
lines changed

packages/runtime-vapor/__tests__/_utils.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createVaporApp } from '../src'
2-
import type { App } from '@vue/runtime-dom'
1+
import { createVaporApp, vaporInteropPlugin } from '../src'
2+
import { type App, type Component, createApp } from '@vue/runtime-dom'
33
import type { VaporComponent, VaporComponentInstance } from '../src/component'
44
import type { RawProps } from '../src/componentProps'
55

@@ -82,3 +82,56 @@ export function makeRender<C = VaporComponent>(
8282

8383
return define
8484
}
85+
86+
export interface InteropRenderContext {
87+
mount: (container?: string | ParentNode) => InteropRenderContext
88+
render: (
89+
props?: RawProps,
90+
container?: string | ParentNode,
91+
) => InteropRenderContext
92+
host: HTMLElement
93+
html: () => string
94+
}
95+
96+
export function makeInteropRender(): (comp: Component) => InteropRenderContext {
97+
let host: HTMLElement
98+
beforeEach(() => {
99+
host = document.createElement('div')
100+
})
101+
afterEach(() => {
102+
host.remove()
103+
})
104+
105+
function define(comp: Component) {
106+
let app: App
107+
function render(
108+
props: RawProps | undefined = undefined,
109+
container: string | ParentNode = host,
110+
) {
111+
app?.unmount()
112+
app = createApp(comp, props)
113+
app.use(vaporInteropPlugin)
114+
return mount(container)
115+
}
116+
117+
function mount(container: string | ParentNode = host) {
118+
app.mount(container)
119+
return res()
120+
}
121+
122+
function html() {
123+
return host.innerHTML
124+
}
125+
126+
const res = () => ({
127+
host,
128+
mount,
129+
render,
130+
html,
131+
})
132+
133+
return res()
134+
}
135+
136+
return define
137+
}

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

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
type Ref,
3-
createApp,
4-
defineComponent,
5-
h,
6-
nextTick,
7-
ref,
8-
} from '@vue/runtime-dom'
1+
import { type Ref, nextTick, ref } from '@vue/runtime-dom'
92
import {
103
createComponent,
114
createDynamicComponent,
@@ -17,7 +10,6 @@ import {
1710
setProp,
1811
setStyle,
1912
template,
20-
vaporInteropPlugin,
2113
} from '../src'
2214
import { makeRender } from './_utils'
2315
import { stringifyStyle } from '@vue/shared'
@@ -407,42 +399,4 @@ describe('attribute fallthrough', () => {
407399
const el = host.children[0]
408400
expect(el.classList.length).toBe(0)
409401
})
410-
411-
it('should not fallthrough emit handlers to vdom child', () => {
412-
const VDomChild = defineComponent({
413-
emits: ['click'],
414-
setup(_, { emit }) {
415-
return () => h('button', { onClick: () => emit('click') }, 'click me')
416-
},
417-
})
418-
419-
const fn = vi.fn()
420-
const VaporChild = defineVaporComponent({
421-
emits: ['click'],
422-
setup() {
423-
return createComponent(
424-
VDomChild as any,
425-
{ onClick: () => fn },
426-
null,
427-
true,
428-
)
429-
},
430-
})
431-
432-
const App = {
433-
setup() {
434-
return () => h(VaporChild as any)
435-
},
436-
}
437-
438-
const root = document.createElement('div')
439-
createApp(App).use(vaporInteropPlugin).mount(root)
440-
441-
expect(root.innerHTML).toBe('<button>click me</button>')
442-
const button = root.querySelector('button')!
443-
button.dispatchEvent(new Event('click'))
444-
445-
// fn should be called once
446-
expect(fn).toHaveBeenCalledTimes(1)
447-
})
448402
})
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { defineComponent, h } from '@vue/runtime-dom'
2+
import { makeInteropRender } from './_utils'
3+
import { createComponent, defineVaporComponent } from '../src'
4+
5+
const define = makeInteropRender()
6+
7+
describe('vdomInterop', () => {
8+
describe.todo('props', () => {})
9+
10+
describe.todo('emit', () => {})
11+
12+
describe.todo('slots', () => {})
13+
14+
describe.todo('provide', () => {})
15+
16+
describe.todo('inject', () => {})
17+
18+
describe.todo('template ref', () => {})
19+
20+
describe.todo('dynamic component', () => {})
21+
22+
describe('attribute fallthrough', () => {
23+
it('should not fallthrough emit handlers to vdom child', () => {
24+
const VDomChild = defineComponent({
25+
emits: ['click'],
26+
setup(_, { emit }) {
27+
return () => h('button', { onClick: () => emit('click') }, 'click me')
28+
},
29+
})
30+
31+
const fn = vi.fn()
32+
const VaporChild = defineVaporComponent({
33+
emits: ['click'],
34+
setup() {
35+
return createComponent(
36+
VDomChild as any,
37+
{ onClick: () => fn },
38+
null,
39+
true,
40+
)
41+
},
42+
})
43+
44+
const { host, html } = define({
45+
setup() {
46+
return () => h(VaporChild as any)
47+
},
48+
}).render()
49+
50+
expect(html()).toBe('<button>click me</button>')
51+
const button = host.querySelector('button')!
52+
button.dispatchEvent(new Event('click'))
53+
54+
// fn should be called once
55+
expect(fn).toHaveBeenCalledTimes(1)
56+
})
57+
})
58+
})

0 commit comments

Comments
 (0)