Skip to content

Commit 3efca4a

Browse files
tests: e2e test for active links with special char encoding
1 parent 5d2e483 commit 3efca4a

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createFileRoute, Link } from '@tanstack/react-router'
2+
import z from 'zod'
3+
4+
export const Route = createFileRoute('/encoding/link-active/$target')({
5+
validateSearch: z.object({ foo: z.string().optional() }),
6+
7+
component: RouteComponent,
8+
})
9+
10+
function RouteComponent() {
11+
return (
12+
<Link
13+
data-testid="self-link"
14+
activeProps={{ className: 'font-bold' }}
15+
activeOptions={{ includeSearch: true }}
16+
to="/encoding/link-active/$target"
17+
params={{ target: Route.useParams().target }}
18+
search={Route.useSearch()}
19+
>
20+
link to self with $target={Route.useParams().target}, search.foo=
21+
{Route.useSearch().foo}
22+
</Link>
23+
)
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/encoding/link-active/')({
4+
component: RouteComponent,
5+
})
6+
7+
function RouteComponent() {
8+
return (
9+
<div>
10+
<Route.Link to="$target" params={{ target: 'ê' }}>
11+
link to $target=ê
12+
</Route.Link>
13+
<br />
14+
<Route.Link to="$target" params={{ target: 'hello' }}>
15+
link to $target=hello
16+
</Route.Link>
17+
</div>
18+
)
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { expect } from '@playwright/test'
2+
3+
import { test } from '@tanstack/router-e2e-utils'
4+
import combinateImport from 'combinate'
5+
6+
// somehow playwright does not correctly import default exports
7+
const combinate = (combinateImport as any).default as typeof combinateImport
8+
9+
test.beforeEach(async ({ page }) => {
10+
await page.goto('/')
11+
})
12+
13+
test.use({
14+
whitelistErrors: [
15+
/Failed to load resource: the server responded with a status of 404/,
16+
],
17+
})
18+
19+
combinate
20+
test.describe('link active', () => {
21+
const testMatrix = combinate({
22+
target: ['ê', 'hello'] as const,
23+
search: [undefined, { foo: 'bar' }, { foo: 'ö' }] as const,
24+
})
25+
26+
testMatrix.forEach(({ target, search }) => {
27+
test(`should correctly highlight active link for $target=${target} and search=${JSON.stringify(search)}`, async ({
28+
page,
29+
}) => {
30+
const url = new URL(
31+
`/encoding/link-active/${target}`,
32+
'http://localhost:3000',
33+
)
34+
if (search) {
35+
Object.entries(search).forEach(([key, value]) => {
36+
url.searchParams.set(key, value)
37+
})
38+
}
39+
const hrefWithoutOrigin = url.href.replace(url.origin, '')
40+
await page.goto(hrefWithoutOrigin)
41+
const link = page.getByTestId('self-link')
42+
await expect(link).toBeInViewport()
43+
await expect(link).toHaveAttribute('class', 'font-bold')
44+
await expect(link).toHaveAttribute('data-status', 'active')
45+
})
46+
})
47+
})

0 commit comments

Comments
 (0)