]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test(ssr): test async components
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 27 Sep 2020 11:37:24 +0000 (13:37 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 27 Sep 2020 11:37:24 +0000 (13:37 +0200)
__tests__/ssr.spec.ts

index d4b9f243ebdb36b435a9115e04ceee9c03c8d97f..660c1ae0234f5c3e8523cf9534b9efdda5d69772 100644 (file)
@@ -9,26 +9,68 @@ import {
   ssrRenderComponent,
 } from '@vue/server-renderer'
 
+const delay = (t: number) => new Promise(resolve => setTimeout(resolve, t))
+
 describe('SSR', () => {
+  const Home = {
+    ssrRender(ctx: any, push: any) {
+      push('Home')
+    },
+  }
+  const Page = {
+    ssrRender(ctx: any, push: any) {
+      push(`${ssrInterpolate(ctx.$route.fullPath)}`)
+    },
+  }
+
+  const AsyncPage = async () => {
+    await delay(10)
+    return Page
+  }
+
   it('works', async () => {
-    const Home = {
-      ssrRender(ctx: any, push: any) {
-        push('Home')
-      },
-    }
-    const Page = {
-      ssrRender(ctx: any, push: any) {
-        push(`${ssrInterpolate(ctx.$route.fullPath)}`)
+    const router = createRouter({
+      history: createMemoryHistory(),
+      routes: [
+        { path: '/', component: Home },
+        {
+          path: '/:id',
+          component: Page,
+        },
+      ],
+    })
+    const App = {
+      ssrRender(ctx: any, push: any, parent: any) {
+        push(
+          ssrRenderComponent(
+            resolveComponent('router-view') as Component,
+            null,
+            null,
+            parent
+          )
+        )
       },
     }
+    const app = createSSRApp(App)
+    app.use(router)
+    // const rootEl = document.createElement('div')
+    // document.body.appendChild(rootEl)
 
+    router.push('/hello')
+    await router.isReady()
+
+    const xxx = await renderToString(app)
+    expect(xxx).toMatchInlineSnapshot(`"/hello"`)
+  })
+
+  it('handles async components', async () => {
     const router = createRouter({
       history: createMemoryHistory(),
       routes: [
         { path: '/', component: Home },
         {
           path: '/:id',
-          component: Page,
+          component: AsyncPage,
         },
       ],
     })