mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 01:18:42 +00:00
Update test structure (#28874)
Organizational improvement to a test, no functional changes to the product Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
This commit is contained in:
parent
fe087db01b
commit
1cf68356b1
2 changed files with 36 additions and 18 deletions
|
|
@ -1,11 +1,12 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import { http, HttpResponse } from "msw";
|
|
||||||
import { screen, waitFor, within } from "@testing-library/react";
|
import { screen, waitFor, within } from "@testing-library/react";
|
||||||
import userEvent from "@testing-library/user-event";
|
import userEvent from "@testing-library/user-event";
|
||||||
import { baseUrl, createCustomRenderer } from "test/test-utils";
|
import { createCustomRenderer } from "test/test-utils";
|
||||||
import mockServer from "test/mock-server";
|
import mockServer from "test/mock-server";
|
||||||
|
|
||||||
|
import getTeamScriptsHandler from "test/handlers/script-handlers";
|
||||||
|
|
||||||
import { createMockScript } from "__mocks__/scriptMock";
|
import { createMockScript } from "__mocks__/scriptMock";
|
||||||
|
|
||||||
import RunScriptBatchPaginatedList from "./RunScriptBatchPaginatedList";
|
import RunScriptBatchPaginatedList from "./RunScriptBatchPaginatedList";
|
||||||
|
|
@ -16,17 +17,12 @@ const waitForLoadingToFinish = async (container: HTMLElement) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const team1Scripts = [
|
const team1ScriptOverrides = [
|
||||||
createMockScript({ team_id: 1, name: "Team script 1" }),
|
createMockScript({ id: 1, team_id: 1, name: "Team script 1" }),
|
||||||
createMockScript({ id: 2, team_id: 1, name: "Team script 2" }),
|
createMockScript({ id: 2, team_id: 1, name: "Team script 2" }),
|
||||||
];
|
];
|
||||||
|
|
||||||
const teamScriptsHandler = http.get(baseUrl(`/scripts?team_id=1`), () => {
|
const team1ScriptsHandler = getTeamScriptsHandler(1, team1ScriptOverrides);
|
||||||
// The case where a team has no scripts is handled by the parent
|
|
||||||
return HttpResponse.json({
|
|
||||||
scripts: team1Scripts,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("RunScriptBatchPaginatedList - component", () => {
|
describe("RunScriptBatchPaginatedList - component", () => {
|
||||||
const render = createCustomRenderer({
|
const render = createCustomRenderer({
|
||||||
|
|
@ -34,7 +30,7 @@ describe("RunScriptBatchPaginatedList - component", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Lists a team's scripts", async () => {
|
it("Lists a team's scripts", async () => {
|
||||||
mockServer.use(teamScriptsHandler);
|
mockServer.use(team1ScriptsHandler);
|
||||||
const { container } = render(
|
const { container } = render(
|
||||||
<RunScriptBatchPaginatedList
|
<RunScriptBatchPaginatedList
|
||||||
onRunScript={jest.fn()}
|
onRunScript={jest.fn()}
|
||||||
|
|
@ -47,15 +43,15 @@ describe("RunScriptBatchPaginatedList - component", () => {
|
||||||
await waitForLoadingToFinish(container);
|
await waitForLoadingToFinish(container);
|
||||||
|
|
||||||
const listedScripts = screen.getAllByRole("listitem");
|
const listedScripts = screen.getAllByRole("listitem");
|
||||||
expect(listedScripts).toHaveLength(team1Scripts.length);
|
expect(listedScripts).toHaveLength(team1ScriptOverrides.length);
|
||||||
team1Scripts.forEach((item, index) => {
|
team1ScriptOverrides.forEach((item, index) => {
|
||||||
expect(listedScripts[index]).toHaveTextContent(item.name);
|
expect(listedScripts[index]).toHaveTextContent(item.name);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// });
|
// });
|
||||||
|
|
||||||
it("Calls `onRunScript` with the appropriate script when `Run script`/`Run again` is clicked", async () => {
|
it("Calls `onRunScript` with the appropriate script when `Run script`/`Run again` is clicked", async () => {
|
||||||
mockServer.use(teamScriptsHandler);
|
mockServer.use(team1ScriptsHandler);
|
||||||
const onRunScript = jest.fn();
|
const onRunScript = jest.fn();
|
||||||
const { container } = render(
|
const { container } = render(
|
||||||
<RunScriptBatchPaginatedList
|
<RunScriptBatchPaginatedList
|
||||||
|
|
@ -76,11 +72,11 @@ describe("RunScriptBatchPaginatedList - component", () => {
|
||||||
// modifying the incoming scripst without breaking this test
|
// modifying the incoming scripst without breaking this test
|
||||||
// const changedItems = onSubmit.mock.calls[0][0];
|
// const changedItems = onSubmit.mock.calls[0][0];
|
||||||
const ranScript = onRunScript.mock.calls[0][0]; // the second arg is a callback
|
const ranScript = onRunScript.mock.calls[0][0]; // the second arg is a callback
|
||||||
expect(ranScript.id).toEqual(team1Scripts[0].id);
|
expect(ranScript.id).toEqual(team1ScriptOverrides[0].id);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Sets the right script for details when clicking on the script's name", async () => {
|
it("Sets the right script for details when clicking on the script's name", async () => {
|
||||||
mockServer.use(teamScriptsHandler);
|
mockServer.use(team1ScriptsHandler);
|
||||||
const onSetScriptForDetails = jest.fn();
|
const onSetScriptForDetails = jest.fn();
|
||||||
const { container } = render(
|
const { container } = render(
|
||||||
<RunScriptBatchPaginatedList
|
<RunScriptBatchPaginatedList
|
||||||
|
|
@ -96,7 +92,7 @@ describe("RunScriptBatchPaginatedList - component", () => {
|
||||||
const listedScripts = screen.getAllByRole("listitem");
|
const listedScripts = screen.getAllByRole("listitem");
|
||||||
// click on the script's name
|
// click on the script's name
|
||||||
await userEvent.click(
|
await userEvent.click(
|
||||||
within(listedScripts[0]).getByText(team1Scripts[0].name)
|
within(listedScripts[0]).getByText(team1ScriptOverrides[0].name)
|
||||||
);
|
);
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(onSetScriptForDetails.mock.calls.length).toEqual(1); //
|
expect(onSetScriptForDetails.mock.calls.length).toEqual(1); //
|
||||||
|
|
@ -104,6 +100,6 @@ describe("RunScriptBatchPaginatedList - component", () => {
|
||||||
// checking ids rather than full equality allows extending the components `fetchPage` to
|
// checking ids rather than full equality allows extending the components `fetchPage` to
|
||||||
// modifying the incoming scripst without breaking this test
|
// modifying the incoming scripst without breaking this test
|
||||||
const detailsScript = onSetScriptForDetails.mock.calls[0][0]; // the second arg is a callback
|
const detailsScript = onSetScriptForDetails.mock.calls[0][0]; // the second arg is a callback
|
||||||
expect(detailsScript.id).toEqual(team1Scripts[0].id);
|
expect(detailsScript.id).toEqual(team1ScriptOverrides[0].id);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
22
frontend/test/handlers/script-handlers.ts
Normal file
22
frontend/test/handlers/script-handlers.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import { http, HttpResponse } from "msw";
|
||||||
|
|
||||||
|
import { baseUrl } from "test/test-utils";
|
||||||
|
import { IScript } from "interfaces/script";
|
||||||
|
import { createMockScript } from "__mocks__/scriptMock";
|
||||||
|
|
||||||
|
// not supported for all teams
|
||||||
|
const getTeamScriptsHandler = (
|
||||||
|
teamId: number,
|
||||||
|
overrides: Partial<IScript>[]
|
||||||
|
) => {
|
||||||
|
const scripts = overrides.map((scriptOverride) =>
|
||||||
|
createMockScript(scriptOverride)
|
||||||
|
);
|
||||||
|
return http.get(baseUrl(`/scripts?team_id=${teamId}`), () =>
|
||||||
|
HttpResponse.json({
|
||||||
|
scripts,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default getTeamScriptsHandler;
|
||||||
Loading…
Reference in a new issue