Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce msw to most of page tests #1333

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading