Skip to content

Instantly share code, notes, and snippets.

@xiaoyunyang
Created July 3, 2019 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaoyunyang/c864021d275c95d2b91c652176b4d9c0 to your computer and use it in GitHub Desktop.
Save xiaoyunyang/c864021d275c95d2b91c652176b4d9c0 to your computer and use it in GitHub Desktop.
Testing With Jest

What to Test

Testing interfaces

  • Child component with the right props
import EditProfileForm from "../feedbackForm";
import Checkbox from "../checkbox";
import EmailInput from "../EmailInput";
import TextInput from "../TextInput";

describe("FeedbackForm", () => {
  let props;
  let submitSpy;
  let form;
  
  beforeEach(() => {
    submitSpy = jest.fn();
    props = {
      userName: "janeDoe",
      firstName: "Jane",
      lastName: "Doe",
      email: "janeDoe@example.com",
      submit: submitSpy
    }
    form = shallow(<EditProfileForm {...props} />);
  });
  describe("render", () => {
    test("renders emailInput with right props", () => {
      const emailInput = form.find(EmailInput);
      expect(emailInput).toHaveLength(1);
      expect(emailInput.props()).toEqual({
        value: props.email
      });
    });
    
    test("renders the text input with right props", () => {
      const textInputs = form.find(TextInput);
      expect(textInputs).toHaveLength(3);
      expect(textInputs.at(0).props()).toEqual({
        value: props.userName,
        locked: true
      });
      expect(textInputs.at(1).props()).toEqual({
        value: props.firstName,
        locked: false
      });
      expect(textInputs.at(2).props()).toEqual({
        value: props.lastName,
        locked: false
      });
    });
  });
  
  describe("submit", () => {
    test("submit with the right props", () => {
      form.simulate("click");
      expect(submitSpy).toHaveBeenNthCalledWith(1, {
        email: form.state().email,
        firstName: form.state().firstName,
        lastName: form.state().lastName
      });
    });
  });
    
   
  
  
})

Mock and Spy with Jest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment