|
1 |
| -import { test, expect, run, fetchHtml, page, getServerUrl } from '@brillout/test-e2e' |
| 1 | +import { test, expect, run, fetchHtml, page, getServerUrl, autoRetry } from '@brillout/test-e2e' |
2 | 2 |
|
3 | 3 | runTest()
|
4 | 4 |
|
| 5 | +const fetchedText = 'A New Hope' |
| 6 | +const url = '/' |
| 7 | + |
5 | 8 | function runTest() {
|
6 | 9 | run('pnpm run dev')
|
7 | 10 |
|
8 |
| - const textLandingPage = 'A New Hope' |
9 |
| - const title = 'Star Wars Movies' |
10 |
| - testUrl({ |
11 |
| - url: '/', |
12 |
| - title, |
13 |
| - text: textLandingPage, |
14 |
| - }) |
15 |
| -} |
16 |
| - |
17 |
| -function testUrl({ url, title, text }: { url: string; title: string; text: string }) { |
18 |
| - test(url + ' (HTML)', async () => { |
| 11 | + test('HTML', async () => { |
19 | 12 | const html = await fetchHtml(url)
|
20 |
| - expect(html).toContain(text) |
21 |
| - expect(getTitle(html)).toBe(title) |
| 13 | + expect(html).toContain(fetchedText) |
22 | 14 | })
|
23 |
| - test(url + ' (Hydration)', async () => { |
| 15 | + |
| 16 | + test('Hydration', async () => { |
24 | 17 | await page.goto(getServerUrl() + url)
|
25 |
| - const body = await page.textContent('body') |
26 |
| - expect(body).toContain(text) |
| 18 | + await testDOM() |
27 | 19 | })
|
| 20 | + |
| 21 | + test('Navigation', async () => { |
| 22 | + await page.click('a[href="/about"]') |
| 23 | + await page.click('a[href="/"]') |
| 24 | + await testDOM() |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +async function testDOM() { |
| 29 | + await testCounter() |
| 30 | + const body = await page.textContent('body') |
| 31 | + expect(body).toContain(fetchedText) |
28 | 32 | }
|
29 | 33 |
|
30 |
| -function getTitle(html: string) { |
31 |
| - const title = html.match(/<title>(.*?)<\/title>/i)?.[1] |
32 |
| - return title |
| 34 | +async function testCounter() { |
| 35 | + // autoRetry() for awaiting client-side code loading & executing |
| 36 | + await autoRetry( |
| 37 | + async () => { |
| 38 | + expect(await page.textContent('button')).toBe('Counter 0') |
| 39 | + await page.click('button') |
| 40 | + expect(await page.textContent('button')).toContain('Counter 1') |
| 41 | + }, |
| 42 | + { timeout: 5 * 1000 }, |
| 43 | + ) |
33 | 44 | }
|
0 commit comments