From d4c001723b2d47b615a3dbca827e6948691bd9e1 Mon Sep 17 00:00:00 2001 From: Kim Dong Hun Date: Thu, 6 Oct 2022 03:06:11 +0900 Subject: [PATCH 1/2] Update lab work --- package.json | 9 +- src/App.test.tsx | 16 ++- src/components/Todo/Todo.test.tsx | 24 ++++ .../TodoList/NewTodo/NewTodo.test.tsx | 11 ++ src/containers/TodoList/NewTodo/NewTodo.tsx | 20 +-- src/containers/TodoList/TodoList.test.tsx | 123 ++++++++++++++++++ src/store/slices/todo.test.ts | 102 +++++++++++++++ src/store/slices/todo.ts | 14 +- src/test-utils/mocks.ts | 9 ++ 9 files changed, 305 insertions(+), 23 deletions(-) create mode 100644 src/components/Todo/Todo.test.tsx create mode 100644 src/containers/TodoList/NewTodo/NewTodo.test.tsx create mode 100644 src/containers/TodoList/TodoList.test.tsx create mode 100644 src/store/slices/todo.test.ts create mode 100644 src/test-utils/mocks.ts diff --git a/package.json b/package.json index e51db5bc..795f335a 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,13 @@ "typescript": "4.7.4", "web-vitals": "2.1.4" }, + "jest": { + "collectCoverageFrom": [ + "src/**/*.{js,jsx,ts,tsx}", + "!src/index.tsx", + "!src/test-utils/*" + ] + }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", @@ -47,4 +54,4 @@ "last 1 safari version" ] } -} \ No newline at end of file +} diff --git a/src/App.test.tsx b/src/App.test.tsx index 838df452..e6054121 100644 --- a/src/App.test.tsx +++ b/src/App.test.tsx @@ -1,7 +1,13 @@ -import { render } from "@testing-library/react"; - +import { render, screen } from "@testing-library/react"; +import { Provider } from "react-redux"; import App from "./App"; - -test("renders learn react link", () => { - render(); +import { store } from "./store"; +test("renders App.tsx", () => { + render( + + + + ); + screen.debug(); + expect(true).toBe(false); // This make failing test case }); diff --git a/src/components/Todo/Todo.test.tsx b/src/components/Todo/Todo.test.tsx new file mode 100644 index 00000000..3bd26338 --- /dev/null +++ b/src/components/Todo/Todo.test.tsx @@ -0,0 +1,24 @@ +import { render, screen } from "@testing-library/react"; +import Todo from "./Todo"; +describe("", () => { + it("should render without errors", () => { + render(); + //screen 으로 렌더링된 것을 접근 + //getByText throws error when cannot find element + screen.getByText("TODO_TITLE"); // Implicit assertion + const doneButton = screen.getByText("Done"); // Implicit assertion + expect(doneButton).toBeInTheDocument(); // Explicit assertion + }); + it("should render done mark when done is true", () => { + render(); + const title = screen.getByText("TODO_TITLE"); + expect(title.classList.contains("done")).toBe(true); + screen.getByText("Undone"); + }); + it("should render undone mark when done is false", () => { + render(); + const title = screen.getByText("TODO_TITLE"); + expect(title.classList.contains("done")).toBe(false); + screen.getByText("Done"); + }); +}); diff --git a/src/containers/TodoList/NewTodo/NewTodo.test.tsx b/src/containers/TodoList/NewTodo/NewTodo.test.tsx new file mode 100644 index 00000000..85c74f39 --- /dev/null +++ b/src/containers/TodoList/NewTodo/NewTodo.test.tsx @@ -0,0 +1,11 @@ +import { render } from "@testing-library/react"; +import NewTodo from "./NewTodo"; + +describe("the test of NewTodo", () => { + //branch submitted + let newTodo: JSX.Element; + beforeEach(() => {}); + it("should", () => { + render(); + }); +}); diff --git a/src/containers/TodoList/NewTodo/NewTodo.tsx b/src/containers/TodoList/NewTodo/NewTodo.tsx index ece2d4b8..3ec2e6ea 100644 --- a/src/containers/TodoList/NewTodo/NewTodo.tsx +++ b/src/containers/TodoList/NewTodo/NewTodo.tsx @@ -12,14 +12,6 @@ export default function NewTodo() { const [submitted, setSubmitted] = useState(false); const dispatch = useDispatch(); - // const navigate = useNavigate() - // const postTodoHandler = () => { - // const data = { title: title, content: content }; - // alert("Submitted\n" + data.title + "\n" + data.content); - // setSubmitted(true); - // navigate('/todos') - // }; - const postTodoHandler = async () => { const data = { title: title, content: content }; const result = await dispatch(postTodo(data)); @@ -38,11 +30,19 @@ export default function NewTodo() {

Add a Todo