Created
March 31, 2022 15:01
-
-
Save scottgamer/03ef66f7525deaa067c441725bcca972 to your computer and use it in GitHub Desktop.
Tests without mocked fetch
This file contains hidden or 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 React from "react"; | |
| import { render, screen } from "@testing-library/react"; | |
| import Posts from "./Posts"; | |
| import server from "../../mocks/server"; | |
| import { rest } from "msw"; | |
| import CONSTANTS from "../../constants"; | |
| describe("Posts test suite", () => { | |
| test("Renders the component with loading state", async () => { | |
| render(<Posts />); | |
| await screen.findByText(/Loading posts.../i); | |
| }); | |
| test("Renders the component without posts", async () => { | |
| server.use( | |
| rest.get(`${CONSTANTS.API_URL}/posts`, (req, res, ctx) => { | |
| return res(ctx.status(200), ctx.json([])); | |
| }) | |
| ); | |
| render(<Posts />); | |
| await screen.findByText(/No posts published/i); | |
| }); | |
| test("Renders the component with posts", async () => { | |
| render(<Posts />); | |
| const postsItems = await screen.findAllByRole("article"); | |
| expect(postsItems).toHaveLength(2); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment