Skip to content

Commit e69402b

Browse files
chore: React Test Library experiments app (#1447)
* chore: React Test Library experiments app * chore: fix lint * chore: fix tests
1 parent fd08a4c commit e69402b

21 files changed

+5849
-1
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
flow-typed/
22
build/
3+
experiments-rtl/

experiments-rtl/.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [
3+
"@babel/preset-typescript",
4+
"@babel/preset-react",
5+
"@babel/preset-env"
6+
],
7+
"plugins": []
8+
}

experiments-rtl/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

experiments-rtl/.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env*.local
29+
30+
# vercel
31+
.vercel
32+
33+
# typescript
34+
*.tsbuildinfo
35+
next-env.d.ts

experiments-rtl/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
# or
12+
pnpm dev
13+
```
14+
15+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
16+
17+
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
18+
19+
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
20+
21+
## Learn More
22+
23+
To learn more about Next.js, take a look at the following resources:
24+
25+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27+
28+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
29+
30+
## Deploy on Vercel
31+
32+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
33+
34+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.

experiments-rtl/jest-setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "@testing-library/jest-dom";

experiments-rtl/jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
testEnvironment: "jsdom",
3+
setupFilesAfterEnv: ["<rootDir>/jest-setup.js"],
4+
};

experiments-rtl/next.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {}
3+
4+
module.exports = nextConfig

experiments-rtl/package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "react-testing-library-experiments",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start",
9+
"test": "jest",
10+
"lint": "next lint"
11+
},
12+
"dependencies": {
13+
"@types/node": "20.4.9",
14+
"@types/react": "18.2.19",
15+
"@types/react-dom": "18.2.7",
16+
"autoprefixer": "10.4.14",
17+
"eslint": "8.46.0",
18+
"eslint-config-next": "13.4.13",
19+
"next": "13.4.13",
20+
"postcss": "8.4.27",
21+
"react": "18.2.0",
22+
"react-dom": "18.2.0",
23+
"tailwindcss": "3.3.3",
24+
"typescript": "5.1.6"
25+
},
26+
"devDependencies": {
27+
"@babel/preset-env": "^7.22.10",
28+
"@babel/preset-react": "^7.22.5",
29+
"@babel/preset-typescript": "^7.22.5",
30+
"@testing-library/jest-dom": "^5.17.0",
31+
"@testing-library/react": "^14.0.0",
32+
"@testing-library/user-event": "^14.4.3",
33+
"@types/jest": "^29.5.3",
34+
"jest": "^29.6.2",
35+
"jest-environment-jsdom": "^29.6.2",
36+
"prettier": "^3.0.1"
37+
}
38+
}

experiments-rtl/postcss.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
plugins: {
3+
tailwindcss: {},
4+
autoprefixer: {},
5+
},
6+
}

experiments-rtl/public/next.svg

Lines changed: 1 addition & 0 deletions
Loading

experiments-rtl/public/vercel.svg

Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as React from "react";
2+
import { render, screen, fireEvent } from "@testing-library/react";
3+
import userEvent from "@testing-library/user-event";
4+
5+
test("userEvent.click()", async () => {
6+
const handleClick = jest.fn();
7+
8+
render(
9+
<button onClick={handleClick} disabled={false}>
10+
Click
11+
</button>
12+
);
13+
14+
const button = screen.getByText("Click");
15+
await userEvent.click(button);
16+
expect(handleClick).toHaveBeenCalledTimes(1);
17+
});
18+
19+
test("fireEvent.click()", () => {
20+
const handleClick = jest.fn();
21+
22+
render(
23+
<button onClick={handleClick} disabled={false}>
24+
Click
25+
</button>
26+
);
27+
28+
const button = screen.getByText("Click");
29+
fireEvent.click(button);
30+
expect(handleClick).toHaveBeenCalledTimes(1);
31+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as React from "react";
2+
import { render, screen, fireEvent } from "@testing-library/react";
3+
import userEvent from "@testing-library/user-event";
4+
5+
interface ManagedInputProps {
6+
defaultValue?: string;
7+
disabled?: boolean;
8+
}
9+
10+
function ManagedInput({ defaultValue, ...props }: ManagedInputProps) {
11+
const [value, setValue] = React.useState(defaultValue ?? "");
12+
13+
return (
14+
<input
15+
type="text"
16+
value={value}
17+
onChange={(e) => setValue(e.currentTarget.value)}
18+
{...props}
19+
/>
20+
);
21+
}
22+
23+
test("userEvent.type()", async () => {
24+
render(
25+
<ManagedInput data-testid="input" defaultValue="Hello" disabled={false} />
26+
);
27+
28+
const input = screen.getByTestId("input");
29+
await userEvent.type(input, " World");
30+
expect(input).toHaveValue("Hello World");
31+
});
32+
33+
test("userEvent.clear()", async () => {
34+
render(
35+
<ManagedInput data-testid="input" defaultValue="Hello" disabled={false} />
36+
);
37+
38+
const input = screen.getByTestId("input");
39+
await userEvent.clear(input);
40+
expect(input).toHaveValue("");
41+
});
42+
43+
test("fireEvent.change()", () => {
44+
render(
45+
<ManagedInput data-testid="input" defaultValue="Hello" disabled={false} />
46+
);
47+
48+
const input = screen.getByTestId("input");
49+
fireEvent.change(input, { target: { value: "World" } });
50+
expect(input).toHaveValue("World");
51+
});

experiments-rtl/src/app/globals.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;
4+
5+
:root {
6+
--foreground-rgb: 0, 0, 0;
7+
--background-start-rgb: 214, 219, 220;
8+
--background-end-rgb: 255, 255, 255;
9+
}
10+
11+
@media (prefers-color-scheme: dark) {
12+
:root {
13+
--foreground-rgb: 255, 255, 255;
14+
--background-start-rgb: 0, 0, 0;
15+
--background-end-rgb: 0, 0, 0;
16+
}
17+
}
18+
19+
body {
20+
color: rgb(var(--foreground-rgb));
21+
background: linear-gradient(
22+
to bottom,
23+
transparent,
24+
rgb(var(--background-end-rgb))
25+
)
26+
rgb(var(--background-start-rgb));
27+
}

experiments-rtl/src/app/layout.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import './globals.css'
2+
import type { Metadata } from 'next'
3+
import { Inter } from 'next/font/google'
4+
5+
const inter = Inter({ subsets: ['latin'] })
6+
7+
export const metadata: Metadata = {
8+
title: 'Create Next App',
9+
description: 'Generated by create next app',
10+
}
11+
12+
export default function RootLayout({
13+
children,
14+
}: {
15+
children: React.ReactNode
16+
}) {
17+
return (
18+
<html lang="en">
19+
<body className={inter.className}>{children}</body>
20+
</html>
21+
)
22+
}

0 commit comments

Comments
 (0)