Created
March 6, 2022 10:02
-
-
Save ozcanzaferayan/aaf63eb55a351445a0873c1b1da2eafa to your computer and use it in GitHub Desktop.
Testing react component - auto mocking - App.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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