mirror of
https://github.com/fleetdm/fleet
synced 2026-05-09 02:01:09 +00:00
For #26649 # Checklist for submitter - [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/Committing-Changes.md#changes-files) for more information. ## 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/26589, utilizing the `labels_include_any` field. The expectation is that when creating or updating a query, `labels_include_any` is an array of label names, and when fetching a single query, `labels_include_any` is an array of objects with a `name` and an `id` key. As part of this work the `TargetLabelSelector` component is updated to allow it to show a message in place of the dropdown when there are no custom options (e.g. "include any", "include all", "exclude any") to choose from.
115 lines
4 KiB
TypeScript
115 lines
4 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();
|
|
});
|
|
});
|