Skip to content

Commit

Permalink
Merge pull request #1333 from syucream/fix/pages-tests-with-msw
Browse files Browse the repository at this point in the history
introduce msw to most of page tests
  • Loading branch information
userlocalhost authored Dec 2, 2024
2 parents 3c5a73e + f6d2dbe commit d3704e1
Show file tree
Hide file tree
Showing 19 changed files with 1,115 additions and 448 deletions.
85 changes: 49 additions & 36 deletions frontend/src/pages/ACLEditPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,54 @@
*/

import { render, screen, act, waitFor } from "@testing-library/react";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import React from "react";
import { createMemoryRouter, RouterProvider } from "react-router-dom";

import { TestWrapperWithoutRoutes } from "TestWrapper";
import { ACLEditPage } from "pages/ACLEditPage";
import { aclPath } from "routes/Routes";

afterEach(() => {
jest.clearAllMocks();
});
const server = setupServer(
// getAcl
http.get("http://localhost/acl/api/v2/acls/1", () => {
return HttpResponse.json({
name: "acl1",
objtype: 1,
is_public: false,
default_permission: 1,
parent: {
id: 10,
name: "Entity1",
},
roles: [
{
id: 1,
name: "member1",
description: "",
current_permission: 1,
},
],
});
}),
// getEntity
http.get("http://localhost/entity/api/v2/1/", () => {
return HttpResponse.json({
id: 1,
name: "test entity",
note: "",
isToplevel: false,
hasOngoingChanges: false,
attrs: [],
webhooks: [],
});
})
);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

