-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.test.js
69 lines (56 loc) · 1.66 KB
/
App.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { render, screen, cleanup } from "@testing-library/react";
import App from "./App";
import useFetchedData from "./hooks/useFetchedData";
jest.mock("./hooks/useFetchedData");
describe("App", () => {
beforeEach(() => {
cleanup();
useFetchedData.mockReturnValue({ data: null, error: null, loading: true });
});
it("should render loading text", () => {
render(<App />);
expect(screen.getByText("Loading...")).toBeInTheDocument();
});
describe("when data is fetched successfully", () => {
beforeEach(() => {
const mockedData = [
{
body: "mocked body",
id: 1,
title: "mock title",
userId: 1,
},
];
useFetchedData.mockReturnValue({
data: mockedData,
error: null,
loading: false,
});
});
it("should render list of todos", async () => {
render(<App />);
const list = await screen.findByRole("list");
expect(list).toBeInTheDocument();
});
it("should not render loading text", () => {
render(<App />);
const loadingText = screen.queryByText("Loading...");
expect(loadingText).not.toBeInTheDocument();
});
});
describe("when data is not fetched successfully", () => {
beforeEach(() => {
const mockedError = new Error("mocked error");
useFetchedData.mockReturnValue({
data: null,
error: mockedError,
loading: false,
});
});
it("should render the error message", async () => {
render(<App />);
const errorMessage = await screen.findByText("Error: mocked error");
expect(errorMessage).toBeInTheDocument();
});
});
});