fleet/frontend/pages/labels/ManageLabelsPage/LabelsTable/LabelsTable.tests.tsx
jacobshandling 7085a731d6
UI: Labels page (#33079)
## For #29721 
- Build the new Labels page
- Forward to the Labels page after saving a label

### [Demo
video](https://drive.google.com/file/d/1iArnSiVn7CSwOpCrKEdO9HByHu9qga3L/view?usp=sharing)

<img width="1798" height="1082" alt="Screenshot 2025-09-17 at 4 00
55 PM"
src="https://github.com/user-attachments/assets/6a51f48c-07c3-44d9-b2bf-07025ffa61ed"
/>



- [x] Changes file added for user-visible changes in `changes/`
- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2025-09-18 09:38:45 -07:00

91 lines
2.8 KiB
TypeScript

import React from "react";
import { screen } from "@testing-library/react";
import { noop } from "lodash";
import createMockUser from "__mocks__/userMock";
import { createMockLabel } from "__mocks__/labelsMock";
import { createCustomRenderer } from "test/test-utils";
import LabelsTable from "./LabelsTable";
describe("LabelsTable", () => {
it("Renders empty state when only builtin labels are provided", () => {
const builtinLabels = [
createMockLabel({ id: 1, name: "All hosts", label_type: "builtin" }),
createMockLabel({ id: 2, name: "macOS", label_type: "builtin" }),
createMockLabel({ id: 3, name: "Ubuntu", label_type: "builtin" }),
];
const mockUser = createMockUser();
const render = createCustomRenderer();
render(
<LabelsTable
labels={builtinLabels}
onClickAction={noop}
currentUser={mockUser}
/>
);
expect(screen.getByText("No labels")).toBeInTheDocument();
expect(
screen.getByText("Labels you create will appear here.")
).toBeInTheDocument();
expect(screen.queryByText("All hosts")).not.toBeInTheDocument();
expect(screen.queryByText("macOS")).not.toBeInTheDocument();
expect(screen.queryByText("Ubuntu")).not.toBeInTheDocument();
});
it("Renders only custom labels when custom and builtin labels are provided", () => {
const labels = [
createMockLabel({
id: 1,
name: "All hosts",
label_type: "builtin",
}),
createMockLabel({
id: 2,
name: "Custom label 1",
label_type: "regular",
description: "First custom label",
}),
createMockLabel({
id: 3,
name: "macOS",
label_type: "builtin",
}),
createMockLabel({
id: 4,
name: "Custom label 2",
label_type: "regular",
description: "Second custom label",
}),
];
const mockUser = createMockUser();
const render = createCustomRenderer();
render(
<LabelsTable
labels={labels}
onClickAction={noop}
currentUser={mockUser}
/>
);
// Custom labels should be visible
expect(screen.getByText("Custom label 1")).toBeInTheDocument();
expect(screen.getByText("Custom label 2")).toBeInTheDocument();
expect(screen.getByText("First custom label")).toBeInTheDocument();
expect(screen.getByText("Second custom label")).toBeInTheDocument();
// Builtin labels should not be visible
expect(screen.queryByText("All hosts")).not.toBeInTheDocument();
expect(screen.queryByText("macOS")).not.toBeInTheDocument();
// Table headers should be visible
expect(screen.getByText("Name")).toBeInTheDocument();
expect(screen.getByText("Description")).toBeInTheDocument();
expect(screen.getByText("Type")).toBeInTheDocument();
});
});