-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.test.tsx
44 lines (39 loc) · 1.24 KB
/
App.test.tsx
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
import {
render,
screen,
waitForElementToBeRemoved,
within,
} from "@testing-library/react";
import { array } from "cooky-cutter";
import { userFixture } from "../fixtures";
import { getUsers } from "../api";
import App from "../App";
jest.mock("../api/users");
describe("<Home />", () => {
let getUsersMock: jest.Mock<any, any>;
beforeEach(() => {
getUsersMock = getUsers as jest.Mock<any, any>;
});
it("renders users if returned by the api", async () => {
// arrange
var getUsersResponse = array(userFixture, 5)();
getUsersMock.mockResolvedValue(getUsersResponse);
// act
render(<App />);
// assert
await waitForElementToBeRemoved(screen.getByRole("progressbar"));
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent(
"User List"
);
const userListItems = screen.getAllByRole("listitem");
expect(userListItems).toHaveLength(getUsersResponse.length);
userListItems.forEach((item, index) => {
const { getByRole } = within(item);
const { firstName, lastName } = getUsersResponse[index];
const expectedHeading = `${firstName} ${lastName}`;
expect(
getByRole("heading", { name: expectedHeading })
).toBeInTheDocument();
});
});
});