Files
fleet/frontend/context/app.tests.tsx
T
Andrew Mellor 8e0c038eff 47701 abm errors UI (#49896)
**Related issue:** Resolves #47701

# Checklist for submitter

## Testing

- [x] Added/updated automated tests

- [ ] QA'd all new/changed functionality manually: Not able to test
token_rejected, terms and conditions or apple server error.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added an Apple Business Manager invalid-token warning banner that
lists the affected organization names.
* Introduced dedicated invalid-token state support so the banner can
appear with the correct priority.
* **Bug Fixes**
* Ensured invalid-token state is cleared when no tokens are returned and
consistently set on token fetch success/error.
* Improved Apple/DEP status messaging and made profile-assignment
rendering more resilient for non-DEP and partial-error responses.
* **Style**
* Adjusted banner spacing and added styling for DEP error presentation.
* **Tests**
* Added/expanded tests for invalid-token messaging, ABM expiry updates,
and MDM status/error scenarios.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-28 15:01:48 +01:00

114 lines
3.1 KiB
TypeScript

import React, { useContext } from "react";
import { screen } from "@testing-library/react";
import { renderWithSetup } from "test/test-utils";
import createMockUser from "__mocks__/userMock";
import AppProvider, { AppContext, sortAvailableTeams } from "./app";
describe("sortAvailableTeams", () => {
it("places Unassigned last for global team users", () => {
const teams = [
{ id: 0, name: "Unassigned" },
{ id: 2, name: "Zebra" },
{ id: 1, name: "Alpha" },
{ id: -1, name: "All fleets" },
];
const result = sortAvailableTeams(teams, createMockUser());
expect(result.map((t) => t.name)).toEqual([
"All fleets",
"Alpha",
"Zebra",
"Unassigned",
]);
});
it("does not include All fleets or Unassigned for non-global users", () => {
const teams = [
{ id: 0, name: "Unassigned" },
{ id: 2, name: "Zebra" },
{ id: 1, name: "Alpha" },
{ id: -1, name: "All fleets" },
];
const result = sortAvailableTeams(
teams,
createMockUser({ global_role: null })
);
expect(result.map((t) => t.name)).toEqual(["Alpha", "Zebra"]);
});
it("sorts named teams alphabetically (case-insensitive)", () => {
const teams = [
{ id: 3, name: "charlie" },
{ id: 1, name: "Alpha" },
{ id: 2, name: "Bravo" },
];
const result = sortAvailableTeams(
teams,
createMockUser({ global_role: null })
);
expect(result.map((t) => t.name)).toEqual(["Alpha", "Bravo", "charlie"]);
});
});
const AbmExpiryConsumer = () => {
const {
hasInvalidABMToken,
invalidAbmTokenOrgNames,
setABMExpiry,
} = useContext(AppContext);
return (
<div>
<button
type="button"
onClick={() =>
setABMExpiry({
earliestExpiry: "",
needsAbmTermsRenewal: false,
hasInvalidABMToken: true,
invalidAbmTokenOrgNames: [
"Acme Inc.",
"Fleet Device Management Inc.",
],
})
}
>
Set invalid tokens
</button>
<div data-testid="has-invalid">{String(hasInvalidABMToken)}</div>
<div data-testid="org-names">{invalidAbmTokenOrgNames.join(", ")}</div>
</div>
);
};
describe("AppProvider - setABMExpiry", () => {
it("defaults hasInvalidABMToken to false and invalidAbmTokenOrgNames to an empty list", () => {
renderWithSetup(
<AppProvider>
<AbmExpiryConsumer />
</AppProvider>
);
expect(screen.getByTestId("has-invalid")).toHaveTextContent("false");
expect(screen.getByTestId("org-names")).toHaveTextContent("");
});
it("updates hasInvalidABMToken and invalidAbmTokenOrgNames when setABMExpiry is called", async () => {
const { user } = renderWithSetup(
<AppProvider>
<AbmExpiryConsumer />
</AppProvider>
);
await user.click(
screen.getByRole("button", { name: "Set invalid tokens" })
);
expect(screen.getByTestId("has-invalid")).toHaveTextContent("true");
expect(screen.getByTestId("org-names")).toHaveTextContent(
"Acme Inc., Fleet Device Management Inc."
);
});
});