Skip to content

Commit 54f635b

Browse files
authored
Unit testing setup for react-router (#15)
Unit testing setup for react-router
1 parent 41facba commit 54f635b

File tree

12 files changed

+2200
-15
lines changed

12 files changed

+2200
-15
lines changed

app/localization/resource.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import english from "../../resources/locales/en/common.json"
33

44
const languages = ["en", "bs"] as const
55
export const supportedLanguages = [...languages]
6-
type Language = (typeof languages)[number]
6+
export type Language = (typeof languages)[number]
77

88
type Resource = {
99
common: typeof english
1010
}
1111

12+
export type Namespace = keyof Resource
13+
1214
export const resources: Record<Language, Resource> = {
1315
en: {
1416
common: english,

app/routes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { flatRoutes } from "@react-router/fs-routes"
22

3-
export default flatRoutes()
3+
export default flatRoutes({
4+
ignoredRouteFiles: ["**/*.test.{ts,tsx}"],
5+
})

app/routes/_index.test.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as Module from "./_index"
2+
3+
describe("Home route", () => {
4+
it("should render the home page text properly in english", async ({
5+
renderStub,
6+
/** debug */
7+
}) => {
8+
const { container } = await renderStub({
9+
entries: [
10+
{
11+
id: "home",
12+
path: "/",
13+
Component: Module.default,
14+
},
15+
],
16+
})
17+
// debug()
18+
expect(
19+
container.queryByText("React Router is awesome!", {
20+
exact: false,
21+
})
22+
).not.toBeNull()
23+
})
24+
25+
it("should render the home page text properly in bosnian", async ({ renderStub }) => {
26+
const { container } = await renderStub({
27+
entries: [
28+
{
29+
id: "home",
30+
path: "/",
31+
Component: Module.default,
32+
},
33+
],
34+
i18n: {
35+
lng: "bs",
36+
},
37+
})
38+
39+
expect(
40+
container.queryByText("React Router je zakon!", {
41+
exact: false,
42+
})
43+
).not.toBeNull()
44+
})
45+
})

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"test": "vitest run",
1818
"test:ui": "vitest --ui --api 9527",
1919
"test:cov": "vitest run --coverage",
20+
"test:debug": "jest-preview",
21+
"test:live": "npm-run-all -p test:ui test:debug",
2022
"typecheck": "tsc",
2123
"validate": "pnpm run check && pnpm run typecheck && pnpm run test && pnpm run check:unused",
2224
"check": "biome check .",
@@ -51,6 +53,7 @@
5153
"@dotenvx/dotenvx": "1.24.5",
5254
"@react-router/dev": "7.0.1",
5355
"@react-router/fs-routes": "7.0.1",
56+
"@testing-library/react": "16.0.1",
5457
"@types/node": "22.9.1",
5558
"@types/prompt": "1.1.9",
5659
"@types/react": "18.3.12",
@@ -60,11 +63,13 @@
6063
"autoprefixer": "10.4.20",
6164
"chalk": "5.3.0",
6265
"happy-dom": "15.11.6",
66+
"jest-preview": "0.3.1",
6367
"knip": "5.37.2",
6468
"lefthook": "1.8.4",
69+
"npm-run-all": "4.1.5",
6570
"postcss": "8.4.49",
6671
"prompt": "1.3.0",
67-
"react-router-devtools": "1.0.3",
72+
"react-router-devtools": "1.0.5",
6873
"tailwindcss": "3.4.15",
6974
"tsx": "4.19.2",
7075
"typescript": "5.6.3",

0 commit comments

Comments
 (0)