Skip to content

Instantly share code, notes, and snippets.

@ozcanzaferayan
Created March 6, 2022 10:02
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 ozcanzaferayan/aaf63eb55a351445a0873c1b1da2eafa to your computer and use it in GitHub Desktop.
Save ozcanzaferayan/aaf63eb55a351445a0873c1b1da2eafa to your computer and use it in GitHub Desktop.
Testing react component - auto mocking - App.test.tsx
import { fireEvent, render, screen } from "@testing-library/react";
import App from "./App";
import fetchMock from "jest-fetch-mock";
import fetch from "jest-fetch-mock";
fetchMock.enableMocks();
beforeEach(() => {
fetch.resetMocks();
});
test("renders user data", async () => {
render(<App />);
const button = screen.getByText(/Get user/i);
fetch.mockResponseOnce(
JSON.stringify({
name: "The Octocat",
followers: "100",
following: "200",
})
);
fireEvent.click(button);
const txtUsername = await screen.findByText(/The Octocat/i);
expect(txtUsername).toBeInTheDocument();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment