This repository was archived by the owner on May 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathTodoList.test.tsx
More file actions
94 lines (90 loc) · 3.06 KB
/
TodoList.test.tsx
File metadata and controls
94 lines (90 loc) · 3.06 KB
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
import { fireEvent, render, screen } from "@testing-library/react";
import { Provider } from "react-redux";
import { MemoryRouter, Route, Routes } from "react-router";
import { TodoState } from "../../store/slices/todo";
import { getMockStore } from "../../test-utils/mock";
import TodoList from "./TodoList";
import { IProps as TodoProps } from "../../components/Todo/Todo";
jest.mock("../../components/Todo/Todo", () => (props: TodoProps) => (
<div data-testid="spyTodo">
<div className="title" onClick={props.clickDetail}>
{props.title}
</div>
<button className="doneButton" onClick={props.clickDone}>
done
</button>
<button className="deleteButton" onClick={props.clickDelete}>
delete
</button>
</div>
));
const stubInitialState: TodoState = {
todos: [
{ id: 1, title: "TODO_TEST_TITLE_1", content: "TODO_TEST_CONTENT_1", done: false },
{ id: 2, title: "TODO_TEST_TITLE_2", content: "TODO_TEST_CONTENT_2", done: false },
{ id: 3, title: "TODO_TEST_TITLE_3", content: "TODO_TEST_CONTENT_3", done: false },
],
selectedTodo: null,
};
const mockStore = getMockStore({ todo: stubInitialState });
const mockNavigate = jest.fn();
jest.mock("react-router", () => ({
...jest.requireActual("react-router"),
useNavigate: () => mockNavigate,
}));
const mockDispatch = jest.fn();
jest.mock("react-redux", () => ({
...jest.requireActual("react-redux"),
useDispatch: () => mockDispatch,
}));
describe("<TodoList />", () => {
let todoList: JSX.Element;
beforeEach(() => {
jest.clearAllMocks();
todoList = (
<Provider store={mockStore}>
<MemoryRouter>
<Routes>
<Route path="/" element={<TodoList title="TODOLIST_TEST_TITLE" />} />
</Routes>
</MemoryRouter>
</Provider>
);
});
it("should render TodoList", () => {
const { container } = render(todoList);
expect(container).toBeTruthy();
});
it("should render todos", () => {
render(todoList);
const todos = screen.getAllByTestId("spyTodo");
expect(todos).toHaveLength(3);
});
it("should handle clickDetail", () => {
render(todoList);
const todos = screen.getAllByTestId("spyTodo");
const todo = todos[0];
// eslint-disable-next-line testing-library/no-node-access
const title = todo.querySelector(".title");
fireEvent.click(title!);
expect(mockNavigate).toHaveBeenCalledTimes(1);
});
it("should handle clickDone", () => {
render(todoList);
const todos = screen.getAllByTestId("spyTodo");
const todo = todos[0];
// eslint-disable-next-line testing-library/no-node-access
const doneButton = todo.querySelector(".doneButton");
fireEvent.click(doneButton!);
expect(mockDispatch).toHaveBeenCalled();
});
it("should handle clickDelete", () => {
render(todoList);
const todos = screen.getAllByTestId("spyTodo");
const todo = todos[0];
// eslint-disable-next-line testing-library/no-node-access
const deleteButton = todo.querySelector(".deleteButton");
fireEvent.click(deleteButton!);
expect(mockDispatch).toHaveBeenCalled();
});
});