forked from vuejs/test-utils
/
teleport.spec.ts
146 lines (120 loc) · 3.72 KB
/
teleport.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { defineComponent, h, Teleport } from 'vue'
import { mount } from '../../src'
import WithTeleportPropsComp from '../components/WithTeleportPropsComp.vue'
import WithTeleportEmitsComp from '../components/WithTeleportEmitsComp.vue'
import WithProps from '../components/WithProps.vue'
import EmitsEvent from '../components/EmitsEvent.vue'
describe('teleport', () => {
beforeEach(() => {
document.body.outerHTML = ''
})
it('teleports a string', () => {
const destination = document.createElement('div')
destination.id = 'far-away'
document.body.appendChild(destination)
const Comp = defineComponent({
setup() {
return () =>
h(() => h(Teleport, { to: '#far-away' }, h('div', 'teleported')))
}
})
mount(Comp)
expect(document.body.outerHTML).toBe(
`<body><div id="far-away"><div>teleported</div></div></body>`
)
})
const Greeter = defineComponent({
emits: {
greet: (msg: string) => {
return true
}
},
props: {
msg: {
type: String,
required: true
}
},
setup({ msg }, { emit }) {
return () =>
h(
'button',
{ onClick: () => emit('greet', `${msg.toUpperCase()}!!!`) },
`${msg}!!!`
)
}
})
it('teleports a component with props', async () => {
const destination = document.createElement('div')
destination.id = 'far-away'
document.body.appendChild(destination)
const onGreet = vi.fn()
const Comp = defineComponent({
setup() {
return () =>
h(() =>
h(
Teleport,
{ to: '#far-away' },
h(Greeter, { onGreet, msg: 'Hello' })
)
)
}
})
mount(Comp)
document.querySelector<HTMLButtonElement>('button')!.click()
expect(onGreet).toHaveBeenCalledWith('HELLO!!!')
expect(document.body.outerHTML).toBe(
`<body><div id="far-away"><button>Hello!!!</button></div></body>`
)
})
it('teleports a component with props', async () => {
const destination = document.createElement('div')
destination.id = 'far-away'
document.body.appendChild(destination)
const onGreet = vi.fn()
const Comp = defineComponent({
setup() {
return () =>
h(() =>
h(
Teleport,
{ to: '#far-away' },
h(Greeter, { onGreet, msg: 'Hello' })
)
)
}
})
const wrapper = mount(Comp)
// although <Greeter /> is teleported outside the component,
// it's still in the vdom of the wrapper,
// so you can just query it like you normally would - pretty neat.
await wrapper.getComponent(Greeter).trigger('click')
expect(onGreet).toHaveBeenCalledWith('HELLO!!!')
expect(wrapper.getComponent(Greeter).props()).toEqual({
msg: 'Hello'
})
expect(document.body.outerHTML).toBe(
`<body><div id="far-away"><button>Hello!!!</button></div></body>`
)
})
it('works with SFC and gets props', async () => {
const destination = document.createElement('div')
destination.id = 'somewhere'
document.body.appendChild(destination)
const wrapper = mount(WithTeleportPropsComp)
const withProps = wrapper.getComponent(WithProps)
expect(withProps.props()).toEqual({
msg: 'hi there'
})
})
it('works with SFC and captures emitted events', async () => {
const destination = document.createElement('div')
destination.id = 'somewhere'
document.body.appendChild(destination)
const wrapper = mount(WithTeleportEmitsComp)
const withProps = wrapper.getComponent(EmitsEvent)
withProps.trigger('click')
expect(withProps.emitted().greet[0]).toEqual(['Hey!'])
})
})