Adding tests for NewTodo

At last, we can add our final test suite and make sure NewTodo is covered as well. For the most part, we'll work with the same skeleton we already have and have used before. Create src/NewTodo.test.js and give it the following skeleton:

import React from "react";
import ReactDOM from "react-dom";
import { shallow } from "enzyme";
import renderer from "react-test-renderer";

import NewTodo from "./NewTodo";

describe(NewTodo, () => {
const mockAddTodo = jest.fn();
const component = shallow(<NewTodo addTodo={mockAddTodo} />);

it("renders without crashing", () => {
const div = document.createElement("div");
ReactDOM.render(<NewTodo addTodo={mockAddTodo} />, div);
ReactDOM.unmountComponentAtNode(div);
});

it("renders and matches our snapshot", () => {
const component = renderer.create(<NewTodo addTodo={mockAddTodo} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it("renders a Todo component", () => {
expect(component.contains(<div className="NewTodo" />));
});
});

We'll also want to modify the tests we wrote that check our content, since we should at least make sure that the there's still a text field and a button as part of the form:

it('contains the form', () => {
expect(component.find('input')).toHaveLength(1);
expect(component.find('button')).toHaveLength(1);
});

We’ll also want to test our mocked addTodo function:

  it("calls the passed in addTodo function when add button is clicked", () => {
component.find("button").simulate("click");
expect(mockAddTodo).toBeCalled();
});

This is essentially identical to what we did in the Todo component suite. We'll need a test for our handleUpdate function, which should modify the "item" state property to the faked input value:

  it("updates the form when keys are pressed", () => {
const updateKey = "New Todo";
component.instance().handleUpdate({ target: { value: updateKey } });
expect(component.state("item")).toEqual(updateKey);
});

The structure of the handleUpdate argument is a little wacky, so we need to make sure we're passing in an object that's compatible with the handleUpdate function that we wrote, which is as follows:

  handleUpdate(event) {
this.setState({ item: event.target.value });
}

We then use the state function to verify that "item" now matches what we passed in! We'll close out our test-writing escapade by verifying that when the button to add an item is clicked that the value in the "item" state key is reset to blank:

 it("blanks out the Todo Name when the button is clicked", () => {
const updateKey = "I should be empty";
component.instance().handleUpdate({ target: { value: updateKey } });
expect(component.state("item")).toEqual(updateKey);
component.find("button").simulate("click");
expect(component.state("item")).toHaveLength(0);
});

We need to verify that we're being thorough with our test by making sure the component has a value first and then is reset to blank. If we don't, we wouldn't have any way to verify that our test was working the way we're expecting!

The full test suite is provided here:

import React from "react";
import ReactDOM from "react-dom";
import { shallow } from "enzyme";
import renderer from "react-test-renderer";

import NewTodo from "./NewTodo";

describe(NewTodo, () => {
const mockAddTodo = jest.fn();
const component = shallow(<NewTodo addTodo={mockAddTodo} />);

it("renders without crashing", () => {
const div = document.createElement("div");
ReactDOM.render(<NewTodo addTodo={mockAddTodo} />, div);
ReactDOM.unmountComponentAtNode(div);
});

it("renders and matches our snapshot", () => {
const component = renderer.create(<NewTodo addTodo={mockAddTodo} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it("renders a Todo component", () => {
expect(component.contains(<div className="NewTodo" />));
});

it("contains the form", () => {
expect(component.find("input")).toHaveLength(1);
expect(component.find("button")).toHaveLength(1);
});

it("calls the passed in addTodo function when add button is clicked", () => {
component.find("button").simulate("click");
expect(mockAddTodo).toBeCalled();
});

it("updates the form when keys are pressed", () => {
const updateKey = "New Todo";
component.instance().handleUpdate({ target: { value: updateKey } });
expect(component.state("item")).toEqual(updateKey);
});

it("blanks out the Todo Name when the button is clicked", () => {
const updateKey = "I should be empty";
component.instance().handleUpdate({ target: { value: updateKey } });
expect(component.state("item")).toEqual(updateKey);
component.find("button").simulate("click");
expect(component.state("item")).toHaveLength(0);
});
});

The final result of all of our tests, if you've been following along, should be the following Test Suite results:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.30.162