diff --git a/frontend/pages/hosts/ManageHostsPage/components/RunScriptBatchPaginatedList/RunScriptBatchPaginatedList.tests.tsx b/frontend/pages/hosts/ManageHostsPage/components/RunScriptBatchPaginatedList/RunScriptBatchPaginatedList.tests.tsx new file mode 100644 index 0000000000..0a1c5701b4 --- /dev/null +++ b/frontend/pages/hosts/ManageHostsPage/components/RunScriptBatchPaginatedList/RunScriptBatchPaginatedList.tests.tsx @@ -0,0 +1,109 @@ +import React from "react"; + +import { http, HttpResponse } from "msw"; +import { screen, waitFor, within } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { baseUrl, createCustomRenderer } from "test/test-utils"; +import mockServer from "test/mock-server"; + +import { createMockScript } from "__mocks__/scriptMock"; + +import RunScriptBatchPaginatedList from "./RunScriptBatchPaginatedList"; + +const waitForLoadingToFinish = async (container: HTMLElement) => { + await waitFor(() => { + expect(container.querySelector(".loading-overlay")).not.toBeInTheDocument(); + }); +}; + +const team1Scripts = [ + createMockScript({ team_id: 1, name: "Team script 1" }), + createMockScript({ id: 2, team_id: 1, name: "Team script 2" }), +]; + +const teamScriptsHandler = http.get(baseUrl(`/scripts?team_id=1`), () => { + // The case where a team has no scripts is handled by the parent + return HttpResponse.json({ + scripts: team1Scripts, + }); +}); + +describe("RunScriptBatchPaginatedList - component", () => { + const render = createCustomRenderer({ + withBackendMock: true, + }); + + it("Lists a team's scripts", async () => { + mockServer.use(teamScriptsHandler); + const { container } = render( + + ); + await waitForLoadingToFinish(container); + + const listedScripts = screen.getAllByRole("listitem"); + expect(listedScripts).toHaveLength(team1Scripts.length); + team1Scripts.forEach((item, index) => { + expect(listedScripts[index]).toHaveTextContent(item.name); + }); + }); + // }); + + it("Calls `onRunScript` with the appropriate script when `Run script`/`Run again` is clicked", async () => { + mockServer.use(teamScriptsHandler); + const onRunScript = jest.fn(); + const { container } = render( + + ); + await waitForLoadingToFinish(container); + const listedScripts = screen.getAllByRole("listitem"); + await userEvent.click(within(listedScripts[0]).getByRole("button")); + await waitFor(() => { + expect(onRunScript.mock.calls.length).toEqual(1); // + }); + // checking ids rather than full equality allows extending the components `fetchPage` to + // modifying the incoming scripst without breaking this test + // const changedItems = onSubmit.mock.calls[0][0]; + const ranScript = onRunScript.mock.calls[0][0]; // the second arg is a callback + expect(ranScript.id).toEqual(team1Scripts[0].id); + }); + + it("Sets the right script for details when clicking on the script's name", async () => { + mockServer.use(teamScriptsHandler); + const onSetScriptForDetails = jest.fn(); + const { container } = render( + + ); + await waitForLoadingToFinish(container); + + const listedScripts = screen.getAllByRole("listitem"); + // click on the script's name + await userEvent.click( + within(listedScripts[0]).getByText(team1Scripts[0].name) + ); + await waitFor(() => { + expect(onSetScriptForDetails.mock.calls.length).toEqual(1); // + }); + // checking ids rather than full equality allows extending the components `fetchPage` to + // modifying the incoming scripst without breaking this test + const detailsScript = onSetScriptForDetails.mock.calls[0][0]; // the second arg is a callback + expect(detailsScript.id).toEqual(team1Scripts[0].id); + }); +}); diff --git a/frontend/pages/hosts/ManageHostsPage/components/RunScriptBatchPaginatedList/RunScriptBatchPaginatedList.tsx b/frontend/pages/hosts/ManageHostsPage/components/RunScriptBatchPaginatedList/RunScriptBatchPaginatedList.tsx index b7e51c8a10..49700fecc7 100644 --- a/frontend/pages/hosts/ManageHostsPage/components/RunScriptBatchPaginatedList/RunScriptBatchPaginatedList.tsx +++ b/frontend/pages/hosts/ManageHostsPage/components/RunScriptBatchPaginatedList/RunScriptBatchPaginatedList.tsx @@ -52,7 +52,7 @@ const RunScriptBatchPaginatedList = ({ } ); - return fetchPromise.then(({ scripts, meta }: IScriptsResponse) => { + return fetchPromise.then(({ scripts }: IScriptsResponse) => { return scripts || []; }); }, diff --git a/frontend/pages/policies/ManagePoliciesPage/components/ConditionalAccessModal/ConditionalAccessModal.tsx b/frontend/pages/policies/ManagePoliciesPage/components/ConditionalAccessModal/ConditionalAccessModal.tsx index 5e3acf0d8e..950069b2c3 100644 --- a/frontend/pages/policies/ManagePoliciesPage/components/ConditionalAccessModal/ConditionalAccessModal.tsx +++ b/frontend/pages/policies/ManagePoliciesPage/components/ConditionalAccessModal/ConditionalAccessModal.tsx @@ -5,8 +5,6 @@ import { LEARN_MORE_ABOUT_BASE_LINK, } from "utilities/constants"; -import { IPolicyStats } from "interfaces/policy"; - import CustomLink from "components/CustomLink"; import Modal from "components/Modal"; import Button from "components/buttons/Button"; diff --git a/frontend/pages/policies/ManagePoliciesPage/components/PoliciesPaginatedList/PoliciesPaginatedList.tests.tsx b/frontend/pages/policies/ManagePoliciesPage/components/PoliciesPaginatedList/PoliciesPaginatedList.tests.tsx index 36fea16eeb..095b5ba8c5 100644 --- a/frontend/pages/policies/ManagePoliciesPage/components/PoliciesPaginatedList/PoliciesPaginatedList.tests.tsx +++ b/frontend/pages/policies/ManagePoliciesPage/components/PoliciesPaginatedList/PoliciesPaginatedList.tests.tsx @@ -41,17 +41,6 @@ const teamPolicies = [ createMockPolicy({ id: 5, team_id: 2, name: "Team policy 2" }), ]; -const globalPoliciesNoMac = [ - createMockPolicy({ team_id: null, name: "Inherited policy 1" }), - createMockPolicy({ id: 2, team_id: null, name: "Inherited policy 2" }), - createMockPolicy({ id: 3, team_id: null, name: "Inherited policy 3" }), -]; - -const teamPoliciesNoMac = [ - createMockPolicy({ id: 4, team_id: 2, name: "Team policy 1" }), - createMockPolicy({ id: 5, team_id: 2, name: "Team policy 2" }), -]; - const globalPoliciesHandler = http.get(baseUrl("/policies"), () => { return HttpResponse.json({ policies: globalPolicies, diff --git a/frontend/pages/policies/ManagePoliciesPage/components/PoliciesPaginatedList/PoliciesPaginatedList.tsx b/frontend/pages/policies/ManagePoliciesPage/components/PoliciesPaginatedList/PoliciesPaginatedList.tsx index b7b915eec0..f0dec40eb6 100644 --- a/frontend/pages/policies/ManagePoliciesPage/components/PoliciesPaginatedList/PoliciesPaginatedList.tsx +++ b/frontend/pages/policies/ManagePoliciesPage/components/PoliciesPaginatedList/PoliciesPaginatedList.tsx @@ -11,7 +11,7 @@ import { ReactElement } from "react-markdown/lib/react-markdown"; import { AppContext } from "context/app"; import PaginatedList, { IPaginatedListHandle } from "components/PaginatedList"; import { useQueryClient } from "react-query"; -import { IPolicy, IPolicyStats } from "interfaces/policy"; +import { IPolicy } from "interfaces/policy"; import teamPoliciesAPI from "services/entities/team_policies"; import globalPoliciesAPI from "services/entities/global_policies";