Skip to content

Instantly share code, notes, and snippets.

@jensen
Created April 7, 2020 20:46
Show Gist options
  • Save jensen/80c5b98d46a63a3d55692a1a51b43af5 to your computer and use it in GitHub Desktop.
Save jensen/80c5b98d46a63a3d55692a1a51b43af5 to your computer and use it in GitHub Desktop.
Example of some React Testing
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import { AddMethod } from "components/request/method";
describe("Adding Requests", () => {
it("should call addRequest with GET when the plus button is clicked", () => {
const addRequestMock = jest.fn();
const { getByTitle } = render(<AddMethod addRequest={addRequestMock} />);
fireEvent.click(getByTitle("Add"));
expect(addRequestMock).toBeCalledTimes(1);
expect(addRequestMock).toBeCalledWith("GET");
});
it("should call addRequest with the matching method", () => {
const addRequestMock = jest.fn();
const { getByText } = render(<AddMethod addRequest={addRequestMock} />);
fireEvent.click(getByText("GET"));
expect(addRequestMock).toBeCalledTimes(1);
expect(addRequestMock).toBeCalledWith("GET");
fireEvent.click(getByText("POST"));
expect(addRequestMock).toBeCalledTimes(2);
expect(addRequestMock).toBeCalledWith("POST");
});
});
@a13chrju
Copy link

a13chrju commented Apr 7, 2020

Arigato!

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