This repository has been archived by the owner on Nov 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.test.js
105 lines (89 loc) · 3.95 KB
/
Counter.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { act } from "react-dom/test-utils";
import { createRoot } from "react-dom/client";
import { Counter } from "./Counter";
global.IS_REACT_ACT_ENVIRONMENT = true;
beforeEach(() => {
document.body.innerHTML = "";
});
test("counter increments and decrements when the buttons are clicked", () => {
const div = document.createElement("div");
document.body.append(div);
const root = createRoot(div);
act(() => root.render(<Counter />));
const [decrement, increment] = div.querySelectorAll("button");
const message = div.firstChild.querySelector("div");
expect(message.textContent).toBe("Current count: 0");
act(() => increment.click());
expect(message.textContent).toBe("Current count: 1");
act(() => decrement.click());
expect(message.textContent).toBe("Current count: 0");
});
test("use dispatchEvent: counter increments and decrements when the buttons are clicked", () => {
const div = document.createElement("div");
document.body.append(div);
const root = createRoot(div);
act(() => root.render(<Counter />));
const [decrement, increment] = div.querySelectorAll("button");
const message = div.firstChild.querySelector("div");
expect(message.textContent).toBe("Current count: 0");
const incrementClickEvent = new MouseEvent("click", {
bubbles: true,
cancelable: true,
button: 0,
});
act(() => increment.dispatchEvent(incrementClickEvent));
expect(message.textContent).toBe("Current count: 1");
const decrementClickEvent = new MouseEvent("click", {
bubbles: true,
cancelable: true,
button: 0,
});
act(() => decrement.dispatchEvent(decrementClickEvent));
expect(message.textContent).toBe("Current count: 0");
});
test("React Testing Library: counter increments and decrements when the buttons are clicked ", () => {
const { container } = render(<Counter />);
screen.debug();
const [decrement, increment] = container.querySelectorAll("button");
const message = container.firstChild.querySelector("div");
expect(message.textContent).toBe("Current count: 0");
fireEvent.click(increment);
expect(message.textContent).toBe("Current count: 1");
fireEvent.click(decrement);
expect(message.textContent).toBe("Current count: 0");
});
test("use @testing-library/jest-dom: counter increments and decrements when the buttons are clicked", () => {
const { container } = render(<Counter />);
const [decrement, increment] = container.querySelectorAll("button");
const message = container.firstChild.querySelector("div");
expect(message).toHaveTextContent("Current count: 0");
fireEvent.click(increment);
expect(message).toHaveTextContent("Current count: 1");
fireEvent.click(decrement);
expect(message).toHaveTextContent("Current count: 0");
});
test("Avoid implementation details: counter increments and decrements when the buttons are clicked", () => {
render(<Counter />);
const increment = screen.getByRole("button", { name: /increment/i });
const decrement = screen.getByRole("button", { name: /decrement/i });
const message = screen.getByText(/current count/i);
expect(message).toHaveTextContent("Current count: 0");
fireEvent.click(increment);
expect(message).toHaveTextContent("Current count: 1");
fireEvent.click(decrement);
expect(message).toHaveTextContent("Current count: 0");
});
test("use userEvent: counter increments and decrements when the buttons are clicked", async () => {
render(<Counter />);
const increment = screen.getByRole("button", { name: /increment/i });
const decrement = screen.getByRole("button", { name: /decrement/i });
const message = screen.getByText(/current count/i);
expect(message).toHaveTextContent("Current count: 0");
await userEvent.click(increment);
expect(message).toHaveTextContent("Current count: 1");
await userEvent.click(decrement);
expect(message).toHaveTextContent("Current count: 0");
});