|
1 |
| -import { defineComponent, ref, nextTick, ComponentOptionsWithoutProps } from 'vue' |
| 1 | +import { h, defineComponent, ref, nextTick, ComponentOptionsWithoutProps, createSSRApp } from 'vue' |
2 | 2 |
|
3 | 3 | import { render } from '../../test-utils/vue-testing-library'
|
| 4 | +import { renderToString } from 'vue/server-renderer' |
4 | 5 | import { Portal, PortalGroup } from './portal'
|
5 | 6 | import { click } from '../../test-utils/interactions'
|
6 | 7 | import { html } from '../../test-utils/html'
|
@@ -38,6 +39,80 @@ function renderTemplate(input: string | ComponentOptionsWithoutProps) {
|
38 | 39 | )
|
39 | 40 | }
|
40 | 41 |
|
| 42 | +async function ssrRenderTemplate(input: string | ComponentOptionsWithoutProps) { |
| 43 | + let defaultComponents = { Portal, PortalGroup } |
| 44 | + |
| 45 | + if (typeof input === 'string') { |
| 46 | + let app = createSSRApp({ |
| 47 | + render: () => h(defineComponent({ template: input, components: defaultComponents })), |
| 48 | + }) |
| 49 | + |
| 50 | + return await renderToString(app) |
| 51 | + } |
| 52 | + |
| 53 | + let app = createSSRApp({ |
| 54 | + render: () => |
| 55 | + h( |
| 56 | + defineComponent( |
| 57 | + Object.assign({}, input, { |
| 58 | + components: { ...defaultComponents, ...input.components }, |
| 59 | + }) as Parameters<typeof defineComponent>[0] |
| 60 | + ) |
| 61 | + ), |
| 62 | + }) |
| 63 | + |
| 64 | + return await renderToString(app) |
| 65 | +} |
| 66 | + |
| 67 | +async function withoutBrowserGlobals<T>(fn: () => Promise<T>) { |
| 68 | + let oldWindow = globalThis.window |
| 69 | + let oldDocument = globalThis.document |
| 70 | + |
| 71 | + Object.defineProperty(globalThis, '_document', { |
| 72 | + value: undefined, |
| 73 | + configurable: true, |
| 74 | + }) |
| 75 | + |
| 76 | + Object.defineProperty(globalThis, '_globalProxy', { |
| 77 | + value: undefined, |
| 78 | + configurable: true, |
| 79 | + }) |
| 80 | + |
| 81 | + try { |
| 82 | + return await fn() |
| 83 | + } finally { |
| 84 | + Object.defineProperty(globalThis, '_globalProxy', { |
| 85 | + value: oldWindow, |
| 86 | + configurable: true, |
| 87 | + }) |
| 88 | + |
| 89 | + Object.defineProperty(globalThis, '_document', { |
| 90 | + value: oldDocument, |
| 91 | + configurable: true, |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +it('SSR-rendering a Portal should not error', async () => { |
| 97 | + expect(getPortalRoot()).toBe(null) |
| 98 | + |
| 99 | + let result = await withoutBrowserGlobals(() => |
| 100 | + ssrRenderTemplate( |
| 101 | + html` |
| 102 | + <main id="parent"> |
| 103 | + <Portal> |
| 104 | + <p id="content">Contents...</p> |
| 105 | + </Portal> |
| 106 | + </main> |
| 107 | + ` |
| 108 | + ) |
| 109 | + ) |
| 110 | + |
| 111 | + expect(getPortalRoot()).toBe(null) |
| 112 | + |
| 113 | + expect(result).toBe(html`<main id="parent"><!----></main>`) |
| 114 | +}) |
| 115 | + |
41 | 116 | it('should be possible to use a Portal', () => {
|
42 | 117 | expect(getPortalRoot()).toBe(null)
|
43 | 118 |
|
|
0 commit comments