Todos.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React from "react";
  2. // Since we're not testing Redux, but our use of it, we can just use the component
  3. // itself instead of mocking Redux.
  4. import { TodoList } from "./Todos";
  5. import { shallow, configure } from "enzyme";
  6. import Adapter from "enzyme-adapter-react-16";
  7. import toJson from "enzyme-to-json";
  8. configure({ adapter: new Adapter() });
  9. const nullFn = () => null;
  10. const mockSubmitEvent = { preventDefault: nullFn };
  11. // Tests
  12. // - addTodo with button click
  13. // - removeTodo with li click
  14. // - matches snapshot
  15. describe("<TodoList />", () => {
  16. let mockAdd;
  17. let mockRemove;
  18. beforeEach(() => {
  19. mockAdd = jest.fn();
  20. mockRemove = jest.fn();
  21. });
  22. it("should call addTodo (Redux action creator) with proper values", () => {
  23. const props = {
  24. addTodo: mockAdd,
  25. todos: [],
  26. };
  27. const wrapper = shallow(<TodoList {...props} />);
  28. const expected = "Buy groceries";
  29. wrapper.find("input").simulate("change", { currentTarget: { value: expected } });
  30. // We could check the operation of the onChange, BTW.
  31. const form = wrapper.find("[data-testid='addTodoForm']");
  32. form.simulate("submit", mockSubmitEvent);
  33. expect(props.addTodo).toHaveBeenCalledWith({ text: expected });
  34. });
  35. it("should call removeTodo (Redux action creator) on li click", () => {
  36. const props = {
  37. removeTodo: mockRemove,
  38. todos: [{ text: "Buy groceries" }, { text: "Change oil" }],
  39. };
  40. const wrapper = shallow(<TodoList {...props} />);
  41. const expectedOffset = 0;
  42. const li = wrapper.find("li").at(expectedOffset);
  43. const actualOffset = parseInt(li.key(), 10);
  44. expect(actualOffset).toBe(expectedOffset);
  45. li.simulate("click");
  46. expect(props.removeTodo).toHaveBeenCalledWith(expectedOffset);
  47. });
  48. it("should match snapshot", () => {
  49. const wrapper = shallow(<TodoList todos={[]} />);
  50. expect(toJson(wrapper)).toMatchSnapshot();
  51. });
  52. });