Skip to content

Instantly share code, notes, and snippets.

@ozcanzaferayan
Last active March 4, 2022 22: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 ozcanzaferayan/fb3e7acc51f60f8dcf4b7815d4985ffe to your computer and use it in GitHub Desktop.
Save ozcanzaferayan/fb3e7acc51f60f8dcf4b7815d4985ffe to your computer and use it in GitHub Desktop.
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