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