Skip to content

Commit

Permalink
improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Apr 8, 2024
1 parent 2271d54 commit 26e2f47
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion examples/full/.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect, run, fetchHtml, page, getServerUrl, autoRetry } from '@brillout/test-e2e'
import { test, expect, run, fetchHtml, page, getServerUrl, autoRetry, partRegex } from '@brillout/test-e2e'

runTest()

Expand Down Expand Up @@ -47,19 +47,25 @@ function runTest() {
let body: string | null
const t1 = textNoSSR
const t2 = textLandingPage

body = await page.textContent('body')
expect(body).toContain(t1)
expect(body).not.toContain(t2)
ensureWasClientSideRouted('/pages/without-ssr')

await page.click('a:has-text("Welcome")')
await testCounter()
body = await page.textContent('body')
expect(body).toContain(t2)
expect(body).not.toContain(t1)
ensureWasClientSideRouted('/pages/without-ssr')

await page.click('a:has-text("Without SSR")')
await testCounter()
body = await page.textContent('body')
expect(body).toContain(t1)
expect(body).not.toContain(t2)
ensureWasClientSideRouted('/pages/without-ssr')
})
}
}
Expand Down Expand Up @@ -96,3 +102,28 @@ async function testCounter() {
{ timeout: 5 * 1000 }
)
}

/** Ensure page wasn't server-side routed.
*
* Examples:
* await ensureWasClientSideRouted('/pages/index')
* await ensureWasClientSideRouted('/pages/about')
*/
async function ensureWasClientSideRouted(pageIdFirst: `/pages/${string}`) {
// Check whether the HTML is from the first page before Client-side Routing.
// page.content() doesn't return the original HTML (it dumps the DOM to HTML).
// Therefore only the serialized pageContext tell us the original HTML.
const html = await page.content()
const pageId = findFirstPageId(html)
expect(pageId).toBe(pageIdFirst)
}
function findFirstPageId(html: string) {
expect(html).toContain('<script id="vike_pageContext" type="application/json">')
expect(html).toContain('_pageId')
expect(html.split('_pageId').length).toBe(2)
const match = partRegex`"_pageId":"${/([^"]+)/}"`.exec(html)
expect(match).toBeTruthy()
const pageId = match![1]
expect(pageId).toBeTruthy()
return pageId
}

0 comments on commit 26e2f47

Please sign in to comment.