fleet/frontend/pages/SoftwarePage/SoftwareAddPage/helpers.tests.tsx
Martin Angers 915408c2a8
IPA: validate conflicts with other installers, return proper error (#38005)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #36621

# 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/guides/committing-changes.md#changes-files)
for more information.

- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)

## Testing

- [x] Added/updated automated tests
- [x] Where appropriate, [automated tests simulate multiple hosts and
test for host
isolation](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/reference/patterns-backend.md#unit-testing)
(updates to one hosts's records do not affect another)

- [x] QA'd all new/changed functionality manually
See
https://github.com/fleetdm/fleet/issues/36621#issuecomment-3740340604

---------

Co-authored-by: Jonathan Katz <44128041+jkatz01@users.noreply.github.com>
Co-authored-by: Carlo DiCelico <carlo@fleetdm.com>
2026-01-13 10:30:03 -05:00

77 lines
2.8 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 team."
const msg = `${ADD_SOFTWARE_ERROR_PREFIX} MyApp already has an installer available for the Marketing team.`;
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 team");
});
it("returns React with correct text and team when the string matches the package exists regex", () => {
const msg = `SoftwareInstaller "MyApp" already exists with team "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");
});
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 team names", () => {
const msg = `${ADD_SOFTWARE_ERROR_PREFIX} Zoom already has an installer available for the Engineering team.`;
const result = formatAlreadyAvailableInstallMessage(msg);
const { container } = render(<>{result}</>);
expect(container.textContent).toContain("Zoom");
expect(container.textContent).toContain("Engineering team");
});
it("returns null if the input is empty", () => {
expect(formatAlreadyAvailableInstallMessage("")).toBeNull();
});
});