Skip to content

Commit 4e13ef7

Browse files
committed
tests: add routing tests
1 parent 00e3a4b commit 4e13ef7

File tree

12 files changed

+179
-5
lines changed

12 files changed

+179
-5
lines changed

tests/e2e/routing.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect } from '@playwright/test'
2+
import { test } from '../utils/playwright-helpers.js'
3+
4+
const ssrRoutes = [
5+
['/static', 'pages router, static rendering, static routing'],
6+
['/prerendered', 'pages router, prerendering, static routing'],
7+
['/posts/prerendered/1', 'pages router, prerendering, dynamic routing'],
8+
['/dynamic', 'pages router, dynamic rendering, static routing'],
9+
['/posts/dynamic/1', 'pages router, dynamic rendering, dynamic routing'],
10+
['/api/okay', 'pages router, api route, static routing'],
11+
['/api/posts/1', 'pages router, api route, dynamic routing'],
12+
['/static-fetch-1', 'app router, prerendering, static routing'],
13+
['/static-fetch/1', 'app router, prerendering, dynamic routing'],
14+
['/static-fetch-dynamic-1', 'app router, dynamic rendering, static routing'],
15+
['/static-fetch-dynamic/1', 'app router, dynamic rendering, dynamic routing'],
16+
['/api/revalidate-handler', 'app router, route handler, static routing'],
17+
['/api/static/1', 'app router, route handler, dynamic routing'],
18+
]
19+
20+
const notFoundRoutes = [
21+
['/non-existing', 'default'],
22+
['/prerendered/3', 'prerendering, dynamic routing'],
23+
['/dynamic/3', 'dynamic rendering, dynamic routing'],
24+
['/api/non-existing', 'route handler, static routing'],
25+
]
26+
27+
test(`routing works correctly`, async ({ page, serverComponents }) => {
28+
for (const [path, description] of ssrRoutes) {
29+
const url = new URL(path, serverComponents.url).href
30+
const response = await page.goto(url)
31+
expect(response?.status(), `expected 200 response for ${description}`).toBe(200)
32+
}
33+
for (const [path, description] of notFoundRoutes) {
34+
const url = new URL(path, serverComponents.url).href
35+
const response = await page.goto(url)
36+
expect(response?.status(), `expected 404 response for ${description}`).toBe(404)
37+
}
38+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export async function generateStaticParams() {
2+
return [{ id: '1' }, { id: '2' }]
3+
}
4+
5+
async function getData(params) {
6+
const res = await fetch(`https://api.tvmaze.com/shows/${params.id}`, {
7+
next: {
8+
tags: [`show-${params.id}`],
9+
},
10+
})
11+
return res.json()
12+
}
13+
14+
export default async function Page({ params }) {
15+
const data = await getData(params)
16+
17+
return (
18+
<>
19+
<h1>Hello, Force Dynamically Rendered Server Component</h1>
20+
<p>Paths /1 and /2 prerendered; other paths not found</p>
21+
<dl>
22+
<dt>Show</dt>
23+
<dd>{data.name}</dd>
24+
<dt>Param</dt>
25+
<dd>{params.id}</dd>
26+
<dt>Time</dt>
27+
<dd data-testid="date-now">{new Date().toISOString()}</dd>
28+
</dl>
29+
</>
30+
)
31+
}
32+
33+
export const dynamic = 'force-dynamic'
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3+
/// <reference types="next/navigation-types/compat/navigation" />
34

45
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/basic-features/typescript for more information.
6+
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default async function handler(req, res) {
2+
return res.send({ code: 200, message: 'okay' })
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function handler(req, res) {
2+
const { id } = req.query
3+
res.send({ code: 200, message: `okay ${id}` })
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function Yar({ title }) {
2+
return <h1>{title}</h1>
3+
}
4+
5+
export async function getServerSideProps() {
6+
return {
7+
props: {
8+
title: 'My Page',
9+
},
10+
}
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default function Page({ params }) {
2+
return (
3+
<>
4+
<h1>Hello, Dyanmically fetched show</h1>
5+
<dl>
6+
<dt>Param</dt>
7+
<dd>{params.id}</dd>
8+
<dt>Time</dt>
9+
<dd data-testid="date-now">{new Date().toISOString()}</dd>
10+
</dl>
11+
</>
12+
)
13+
}
14+
15+
export async function getServerSideProps({ params }) {
16+
const res = await fetch(`https://api.tvmaze.com/shows/${params.id}`)
17+
const data = await res.json()
18+
19+
return {
20+
props: {
21+
params,
22+
data,
23+
},
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default function Page({ params }) {
2+
return (
3+
<>
4+
<h1>Hello, Statically fetched show</h1>
5+
<p>Paths /1 and /2 prerendered; other paths not found</p>
6+
<dl>
7+
<dt>Param</dt>
8+
<dd>{params.id}</dd>
9+
<dt>Time</dt>
10+
<dd data-testid="date-now">{new Date().toISOString()}</dd>
11+
</dl>
12+
</>
13+
)
14+
}
15+
16+
export async function getStaticPaths() {
17+
return {
18+
paths: [{ params: { id: '1' } }, { params: { id: '2' } }],
19+
fallback: false,
20+
}
21+
}
22+
23+
export async function getStaticProps({ params }) {
24+
const res = await fetch(`https://api.tvmaze.com/shows/${params.id}`)
25+
const data = await res.json()
26+
27+
return {
28+
props: {
29+
params,
30+
data,
31+
},
32+
}
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function Yar({ title }) {
2+
return <h1>{title}</h1>
3+
}
4+
5+
export async function getStaticProps() {
6+
return {
7+
props: {
8+
title: 'My Page',
9+
},
10+
}
11+
}

0 commit comments

Comments
 (0)