Skip to content

Instantly share code, notes, and snippets.

@rohanBagchi
Created February 22, 2021 04:18
Show Gist options
  • Save rohanBagchi/3ed43263738fb24ef0f70f726b9ba111 to your computer and use it in GitHub Desktop.
Save rohanBagchi/3ed43263738fb24ef0f70f726b9ba111 to your computer and use it in GitHub Desktop.
devto-testing rtl test
import { render, fireEvent, screen } from "@testing-library/react";
import { rest } from "msw";
import { setupServer } from "msw/node";
import App from "./App";
const joke = 'Foo Bar!';
const server = setupServer(
rest.get("https://api.icndb.com/jokes/random", (req, res, ctx) => {
return res(
ctx.json({
type: "success",
value: {
joke
}
})
);
})
);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
test("App", async () => {
render(<App />);
fireEvent.click(screen.getByText("Get a random joke"));
expect(await screen.findByText(joke)).toBeDefined();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment