Form.test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React from "react";
  2. import { Form } from "./Form";
  3. import { shallow, configure } from "enzyme";
  4. import Adapter from "enzyme-adapter-react-16";
  5. import toJson from "enzyme-to-json";
  6. import { api } from "./api";
  7. configure({ adapter: new Adapter() });
  8. const makeChangeEvent = value => ({ currentTarget: { value } });
  9. const nullFn = () => null;
  10. const updateInput = (wrapper, instance, newValue) => {
  11. const input = wrapper.find(instance);
  12. input.simulate("change", makeChangeEvent(newValue));
  13. // Now updated by the event.
  14. return wrapper.find(instance);
  15. };
  16. describe("<Form />", () => {
  17. let wrapper;
  18. beforeEach(() => {
  19. wrapper = shallow(<Form />);
  20. });
  21. // opted-in by default: have to opt-out (!)
  22. // actually input their information
  23. // submits the form, calls the api method
  24. // matches snapshot
  25. it("should receive default promotions as true", () => {
  26. const promotionInput = wrapper.find("[data-testid='checked']");
  27. expect(promotionInput.props().checked).toBe(true);
  28. });
  29. it("should allow users to fill out form", () => {
  30. const expectations = {
  31. "name": "Tyler",
  32. "email": "tyler@durden.me",
  33. "number": "42", // User inputs in test fields are strings, even when numeric.
  34. };
  35. const actual = {};
  36. for (const [input, expected] of Object.entries(expectations)) {
  37. actual[input] = updateInput(wrapper, `[data-testid="${input}"]`, expected);
  38. }
  39. for (const [input, expected] of Object.entries(expectations)) {
  40. expect(actual[input].props().value).toBe(expected);
  41. expect(actual[input].getElement().props.value).toBe(expected);
  42. }
  43. const promotionInputPre = wrapper.find("[data-testid='checked']");
  44. promotionInputPre.simulate("click");
  45. const promotionInputPost = wrapper.find("[data-testid='checked']");
  46. expect(promotionInputPost.props().checked).toBe(false);
  47. });
  48. it("should submit the form to the API", () => {
  49. const expectations = {
  50. "name": "Tyler",
  51. "email": "tyler@durden.me",
  52. "number": "42", // User inputs in test fields are strings, even when numeric.
  53. };
  54. for (const [input, expected] of Object.entries(expectations)) {
  55. updateInput(wrapper, `[data-testid="${input}"]`, expected);
  56. }
  57. // Ensure addUser is called on submit, but we're not testing the API, so mock it.
  58. jest.spyOn(api, "addUser").mockImplementation(() => Promise.resolve({ data: "New user added" }));
  59. const submit = wrapper.find("[data-testid='addUserForm']");
  60. submit.simulate("submit", { preventDefault: nullFn });
  61. expect(api.addUser).toHaveBeenCalledWith(...Object.values(expectations));
  62. });
  63. it("should match snapshot", () => {
  64. expect(toJson(wrapper)).toMatchSnapshot();
  65. });
  66. });