-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetExamplesButton.test.tsx
More file actions
51 lines (41 loc) · 1.79 KB
/
GetExamplesButton.test.tsx
File metadata and controls
51 lines (41 loc) · 1.79 KB
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
import { act, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, test, vi } from "vitest";
import { delayedResponse } from "../../../test/helpers/response";
import { userEventSetup } from "../../../test/helpers/userEventSetup";
import * as exampleApi from "../../api/example";
import { GetExamplesButton } from "./GetExamplesButton";
describe("GetExamplesButton", () => {
test("should called get examples api and view examples", async () => {
render(<GetExamplesButton />);
await userEvent.click(screen.getByRole("button"));
expect(exampleApi.getExamples).toBeCalledTimes(1);
// Since getExamples is mocked in jest.setup.ts, return value defined in __mocks__ file
expect(screen.getAllByRole("listitem").length).toBe(3);
expect(screen.getAllByRole("listitem")[0]).toHaveTextContent("nus1");
expect(screen.getAllByRole("listitem")[1]).toHaveTextContent("nus2");
expect(screen.getAllByRole("listitem")[2]).toHaveTextContent("nus3");
});
test("should render loading", async () => {
vi.useFakeTimers({ shouldAdvanceTime: true });
const user = userEventSetup();
vi.spyOn(exampleApi, "getExamples").mockImplementation(() =>
delayedResponse<exampleApi.GetExamplesResponse>(500, {
examples: [
{ id: "1", name: "nus1" },
{ id: "2", name: "nus2" },
{ id: "3", name: "nus3" },
],
}),
);
render(<GetExamplesButton />);
await user.click(screen.getByRole("button"));
expect(screen.getByTestId("Loading")).toBeInTheDocument();
act(() => {
vi.advanceTimersByTime(500);
});
await waitFor(() => expect(screen.queryByTestId("Loading")).toBeNull());
vi.runOnlyPendingTimers();
vi.useRealTimers();
});
});