fleet/frontend/components/TeamsDropdown/TeamsDropdown.tests.tsx
jacobshandling f0d3809b22
UI: Allow editing the name and team of a "Save as new" query (#30544)
## #14801 
### [Demo
video](https://drive.google.com/file/d/1Lovk7iwvgUv1NpfsqSt-Is0yTBt0SZ5O/view?usp=sharing)
<img width="1624" alt="Screenshot 2025-07-02 at 4 58 33 PM"
src="https://github.com/user-attachments/assets/86c7b214-e8e4-4e58-9969-b1373ed97691"
/>


* **New Features**
* Added the ability to select a team and update the name when saving a
query as a new copy, using a dedicated modal dialog.

* **Improvements**
* Enhanced the team selection dropdown with new styling options and
clarified prop names.
* Updated query editing workflow to use a modal for "Save as new"
actions.
* Improved type safety and clarity in several interfaces and utility
functions.

* **Bug Fixes**
  * Fixed inconsistencies in prop naming for team dropdown components.
* Ensured "Discard data" setting is maintained when "Save as new"ing a
query - it was previously not maintained correctly

* **Tests**
* Updated and removed tests to align with the new "Save as new" query
workflow and prop changes.
  * Added utilities for creating mock location objects in tests.

* **Style**
  * Added a new light grey color to the UI color palette.

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

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2025-07-03 13:11:06 -07:00

88 lines
2.5 KiB
TypeScript

import React from "react";
import { render, screen } from "@testing-library/react";
import { noop } from "lodash";
// TODOL Replace renderWithAppContext with createCustomRenderer
import { renderWithAppContext } from "test/test-utils";
import { APP_CONTEXT_NO_TEAM_ID } from "interfaces/team";
import TeamsDropdown from "./TeamsDropdown";
describe("TeamsDropdown - component", () => {
const USER_TEAMS = [
{ id: -1, name: "All teams" },
{ id: 1, name: "Team 1" },
{ id: 2, name: "Team 2" },
];
it("renders the given selected team from selectedTeamId", () => {
render(
<TeamsDropdown
currentUserTeams={USER_TEAMS}
selectedTeamId={1}
onChange={noop}
/>
);
const selectedTeam = screen.getByText("Team 1");
expect(selectedTeam).toBeInTheDocument();
});
it("renders the first team option when includeAllTeams is false and when no selectedTeamId is given", () => {
render(
<TeamsDropdown
currentUserTeams={USER_TEAMS}
includeAllTeams={false}
onChange={noop}
/>
);
const selectedTeam = screen.getByText("Team 1");
expect(selectedTeam).toBeInTheDocument();
});
describe("user is on the global team", () => {
const contextValue = {
isOnGlobalTeam: true,
};
it("renders 'All teams' when no selectedTeamId is given", () => {
renderWithAppContext(
<TeamsDropdown currentUserTeams={USER_TEAMS} onChange={noop} />,
{ contextValue }
);
const selectedTeam = screen.getByText("All teams");
expect(selectedTeam).toBeInTheDocument();
});
it("renders the first team option when includeAllTeams is false and when no selectedTeamId is given", () => {
renderWithAppContext(
<TeamsDropdown
currentUserTeams={USER_TEAMS}
includeAllTeams={false}
onChange={noop}
/>,
{ contextValue }
);
const selectedTeam = screen.getByText("Team 1");
expect(selectedTeam).toBeInTheDocument();
});
});
describe("user is not on the global team", () => {
const contextValue = { isOnGlobalTeam: false };
const filteredUserTeams = USER_TEAMS.filter(
(t) => t.id > APP_CONTEXT_NO_TEAM_ID
);
it("renders the first team when no selectedTeamId is given", () => {
renderWithAppContext(
<TeamsDropdown currentUserTeams={filteredUserTeams} onChange={noop} />,
{ contextValue }
);
expect(screen.getByText("Team 1")).toBeInTheDocument();
});
});
});