From 2271d549606d9d1dc141b6334778707e3e1391eb Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Mon, 8 Apr 2024 18:32:45 +0200 Subject: [PATCH] add test --- examples/full/.test.ts | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/examples/full/.test.ts b/examples/full/.test.ts index caa77a97..4d64a774 100644 --- a/examples/full/.test.ts +++ b/examples/full/.test.ts @@ -5,10 +5,11 @@ runTest() function runTest() { run('pnpm run dev') + const textLandingPage = 'Rendered to HTML.' testUrl({ url: '/', title: 'My Vike + React App', - text: 'My Vike + React app', + text: textLandingPage, counter: true }) @@ -24,10 +25,11 @@ function runTest() { text: '1983-05-25' }) + const textNoSSR = 'This page is rendered only in the browser' { const url = '/without-ssr' const title = 'My Vike + React App' - const text = "It isn't rendered to HTML." + const text = textNoSSR test(url + ' (HTML)', async () => { const html = await fetchHtml(url) // Isn't rendered to HTML @@ -41,6 +43,24 @@ function runTest() { const body = await page.textContent('body') expect(body).toContain(text) }) + test('Switch between SSR and non-SSR page', async () => { + let body: string | null + const t1 = textNoSSR + const t2 = textLandingPage + body = await page.textContent('body') + expect(body).toContain(t1) + expect(body).not.toContain(t2) + await page.click('a:has-text("Welcome")') + await testCounter() + body = await page.textContent('body') + expect(body).toContain(t2) + expect(body).not.toContain(t1) + await page.click('a:has-text("Without SSR")') + await testCounter() + body = await page.textContent('body') + expect(body).toContain(t1) + expect(body).not.toContain(t2) + }) } } @@ -55,7 +75,6 @@ function testUrl({ url, title, text, counter }: { url: string; title: string; te const body = await page.textContent('body') expect(body).toContain(text) if (counter) { - expect(await page.textContent('button')).toBe('Counter 0') await testCounter() } }) @@ -68,8 +87,12 @@ function getTitle(html: string) { async function testCounter() { // autoRetry() for awaiting client-side code loading & executing - await autoRetry(async () => { - await page.click('button') - expect(await page.textContent('button')).toContain('Counter 1') - }) + await autoRetry( + async () => { + expect(await page.textContent('button')).toBe('Counter 0') + await page.click('button') + expect(await page.textContent('button')).toContain('Counter 1') + }, + { timeout: 5 * 1000 } + ) }