-
-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add ssr + client headers result test
- Loading branch information
1 parent
131747e
commit 2466ec1
Showing
5 changed files
with
188 additions
and
76 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
e2e/start/basic/app/routes/-server-fns/response-headers.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as React from 'react' | ||
import { createServerFn } from '@tanstack/start' | ||
import { getHeaders, setHeader } from '@tanstack/start/server' | ||
import type { HTTPHeaderName } from '@tanstack/start/server' | ||
|
||
export const getTestHeaders = createServerFn().handler(() => { | ||
setHeader('x-test-header', 'test-value') | ||
|
||
return { | ||
serverHeaders: getHeaders(), | ||
headers: getHeaders(), | ||
} | ||
}) | ||
|
||
type TestHeadersResult = { | ||
headers?: Partial<Record<HTTPHeaderName, string | undefined>> | ||
serverHeaders?: Partial<Record<HTTPHeaderName, string | undefined>> | ||
} | ||
|
||
// Usage | ||
export function ResponseHeaders({ | ||
initialTestHeaders, | ||
}: { | ||
initialTestHeaders: TestHeadersResult | ||
}) { | ||
const [testHeadersResult, setTestHeadersResult] = | ||
React.useState<TestHeadersResult>(initialTestHeaders) | ||
|
||
return ( | ||
<div className="p-2 border m-2 grid gap-2"> | ||
<h3>Headers Test</h3> | ||
<form | ||
className="flex flex-col gap-2" | ||
data-testid="serialize-formdata-form" | ||
onSubmit={(evt) => { | ||
evt.preventDefault() | ||
getTestHeaders().then(setTestHeadersResult) | ||
}} | ||
> | ||
<button | ||
type="submit" | ||
data-testid="test-headers-btn" | ||
className="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50" | ||
> | ||
Get Headers | ||
</button> | ||
</form> | ||
<div className="overflow-y-auto"> | ||
<h4>Headers:</h4> | ||
<pre data-testid="test-headers-result"> | ||
{JSON.stringify(testHeadersResult.headers, null, 2)} | ||
</pre> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { expect, test } from '@playwright/test' | ||
|
||
test('Navigating to post', async ({ page }) => { | ||
await page.goto('/') | ||
|
||
await page.getByRole('link', { name: 'Posts' }).click() | ||
await page.getByRole('link', { name: 'sunt aut facere repe' }).click() | ||
await page.getByRole('link', { name: 'Deep View' }).click() | ||
await expect(page.getByRole('heading')).toContainText('sunt aut facere') | ||
}) | ||
|
||
test('Navigating to user', async ({ page }) => { | ||
await page.goto('/') | ||
|
||
await page.getByRole('link', { name: 'Users' }).click() | ||
await page.getByRole('link', { name: 'Leanne Graham' }).click() | ||
await expect(page.getByRole('heading')).toContainText('Leanne Graham') | ||
}) | ||
|
||
test('Navigating nested layouts', async ({ page }) => { | ||
await page.goto('/') | ||
|
||
await page.getByRole('link', { name: 'Layout', exact: true }).click() | ||
|
||
await expect(page.locator('body')).toContainText("I'm a layout") | ||
await expect(page.locator('body')).toContainText("I'm a nested layout") | ||
|
||
await page.getByRole('link', { name: 'Layout A' }).click() | ||
await expect(page.locator('body')).toContainText("I'm layout A!") | ||
|
||
await page.getByRole('link', { name: 'Layout B' }).click() | ||
await expect(page.locator('body')).toContainText("I'm layout B!") | ||
}) | ||
|
||
test('directly going to a route with scripts', async ({ page }) => { | ||
await page.goto('/scripts') | ||
expect(await page.evaluate('window.SCRIPT_1')).toBe(true) | ||
expect(await page.evaluate('window.SCRIPT_2')).toBe(undefined) | ||
}) | ||
|
||
test('Navigating to a not-found route', async ({ page }) => { | ||
await page.goto('/') | ||
|
||
await page.getByRole('link', { name: 'This Route Does Not Exist' }).click() | ||
await page.getByRole('link', { name: 'Start Over' }).click() | ||
await expect(page.getByRole('heading')).toContainText('Welcome Home!') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { expect, test } from '@playwright/test' | ||
|
||
test('Navigating to deferred route', async ({ page }) => { | ||
await page.goto('/') | ||
|
||
await page.getByRole('link', { name: 'Deferred' }).click() | ||
|
||
await expect(page.getByTestId('regular-person')).toContainText('John Doe') | ||
await expect(page.getByTestId('deferred-person')).toContainText( | ||
'Tanner Linsley', | ||
) | ||
await expect(page.getByTestId('deferred-stuff')).toContainText( | ||
'Hello deferred!', | ||
) | ||
}) | ||
|
||
test('Directly visiting the deferred route', async ({ page }) => { | ||
await page.goto('/deferred') | ||
|
||
await expect(page.getByTestId('regular-person')).toContainText('John Doe') | ||
await expect(page.getByTestId('deferred-person')).toContainText( | ||
'Tanner Linsley', | ||
) | ||
await expect(page.getByTestId('deferred-stuff')).toContainText( | ||
'Hello deferred!', | ||
) | ||
}) | ||
|
||
test('streaming loader data', async ({ page }) => { | ||
await page.goto('/stream') | ||
|
||
await expect(page.getByTestId('promise-data')).toContainText('promise-data') | ||
await expect(page.getByTestId('stream-data')).toContainText('stream-data') | ||
}) |