test("should match snapshot", async () => {
Object.defineProperty(window, "django_context", {
Expand All @@ -23,43 +62,17 @@ test("should match snapshot", async () => {
writable: false,
});

const acl = {
name: "acl1",
objtype: "type1",
is_public: false,
default_permission: 1,
parent: {
id: 10,
name: "Entity1",
},
acltypes: [
const router = createMemoryRouter(
[
{
id: 1,
name: "type1",
path: aclPath(":objectId"),
element: <ACLEditPage />,
},
],
roles: [
{
id: 1,
name: "member1",
type: 1,
current_permission: 1,
},
],
};

/* eslint-disable */
jest
.spyOn(require("../repository/AironeApiClient").aironeApiClient, "getAcl")
.mockResolvedValue(Promise.resolve(acl));
/* eslint-enable */

const router = createMemoryRouter([
{
path: "/",
element: <ACLEditPage />,
},
]);
initialEntries: [aclPath(1)],
}
);
const result = await act(async () => {
return render(<RouterProvider router={router} />, {
wrapper: TestWrapperWithoutRoutes,
Expand Down
113 changes: 64 additions & 49 deletions frontend/src/pages/ACLHistoryPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,60 @@
* @jest-environment jsdom
*/

import { ACLHistory } from "@dmm-com/airone-apiclient-typescript-fetch";
import {
render,
waitForElementToBeRemoved,
screen,
} from "@testing-library/react";
import { render, screen, act, waitFor } from "@testing-library/react";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import React from "react";
import { createMemoryRouter, RouterProvider } from "react-router-dom";

import { ACLHistoryPage } from "./ACLHistoryPage";

import { TestWrapper } from "TestWrapper";
import { TestWrapperWithoutRoutes } from "TestWrapper";
import { aclHistoryPath } from "routes/Routes";

afterEach(() => {
jest.clearAllMocks();
});
const server = setupServer(
// getAcl
http.get("http://localhost/acl/api/v2/acls/1", () => {
return HttpResponse.json({
name: "acl1",
objtype: 1,
is_public: false,
default_permission: 1,
parent: {
id: 10,
name: "Entity1",
},
roles: [
{
id: 1,
name: "member1",
description: "",
current_permission: 1,
},
],
});
}),
// getAclHistory
http.get("http://localhost/acl/api/v2/acls/1/history", () => {
return HttpResponse.json([]);
}),
// getEntity
http.get("http://localhost/entity/api/v2/1/", () => {
return HttpResponse.json({
id: 1,
name: "test entity",
note: "",
isToplevel: false,
hasOngoingChanges: false,
attrs: [],
webhooks: [],
});
})
);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

test("should match snapshot", async () => {
Object.defineProperty(window, "django_context", {
Expand All @@ -28,49 +67,25 @@ test("should match snapshot", async () => {
writable: false,
});

const history: Array<ACLHistory> = [];
const acl = {
name: "acl1",
objtype: "type1",
is_public: false,
default_permission: 1,
parent: {
id: 10,
name: "Entity1",
},
acltypes: [
{
id: 1,
name: "type1",
},
],
roles: [
const router = createMemoryRouter(
[
{
id: 1,
name: "member1",
type: 1,
current_permission: 1,
path: aclHistoryPath(":objectId"),
element: <ACLHistoryPage />,
},
],
};

/* eslint-disable */
jest
.spyOn(
require("../repository/AironeApiClient").aironeApiClient,
"getAclHistory"
)
.mockResolvedValue(Promise.resolve(history));
jest
.spyOn(require("../repository/AironeApiClient").aironeApiClient, "getAcl")
.mockResolvedValue(Promise.resolve(acl));
/* eslint-enable */

// wait async calls and get rendered fragment
const result = render(<ACLHistoryPage />, {
wrapper: TestWrapper,
{
initialEntries: [aclHistoryPath(1)],
}
);
const result = await act(async () => {
return render(<RouterProvider router={router} />, {
wrapper: TestWrapperWithoutRoutes,
});
});
await waitFor(() => {
expect(screen.queryByTestId("loading")).not.toBeInTheDocument();
});
await waitForElementToBeRemoved(screen.getByTestId("loading"));

expect(result).toMatchSnapshot();
});
114 changes: 55 additions & 59 deletions frontend/src/pages/AdvancedSearchResultsPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,59 @@
* @jest-environment jsdom
*/

import { AdvancedSearchResultValue } from "@dmm-com/airone-apiclient-typescript-fetch";
import {
render,
waitForElementToBeRemoved,
screen,
} from "@testing-library/react";
import { render, screen, act, waitFor } from "@testing-library/react";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import React from "react";

import { TestWrapper } from "TestWrapper";
import { AdvancedSearchResultsPage } from "pages/AdvancedSearchResultsPage";

afterEach(() => {
jest.clearAllMocks();
});
const server = setupServer(
// getEntityAttrs
http.get(
"http://localhost/entity/api/v2/attrs?entity_ids=1&referral_attr=",
() => {
return HttpResponse.json([]);
}
),
// advancedSearch
http.post("http://localhost/entry/api/v2/advanced_search/", () => {
return HttpResponse.json({
count: 1,
total_count: 1,
values: [
{
entry: {
id: 2,
name: "entry1",
},
entity: {
id: 1,
name: "entity1",
},
attrs: {
attr1: {
type: 2,
value: { asString: "attr1" },
isReadable: true,
},
attr2: {
type: 2,
value: { asString: "attr1" },
isReadable: true,
},
},
isReadable: true,
},
],
});
})
);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

test("should match snapshot", async () => {
Object.defineProperty(window, "django_context", {
Expand All @@ -27,58 +66,15 @@ test("should match snapshot", async () => {
writable: false,
});

const results: AdvancedSearchResultValue[] = [
{
entry: {
id: 2,
name: "entry1",
},
entity: {
id: 1,
name: "entity1",
},
attrs: {
attr1: {
type: 2,
value: { asString: "attr1" },
isReadable: true,
},
attr2: {
type: 2,
value: { asString: "attr1" },
isReadable: true,
},
},
isReadable: true,
},
];

/* eslint-disable */
jest
.spyOn(
require("repository/AironeApiClient").aironeApiClient,
"advancedSearch"
)
.mockResolvedValue(
Promise.resolve({
count: 1,
values: results,
totalCount: 1,
})
);
jest
.spyOn(
require("repository/AironeApiClient").aironeApiClient,
"getEntityAttrs"
)
.mockResolvedValue(Promise.resolve([]));
/* eslint-enable */

// wait async calls and get rendered fragment
const result = render(<AdvancedSearchResultsPage />, {
wrapper: TestWrapper,
const result = await act(async () => {
return render(<AdvancedSearchResultsPage />, {
wrapper: TestWrapper,
});
});
await waitFor(() => {
expect(screen.queryByTestId("loading")).not.toBeInTheDocument();
});
await waitForElementToBeRemoved(screen.getByTestId("loading"));

expect(result).toMatchSnapshot();
});
Loading

0 comments on commit d3704e1

Please sign in to comment.