├── .gitignore
├── README.md
├── __tests__
├── state-functions.test.js
└── todo.test.js
├── app
├── add-todo.js
├── index.js
├── state-functions.js
├── todo.js
└── todos.js
├── babel.config.js
├── index.html
├── package-lock.json
├── package.json
├── prettier.config.js
├── setup-tests.js
└── webpack.config.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | dist/
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # How to Test React Components Using Jest
2 |
3 | A repository written for a [blog post about testing React with Jest](https://www.sitepoint.com/test-react-components-jest).
4 |
5 | ## Requirements
6 |
7 | * [Node.js](http://nodejs.org/)
8 |
9 | ## Running locally
10 |
11 | - `git clone` this repo
12 | - `cd testing-react-with-jest`
13 | - `npm install`
14 | - In one tab, run `npm run watch`. This will fire up Webpack and rebuild your app on each change.
15 | - In another tab, run `npm start`. This will fire up a local server that will refresh automatically when the code changes.
16 | - `open http://localhost:8081` to view to the app.
17 |
18 | ## Tests
19 |
20 | Run `npm test` to run the tests with Jest.
21 |
22 | Run `npm test -- --watch` to run Jest and have it automatically rerun everytime you change a file.
23 |
24 | ## Problems / Questions
25 |
26 | Please feel free to raise an issue if you have any Qs :)
27 |
28 |
29 | ## License
30 |
31 | The MIT License (MIT)
32 |
33 | Copyright (c) 2020 SitePoint
34 |
35 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
36 |
37 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
38 |
39 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 |
--------------------------------------------------------------------------------
/__tests__/state-functions.test.js:
--------------------------------------------------------------------------------
1 | import { toggleDone, deleteTodo } from "../app/state-functions";
2 |
3 | test("tooggleDone completes an incomplete todo", () => {
4 | const startState = [{ id: 1, done: false, text: "Buy Milk" }];
5 |
6 | const finState = toggleDone(startState, 1);
7 |
8 | expect(finState).toEqual([{ id: 1, done: true, text: "Buy Milk" }]);
9 | });
10 |
11 | test("deleteTodo deletes the todo it is given", () => {
12 | const startState = [{ id: 1, done: false, text: "Buy Milk" }];
13 |
14 | const finState = deleteTodo(startState, 1);
15 |
16 | expect(finState).toEqual([]);
17 | });
18 |
--------------------------------------------------------------------------------
/__tests__/todo.test.js:
--------------------------------------------------------------------------------
1 | import Todo from "../app/todo";
2 | import React from "react";
3 | import { mount } from "enzyme";
4 |
5 | test("TodoComponent calls doneChange when todo is clicked", () => {
6 | const todo = { id: 1, done: false, name: "Buy Milk" };
7 | const doneChange = jest.fn();
8 | const wrapper = mount();
9 |
10 | const p = wrapper.find(".toggle-todo");
11 | p.simulate("click");
12 | expect(doneChange).toBeCalledWith(1);
13 | });
14 |
--------------------------------------------------------------------------------
/app/add-todo.js:
--------------------------------------------------------------------------------
1 | import React, { useState } from "react";
2 |
3 | export default function AddTodo(props) {
4 | const [formInput, setFormInput] = useState("");
5 |
6 | const inputChange = (event) => {
7 | setFormInput(event.target.value);
8 | };
9 |
10 | const addTodo = (event) => {
11 | event.preventDefault();
12 |
13 | const newTodoName = formInput;
14 |
15 | if (newTodoName) {
16 | props.onNewTodo({
17 | name: newTodoName,
18 | });
19 |
20 | setFormInput("");
21 | }
22 | };
23 |
24 | return (
25 |