fleet/frontend/pages/labels/EditLabelPage/EditLabelPage.tests.tsx
Ian Littman 8e4e89f4e9
API + auth + UI changes for team labels (#37208)
Covers #36760, #36758.

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)

## Testing

- [x] Added/updated automated tests
- [x] Where appropriate, [automated tests simulate multiple hosts and
test for host
isolation](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/reference/patterns-backend.md#unit-testing)
(updates to one hosts's records do not affect another)

- [ ] QA'd all new/changed functionality manually
2025-12-29 21:28:45 -06:00

105 lines
3.1 KiB
TypeScript

import React from "react";
import { screen } from "@testing-library/react";
import { createCustomRenderer } from "test/test-utils";
import mockServer from "test/mock-server";
import {
getLabelHandler,
getLabelHostsHandler,
} from "test/handlers/label-handlers";
import EditLabelPage from "./EditLabelPage";
// TODO: make this a utility for other tests.
const generateMockRouterProps = (overrides?: any) => {
return {
location: {},
params: {},
route: {},
router: [],
routeParams: {},
...overrides,
};
};
describe("EditLabelPage", () => {
it("renders a message for build in labels", async () => {
mockServer.use(getLabelHandler({ label_type: "builtin" }));
const render = createCustomRenderer({ withBackendMock: true });
const routerProps = generateMockRouterProps({
routeParams: { label_id: "1" },
});
render(<EditLabelPage {...routerProps} />);
// waiting for the message to render
const builtinMessage = await screen.findByText(
"Built in labels cannot be edited"
);
expect(builtinMessage).toBeInTheDocument();
});
it("renders the DynamicLabelForm when the label is dynamic", async () => {
mockServer.use(getLabelHandler({ label_membership_type: "dynamic" }));
const render = createCustomRenderer({ withBackendMock: true });
const routerProps = generateMockRouterProps({
routeParams: { label_id: "1" },
});
render(<EditLabelPage {...routerProps} />);
// waiting for the message to render
const queryLabel = await screen.findByText("Query");
const platformLabel = await screen.findByText("Platform");
expect(queryLabel).toBeInTheDocument();
expect(platformLabel).toBeInTheDocument();
expect(
screen.getByText(/Label queries and platforms are immutable/)
).toBeInTheDocument();
});
it("renders the ManualLabelForm when the label is manual", async () => {
mockServer.use(getLabelHandler({ label_membership_type: "manual" }));
mockServer.use(
getLabelHostsHandler([
{
hostname: "hosty numero uno",
display_name: "Test host #1",
team_id: 2,
team_name: "Mobile",
platform: "ios",
os_version: "iOS 14.7.1",
hardware_serial: "test-serial-1",
},
{
hostname: "hosty numero dos",
display_name: "Test host #2",
team_id: 2,
team_name: "Mobile",
platform: "ios",
os_version: "iOS 14.7.1",
hardware_serial: "test-serial-2",
},
])
);
const render = createCustomRenderer({ withBackendMock: true });
const routerProps = generateMockRouterProps({
routeParams: { label_id: "1" },
});
render(<EditLabelPage {...routerProps} />);
// waiting for the message to render
const selectHostsLabel = await screen.findByText("Select hosts");
expect(selectHostsLabel).toBeInTheDocument();
// expect host info to be on the page
const host1 = await screen.findByText("Test host #1");
const host2 = await screen.findByText("Test host #2");
expect(host1).toBeInTheDocument();
expect(host2).toBeInTheDocument();
});
});