fleet/frontend/pages/SoftwarePage/SoftwareAddPage/helpers.tests.tsx
Scott Gress 2747c96308
Fix software installer error team -> fleet (#41070)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** For #41031

# Details

* Updates server-side error message about software installers to use
"fleet" instead of "team".
* Update front-end code that rewrites that error text 🤦  

# Checklist for submitter

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

- [ ] 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.
n/a

## Testing

- [X] Added/updated automated tests
- [X] QA'd all new/changed functionality manually
- [X] Saw correct error banner when trying to add a VPP app that
conflicted with an FMA
<img width="741" height="67" alt="image"
src="https://github.com/user-attachments/assets/d171097c-b165-45f8-bafb-fd6337c94cb9"
/>
- [X] Saw correct error banner when trying to add a script with the same
contents as a another script
<img width="765" height="60" alt="image"
src="https://github.com/user-attachments/assets/db02b92a-942d-448d-9062-3fca49132a94"
/>

I haven't tested all the other cases but I think these two cover them;
one uses the `CantAddSoftwareConflictMessage` constant on the server and
one uses a hard-coded message. Everything else uses the constant.

For unreleased bug fixes in a release candidate, one of:

- [X] Confirmed that the fix is not expected to adversely impact load
test results
2026-03-05 17:28:52 -06:00

77 lines
2.9 KiB
TypeScript

import React from "react";
import { render } from "@testing-library/react";
import {
ensurePeriod,
formatAlreadyAvailableInstallMessage,
ADD_SOFTWARE_ERROR_PREFIX,
} from "./helpers"; // Adjust path as needed
// --- ensurePeriod tests ---
describe("ensurePeriod", () => {
it("adds a period to a string that doesn't end with a period", () => {
expect(ensurePeriod("Test string")).toBe("Test string.");
});
it("returns the original string if it already ends with a period", () => {
expect(ensurePeriod("Test string.")).toBe("Test string.");
});
it("returns an empty string unchanged", () => {
expect(ensurePeriod("")).toBe("");
});
it("returns the original string if the string is only a period", () => {
expect(ensurePeriod(".")).toBe(".");
});
});
// --- formatAlreadyAvailableInstallMessage tests ---
describe("formatAlreadyAvailableInstallMessage", () => {
it("returns a React fragment with the correct text and team when the string matches the regex", () => {
// Example input: "Couldn't add. MyApp already has an installer available for the Marketing fleet."
const msg = `${ADD_SOFTWARE_ERROR_PREFIX} MyApp already has an installer available for the Marketing fleet.`;
const result = formatAlreadyAvailableInstallMessage(msg);
// Render for querying text
const { container } = render(<>{result}</>);
expect(container.textContent).toContain("Couldn't add.");
expect(container.textContent).toContain("MyApp");
expect(container.textContent).toContain("Marketing fleet");
});
it("returns React with correct text and fleet when the string matches the package exists regex", () => {
const msg = `SoftwareInstaller "MyApp" already exists with fleet "Marketing".`;
const result = formatAlreadyAvailableInstallMessage(msg);
const { container } = render(<>{result}</>);
expect(container.textContent).toContain("Couldn't add.");
expect(container.textContent).toContain("MyApp");
expect(container.textContent).toContain(
"already has an installer available"
);
expect(container.textContent).toContain("Marketing fleet");
});
it("returns null if the string does not match the expected pattern", () => {
const msg = "Random error message not matching pattern";
const result = formatAlreadyAvailableInstallMessage(msg);
expect(result).toBeNull();
});
it("works for different app names and fleet names", () => {
const msg = `${ADD_SOFTWARE_ERROR_PREFIX} Zoom already has an installer available for the Engineering fleet.`;
const result = formatAlreadyAvailableInstallMessage(msg);
const { container } = render(<>{result}</>);
expect(container.textContent).toContain("Zoom");
expect(container.textContent).toContain("Engineering fleet");
});
it("returns null if the input is empty", () => {
expect(formatAlreadyAvailableInstallMessage("")).toBeNull();
});
});