/
portal.test.ts
408 lines (325 loc) · 10.1 KB
/
portal.test.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import { h, defineComponent, ref, nextTick, ComponentOptionsWithoutProps, createSSRApp } from 'vue'
import { render } from '../../test-utils/vue-testing-library'
import { renderToString } from 'vue/server-renderer'
import { Portal, PortalGroup } from './portal'
import { click } from '../../test-utils/interactions'
import { html } from '../../test-utils/html'
function getPortalRoot() {
return document.getElementById('headlessui-portal-root')!
}
beforeEach(() => {
document.body.innerHTML = ''
})
jest.mock('../../hooks/use-id')
beforeAll(() => {
jest.spyOn(window, 'requestAnimationFrame').mockImplementation(setImmediate as any)
jest.spyOn(window, 'cancelAnimationFrame').mockImplementation(clearImmediate as any)
})
afterAll(() => jest.restoreAllMocks())
function renderTemplate(input: string | ComponentOptionsWithoutProps) {
let defaultComponents = { Portal, PortalGroup }
if (typeof input === 'string') {
return render(defineComponent({ template: input, components: defaultComponents }))
}
return render(
defineComponent(
Object.assign({}, input, {
components: { ...defaultComponents, ...input.components },
}) as Parameters<typeof defineComponent>[0]
)
)
}
async function ssrRenderTemplate(input: string | ComponentOptionsWithoutProps) {
let defaultComponents = { Portal, PortalGroup }
if (typeof input === 'string') {
let app = createSSRApp({
render: () => h(defineComponent({ template: input, components: defaultComponents })),
})
return await renderToString(app)
}
let app = createSSRApp({
render: () =>
h(
defineComponent(
Object.assign({}, input, {
components: { ...defaultComponents, ...input.components },
}) as Parameters<typeof defineComponent>[0]
)
),
})
return await renderToString(app)
}
async function withoutBrowserGlobals<T>(fn: () => Promise<T>) {
let oldWindow = globalThis.window
let oldDocument = globalThis.document
Object.defineProperty(globalThis, '_document', {
value: undefined,
configurable: true,
})
Object.defineProperty(globalThis, '_globalProxy', {
value: undefined,
configurable: true,
})
try {
return await fn()
} finally {
Object.defineProperty(globalThis, '_globalProxy', {
value: oldWindow,
configurable: true,
})
Object.defineProperty(globalThis, '_document', {
value: oldDocument,
configurable: true,
})
}
}
it('SSR-rendering a Portal should not error', async () => {
expect(getPortalRoot()).toBe(null)
let result = await withoutBrowserGlobals(() =>
ssrRenderTemplate(
html`
<main id="parent">
<Portal>
<p id="content">Contents...</p>
</Portal>
</main>
`
)
)
expect(getPortalRoot()).toBe(null)
expect(result).toBe(html`<main id="parent"><!----></main>`)
})
it('should be possible to use a Portal', () => {
expect(getPortalRoot()).toBe(null)
renderTemplate(
html`
<main id="parent">
<Portal>
<p id="content">Contents...</p>
</Portal>
</main>
`
)
let parent = document.getElementById('parent')
let content = document.getElementById('content')
expect(getPortalRoot()).not.toBe(null)
// Ensure the content is not part of the parent
expect(parent).not.toContainElement(content)
// Ensure the content does exist
expect(content).not.toBe(null)
expect(content).toHaveTextContent('Contents...')
})
it('should be possible to use multiple Portal elements', () => {
expect(getPortalRoot()).toBe(null)
renderTemplate(
html`
<main id="parent">
<Portal>
<p id="content1">Contents 1 ...</p>
</Portal>
<hr />
<Portal>
<p id="content2">Contents 2 ...</p>
</Portal>
</main>
`
)
let parent = document.getElementById('parent')
let content1 = document.getElementById('content1')
let content2 = document.getElementById('content2')
expect(getPortalRoot()).not.toBe(null)
// Ensure the content1 is not part of the parent
expect(parent).not.toContainElement(content1)
// Ensure the content2 is not part of the parent
expect(parent).not.toContainElement(content2)
// Ensure the content does exist
expect(content1).not.toBe(null)
expect(content1).toHaveTextContent('Contents 1 ...')
// Ensure the content does exist
expect(content2).not.toBe(null)
expect(content2).toHaveTextContent('Contents 2 ...')
})
it('should cleanup the Portal root when the last Portal is unmounted', async () => {
expect(getPortalRoot()).toBe(null)
renderTemplate({
template: html`
<main id="parent">
<button id="a" @click="toggleA">Toggle A</button>
<button id="b" @click="toggleB">Toggle B</button>
<Portal v-if="renderA">
<p id="content1">Contents 1 ...</p>
</Portal>
<Portal v-if="renderB">
<p id="content2">Contents 2 ...</p>
</Portal>
</main>
`,
setup() {
let renderA = ref(false)
let renderB = ref(false)
return {
renderA,
renderB,
toggleA() {
renderA.value = !renderA.value
},
toggleB() {
renderB.value = !renderB.value
},
}
},
})
let a = document.getElementById('a')
let b = document.getElementById('b')
expect(getPortalRoot()).toBe(null)
// Let's render the first Portal
await click(a)
expect(getPortalRoot()).not.toBe(null)
expect(getPortalRoot().children).toHaveLength(1)
// Let's render the second Portal
await click(b)
expect(getPortalRoot()).not.toBe(null)
expect(getPortalRoot().children).toHaveLength(2)
// Let's remove the first portal
await click(a)
expect(getPortalRoot()).not.toBe(null)
expect(getPortalRoot().children).toHaveLength(1)
// Let's remove the second Portal
await click(b)
expect(getPortalRoot()).toBe(null)
// Let's render the first Portal again
await click(a)
expect(getPortalRoot()).not.toBe(null)
expect(getPortalRoot().children).toHaveLength(1)
})
it('should be possible to render multiple portals at the same time', async () => {
expect(getPortalRoot()).toBe(null)
renderTemplate({
template: html`
<main id="parent">
<button id="a" @click="toggleA">Toggle A</button>
<button id="b" @click="toggleB">Toggle B</button>
<button id="c" @click="toggleC">Toggle C</button>
<button id="double" @click="toggleAB">Toggle A & B</button>
<Portal v-if="renderA">
<p id="content1">Contents 1 ...</p>
</Portal>
<Portal v-if="renderB">
<p id="content2">Contents 2 ...</p>
</Portal>
<Portal v-if="renderC">
<p id="content3">Contents 3 ...</p>
</Portal>
</main>
`,
setup() {
let renderA = ref(true)
let renderB = ref(true)
let renderC = ref(true)
return {
renderA,
renderB,
renderC,
toggleA() {
renderA.value = !renderA.value
},
toggleB() {
renderB.value = !renderB.value
},
toggleC() {
renderC.value = !renderC.value
},
toggleAB() {
renderA.value = !renderA.value
renderB.value = !renderB.value
},
}
},
})
expect(getPortalRoot()).not.toBe(null)
expect(getPortalRoot().children).toHaveLength(3)
// Remove Portal 1
await click(document.getElementById('a'))
expect(getPortalRoot().children).toHaveLength(2)
// Remove Portal 2
await click(document.getElementById('b'))
expect(getPortalRoot().children).toHaveLength(1)
// Re-add Portal 1
await click(document.getElementById('a'))
expect(getPortalRoot().children).toHaveLength(2)
// Remove Portal 3
await click(document.getElementById('c'))
expect(getPortalRoot().children).toHaveLength(1)
// Remove Portal 1
await click(document.getElementById('a'))
expect(getPortalRoot()).toBe(null)
// Render A and B at the same time!
await click(document.getElementById('double'))
expect(getPortalRoot().children).toHaveLength(2)
})
it('should be possible to tamper with the modal root and restore correctly', async () => {
expect(getPortalRoot()).toBe(null)
renderTemplate({
template: html`
<main id="parent">
<button id="a" @click="toggleA">Toggle A</button>
<button id="b" @click="toggleB">Toggle B</button>
<Portal v-if="renderA">
<p id="content1">Contents 1 ...</p>
</Portal>
<Portal v-if="renderB">
<p id="content2">Contents 2 ...</p>
</Portal>
</main>
`,
setup() {
let renderA = ref(true)
let renderB = ref(true)
return {
renderA,
renderB,
toggleA() {
renderA.value = !renderA.value
},
toggleB() {
renderB.value = !renderB.value
},
}
},
})
expect(getPortalRoot()).not.toBe(null)
// Tamper tamper
document.body.removeChild(document.getElementById('headlessui-portal-root')!)
// Hide Portal 1 and 2
await click(document.getElementById('a'))
await click(document.getElementById('b'))
expect(getPortalRoot()).toBe(null)
// Re-show Portal 1 and 2
await click(document.getElementById('a'))
await click(document.getElementById('b'))
expect(getPortalRoot()).not.toBe(null)
expect(getPortalRoot().children).toHaveLength(2)
})
it('should be possible to force the Portal into a specific element using PortalGroup', async () => {
renderTemplate({
template: html`
<main>
<aside ref="container" id="group-1">A</aside>
<PortalGroup :target="container">
<section id="group-2">
<span>B</span>
</section>
<Portal>Next to A</Portal>
</PortalGroup>
</main>
`,
setup() {
let container = ref(null)
return { container }
},
})
await new Promise<void>(nextTick)
expect(document.body.innerHTML).toMatchInlineSnapshot(
`"<div><div><div data-v-app=\\"\\"><main><aside id=\\"group-1\\">A<div data-headlessui-portal=\\"\\">Next to A</div></aside><section id=\\"group-2\\"><span>B</span></section><!--teleport start--><!--teleport end--></main></div></div></div>"`
)
})