mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 21:47:20 +00:00
For #27276 # Details This PR adds the ability to select labels when saving or editing a query in the UI, so that the query will only target hosts with those labels. It follows the API design from https://github.com/fleetdm/fleet/pull/27196, utilizing the labels_include_any and labels_exclude_any fields. The expectation is that when creating or updating a query, labels_include_any and labels_exclude_any are arrays of label names, and when fetching a single query, they are arrays of objects with a name and an id key. Other updates in this PR: * Removed colons from various headings on the Save Policy Modal and Edit Policy form * Updated the "Delete label" text * Removed "Policy runs on all hosts with these platforms." subheading underneath the platform selector * TargetLabelSelector component now has `suppressTitle` flag to turn off the "Target" title.
186 lines
6 KiB
TypeScript
186 lines
6 KiB
TypeScript
import React from "react";
|
|
import { render, screen } from "@testing-library/react";
|
|
import { noop } from "lodash";
|
|
|
|
import TargetLabelSelector from "./TargetLabelSelector";
|
|
|
|
describe("TargetLabelSelector component", () => {
|
|
describe("renders the custom target selector when the target type is 'Custom'", () => {
|
|
it("with a dropdown when there are custom options to choose from", () => {
|
|
render(
|
|
<TargetLabelSelector
|
|
selectedTargetType="Custom"
|
|
selectedCustomTarget="labelIncludeAny"
|
|
customTargetOptions={[
|
|
{ value: "labelIncludeAny", label: "Include any" },
|
|
]}
|
|
selectedLabels={{}}
|
|
labels={[
|
|
{ id: 1, name: "label 1", label_type: "regular" },
|
|
{ id: 2, name: "label 2", label_type: "regular" },
|
|
]}
|
|
onSelectCustomTarget={noop}
|
|
onSelectLabel={noop}
|
|
onSelectTargetType={noop}
|
|
/>
|
|
);
|
|
|
|
// custom target selector is rendering
|
|
expect(screen.getByRole("option", { name: "Include any" })).toBeVisible();
|
|
|
|
// lables are rendering
|
|
expect(screen.getByRole("checkbox", { name: "label 1" })).toBeVisible();
|
|
expect(screen.getByRole("checkbox", { name: "label 2" })).toBeVisible();
|
|
});
|
|
|
|
it("with an optional message and no dropdown when there are no custom options to choose from", () => {
|
|
const HELP_TEXT = "go boldly where no target has gone before";
|
|
render(
|
|
<TargetLabelSelector
|
|
selectedTargetType="Custom"
|
|
selectedCustomTarget="labelIncludeAny"
|
|
customHelpText={<span>{HELP_TEXT}</span>}
|
|
selectedLabels={{}}
|
|
labels={[
|
|
{ id: 1, name: "label 1", label_type: "regular" },
|
|
{ id: 2, name: "label 2", label_type: "regular" },
|
|
]}
|
|
onSelectCustomTarget={noop}
|
|
onSelectLabel={noop}
|
|
onSelectTargetType={noop}
|
|
/>
|
|
);
|
|
|
|
// custom target help text is visible
|
|
expect(screen.getByText(HELP_TEXT)).toBeVisible();
|
|
|
|
expect(screen.queryByRole("option")).not.toBeInTheDocument();
|
|
|
|
// lables are rendering
|
|
expect(screen.getByRole("checkbox", { name: "label 1" })).toBeVisible();
|
|
expect(screen.getByRole("checkbox", { name: "label 2" })).toBeVisible();
|
|
});
|
|
});
|
|
|
|
it("does not render the custom target selector when the target type is 'All hosts'", () => {
|
|
render(
|
|
<TargetLabelSelector
|
|
selectedTargetType="All hosts"
|
|
selectedCustomTarget="labelIncludeAny"
|
|
customTargetOptions={[
|
|
{ value: "labelIncludeAny", label: "Include any" },
|
|
]}
|
|
selectedLabels={{}}
|
|
labels={[
|
|
{ id: 1, name: "label 1", label_type: "regular" },
|
|
{ id: 2, name: "label 2", label_type: "regular" },
|
|
]}
|
|
onSelectCustomTarget={noop}
|
|
onSelectLabel={noop}
|
|
onSelectTargetType={noop}
|
|
/>
|
|
);
|
|
|
|
// custom target selector is not rendering
|
|
expect(screen.queryByRole("option", { name: "Include any" })).toBeNull();
|
|
|
|
// lables are not rendering
|
|
expect(screen.queryByRole("checkbox", { name: "label 1" })).toBeNull();
|
|
expect(screen.queryByRole("checkbox", { name: "label 2" })).toBeNull();
|
|
});
|
|
|
|
it("renders selected labels as checked", () => {
|
|
render(
|
|
<TargetLabelSelector
|
|
selectedTargetType="Custom"
|
|
selectedCustomTarget="labelIncludeAny"
|
|
customTargetOptions={[
|
|
{ value: "labelIncludeAny", label: "Include any" },
|
|
]}
|
|
selectedLabels={{ "label 1": true, "label 2": false }}
|
|
labels={[
|
|
{ id: 1, name: "label 1", label_type: "regular" },
|
|
{ id: 2, name: "label 2", label_type: "regular" },
|
|
]}
|
|
onSelectCustomTarget={noop}
|
|
onSelectLabel={noop}
|
|
onSelectTargetType={noop}
|
|
/>
|
|
);
|
|
|
|
// lables are rendering
|
|
expect(screen.getByRole("checkbox", { name: "label 1" })).toBeChecked();
|
|
expect(screen.getByRole("checkbox", { name: "label 2" })).not.toBeChecked();
|
|
});
|
|
|
|
it("sets the title to Target by default", () => {
|
|
const TITLE = "Target";
|
|
render(
|
|
<TargetLabelSelector
|
|
selectedTargetType="Custom"
|
|
selectedCustomTarget="labelIncludeAny"
|
|
customTargetOptions={[
|
|
{ value: "labelIncludeAny", label: "Include any" },
|
|
]}
|
|
selectedLabels={{}}
|
|
labels={[
|
|
{ id: 1, name: "label 1", label_type: "regular" },
|
|
{ id: 2, name: "label 2", label_type: "regular" },
|
|
]}
|
|
onSelectCustomTarget={noop}
|
|
onSelectLabel={noop}
|
|
onSelectTargetType={noop}
|
|
title={TITLE}
|
|
/>
|
|
);
|
|
|
|
expect(screen.getByText(TITLE)).toBeVisible();
|
|
});
|
|
|
|
it("allows a custom title to be passed in", () => {
|
|
const TITLE = "Choose a target";
|
|
render(
|
|
<TargetLabelSelector
|
|
selectedTargetType="Custom"
|
|
selectedCustomTarget="labelIncludeAny"
|
|
customTargetOptions={[
|
|
{ value: "labelIncludeAny", label: "Include any" },
|
|
]}
|
|
selectedLabels={{}}
|
|
labels={[
|
|
{ id: 1, name: "label 1", label_type: "regular" },
|
|
{ id: 2, name: "label 2", label_type: "regular" },
|
|
]}
|
|
onSelectCustomTarget={noop}
|
|
onSelectLabel={noop}
|
|
onSelectTargetType={noop}
|
|
title={TITLE}
|
|
/>
|
|
);
|
|
|
|
expect(screen.getByText(TITLE)).toBeVisible();
|
|
});
|
|
|
|
it("suppresses the title when suppressTitle is true", () => {
|
|
render(
|
|
<TargetLabelSelector
|
|
selectedTargetType="Custom"
|
|
selectedCustomTarget="labelIncludeAny"
|
|
customTargetOptions={[
|
|
{ value: "labelIncludeAny", label: "Include any" },
|
|
]}
|
|
selectedLabels={{}}
|
|
labels={[
|
|
{ id: 1, name: "label 1", label_type: "regular" },
|
|
{ id: 2, name: "label 2", label_type: "regular" },
|
|
]}
|
|
onSelectCustomTarget={noop}
|
|
onSelectLabel={noop}
|
|
onSelectTargetType={noop}
|
|
suppressTitle
|
|
/>
|
|
);
|
|
|
|
expect(screen.queryByText("Target")).not.toBeInTheDocument();
|
|
});
|
|
});
|