|
2 | 2 | * @jest-environment jsdom
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -import { |
6 |
| - render, |
7 |
| - waitForElementToBeRemoved, |
8 |
| - screen, |
9 |
| -} from "@testing-library/react"; |
| 5 | +import { act, render, screen, waitFor } from "@testing-library/react"; |
| 6 | +import { http, HttpResponse } from "msw"; |
| 7 | +import { setupServer } from "msw/node"; |
10 | 8 | import React from "react";
|
| 9 | +import { createMemoryRouter, RouterProvider } from "react-router-dom"; |
11 | 10 |
|
12 | 11 | import { EntryListPage } from "./EntryListPage";
|
13 | 12 |
|
14 |
| -import { TestWrapper } from "TestWrapper"; |
| 13 | +import { entityEntriesPath } from "Routes"; |
| 14 | +import { TestWrapperWithoutRoutes } from "TestWrapper"; |
15 | 15 |
|
16 |
| -afterEach(() => { |
17 |
| - jest.clearAllMocks(); |
18 |
| -}); |
19 |
| - |
20 |
| -test("should match snapshot", async () => { |
21 |
| - const entity = { |
22 |
| - id: 1, |
23 |
| - name: "aaa", |
24 |
| - note: "", |
25 |
| - isToplevel: false, |
26 |
| - attrs: [], |
27 |
| - }; |
28 |
| - const entries = [ |
29 |
| - { |
| 16 | +const server = setupServer( |
| 17 | + // getEntity |
| 18 | + http.get("http://localhost/entity/api/v2/1", () => { |
| 19 | + return HttpResponse.json({ |
30 | 20 | id: 1,
|
31 | 21 | name: "aaa",
|
32 |
| - schema: null, |
33 |
| - isActive: true, |
34 |
| - }, |
35 |
| - { |
36 |
| - id: 2, |
37 |
| - name: "aaaaa", |
38 |
| - schema: null, |
39 |
| - isActive: true, |
40 |
| - }, |
41 |
| - { |
42 |
| - id: 3, |
43 |
| - name: "bbbbb", |
44 |
| - schema: null, |
45 |
| - isActive: true, |
46 |
| - }, |
47 |
| - ]; |
| 22 | + note: "", |
| 23 | + is_toplevel: false, |
| 24 | + attrs: [], |
| 25 | + webhooks: [], |
| 26 | + }); |
| 27 | + }), |
| 28 | + // getEntries |
| 29 | + http.get("http://localhost/entity/api/v2/1/entries/", () => { |
| 30 | + return HttpResponse.json({ |
| 31 | + count: 2, |
| 32 | + next: null, |
| 33 | + previous: null, |
| 34 | + results: [ |
| 35 | + { |
| 36 | + id: 1, |
| 37 | + name: "aaa", |
| 38 | + schema: null, |
| 39 | + is_active: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + id: 2, |
| 43 | + name: "aaaaa", |
| 44 | + schema: null, |
| 45 | + is_active: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + id: 3, |
| 49 | + name: "bbbbb", |
| 50 | + schema: null, |
| 51 | + is_active: true, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }); |
| 55 | + }) |
| 56 | +); |
48 | 57 |
|
49 |
| - /* eslint-disable */ |
50 |
| - jest |
51 |
| - .spyOn( |
52 |
| - require("../repository/AironeApiClient").aironeApiClient, |
53 |
| - "getEntity" |
54 |
| - ) |
55 |
| - .mockResolvedValue(Promise.resolve(entity)); |
56 |
| - jest |
57 |
| - .spyOn( |
58 |
| - require("../repository/AironeApiClient").aironeApiClient, |
59 |
| - "getEntries" |
60 |
| - ) |
61 |
| - .mockResolvedValue(Promise.resolve({ results: entries })); |
62 |
| - /* eslint-enable */ |
| 58 | +beforeAll(() => server.listen()); |
| 59 | +afterEach(() => server.resetHandlers()); |
| 60 | +afterAll(() => server.close()); |
63 | 61 |
|
64 |
| - // wait async calls and get rendered fragment |
65 |
| - const result = render(<EntryListPage />, { |
66 |
| - wrapper: TestWrapper, |
67 |
| - }); |
68 |
| - await waitForElementToBeRemoved(screen.getByTestId("loading")); |
| 62 | +describe("EntryListPage", () => { |
| 63 | + test("should match snapshot", async () => { |
| 64 | + const router = createMemoryRouter( |
| 65 | + [ |
| 66 | + { |
| 67 | + path: entityEntriesPath(":entityId"), |
| 68 | + element: <EntryListPage />, |
| 69 | + }, |
| 70 | + ], |
| 71 | + { |
| 72 | + initialEntries: ["/ui/entities/1/entries"], |
| 73 | + } |
| 74 | + ); |
| 75 | + const result = await act(async () => { |
| 76 | + return render(<RouterProvider router={router} />, { |
| 77 | + wrapper: TestWrapperWithoutRoutes, |
| 78 | + }); |
| 79 | + }); |
| 80 | + await waitFor(() => { |
| 81 | + expect(screen.queryByTestId("loading")).not.toBeInTheDocument(); |
| 82 | + }); |
69 | 83 |
|
70 |
| - expect(result).toMatchSnapshot(); |
| 84 | + expect(result).toMatchSnapshot(); |
| 85 | + }); |
71 | 86 | });
|
0 commit comments