Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing react components - App.test.tsx
import { fireEvent, render, screen } from "@testing-library/react";
import App from "./App";
const unmockedFetch = global.fetch;
beforeAll(() => {
global.fetch = () =>
Promise.resolve({
json: () =>
Promise.resolve({
name: "The Octocat",
followers: "100",
following: "200",
}),
} as Response);
});
afterAll(() => {
global.fetch = unmockedFetch;
});
test("renders user data", async () => {
render(<App />);
const button = screen.getByText(/Get user/i);
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