fleet/frontend/utilities/url/url.tests.ts
Scott Gress 2686907dba
Update API calls in front-end to use new, non-deprecated URLs and params (#41515)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #41391

# Details

This PR updates front-end API calls to use new URLs and API params, so
that the front end doesn't cause deprecation warnings to appear on the
server.

# 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, should not be user-visible

## Testing

- [X] Added/updated automated tests
- [ ] QA'd all new/changed functionality manually
The biggest risk here is not that we missed a spot that still causes a
deprecation warning, but that we might inadvertently make a change that
breaks the front end, for instance by sending `fleet_id` to a function
that drops it silently and thus sends no ID to the server. Fortunately
we use TypeScript in virtually every place affected by these changes, so
the code would not compile if there were mismatches between the API
expectation and what we're sending. Still, spot checking as many places
as possible both for deprecation-warning leaks and loss of functionality
is important.

## Summary by CodeRabbit

* **Refactor**
* Updated API nomenclature across the application to use "fleets"
instead of "teams" and "reports" instead of "queries" in endpoint paths
and request/response payloads.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-03-12 22:26:48 -05:00

160 lines
4.4 KiB
TypeScript

import {
buildQueryStringFromParams,
getPathWithQueryParams,
reconcileMutuallyInclusiveHostParams,
} from ".";
describe("url utilites > buildQueryStringFromParams", () => {
it("creates a query string from a params object", () => {
const params = {
query: "test",
page: 1,
order: "asc",
isNew: true,
};
expect(buildQueryStringFromParams(params)).toBe(
"query=test&page=1&order=asc&isNew=true"
);
});
it("filters out undefined values", () => {
const params = {
query: undefined,
page: 1,
order: "asc",
};
expect(buildQueryStringFromParams(params)).toBe("page=1&order=asc");
});
it("filters out empty string values", () => {
const params = {
query: "",
page: 1,
order: "asc",
};
expect(buildQueryStringFromParams(params)).toBe("page=1&order=asc");
});
it("filters out null values", () => {
const params = {
query: null,
page: 1,
order: "asc",
};
expect(buildQueryStringFromParams(params)).toBe("page=1&order=asc");
});
});
describe("url utilities > getPathWithQueryParams", () => {
it("returns the endpoint when no query params are provided", () => {
expect(getPathWithQueryParams("/api/users")).toBe("/api/users");
});
it("appends query string when query params are provided", () => {
const endpoint = "/hosts/manage";
const queryParams = {
software_id: 25,
team_id: 10,
order_key: "issues",
};
expect(getPathWithQueryParams(endpoint, queryParams)).toBe(
"/hosts/manage?software_id=25&team_id=10&order_key=issues"
);
});
it("filters out undefined, null, and empty string values from query params", () => {
const endpoint = "/hosts/manage";
const queryParams = {
software_id: undefined,
team_id: null,
policy_response: "",
policy_id: 4,
};
expect(getPathWithQueryParams(endpoint, queryParams)).toBe(
"/hosts/manage?policy_id=4"
);
});
it("returns only the endpoint when all query params are filtered out", () => {
const endpoint = "/hosts/manage";
const queryParams = {
software_id: undefined,
team_id: null,
policy_response: "",
};
expect(getPathWithQueryParams(endpoint, queryParams)).toBe("/hosts/manage");
});
});
describe("url utilities > reconcileMutuallyInclusiveHostParams", () => {
it("leaves macSettingsStatus and teamId unchanged when both are present", () => {
const [macSettingsStatus, teamId] = ["pending" as const, 1];
expect(
reconcileMutuallyInclusiveHostParams({ macSettingsStatus, teamId })
).toEqual({
macos_settings: "pending",
fleet_id: 1,
});
});
it("leaves macSettingsStatus and teamId unchanged when both are present, teamId=0", () => {
const [macSettingsStatus, teamId] = ["pending" as const, 0];
expect(
reconcileMutuallyInclusiveHostParams({
macSettingsStatus,
teamId,
})
).toEqual({
macos_settings: "pending",
fleet_id: 0,
});
});
it("adds fleet_id: 0 when macSettingsStatus is present and teamId is not", () => {
const [macSettingsStatus, teamId] = ["pending" as const, undefined];
expect(
reconcileMutuallyInclusiveHostParams({
macSettingsStatus,
teamId,
})
).toEqual({ macos_settings: "pending", fleet_id: 0 });
});
it("does not add macos_settings when teamId is present and macSettingsStatus is not", () => {
const [macSettingsStatus, teamId] = [undefined, 1];
expect(
reconcileMutuallyInclusiveHostParams({ macSettingsStatus, teamId })
).toEqual({
fleet_id: 1,
});
});
it("adds nothing when neither macSettingsStatus nor teamId are present", () => {
const [macSettingsStatus, teamId] = [undefined, undefined];
expect(
reconcileMutuallyInclusiveHostParams({ macSettingsStatus, teamId })
).toEqual({});
});
it("leaves teamId unchanged and excludes others if label is present", () => {
expect(
reconcileMutuallyInclusiveHostParams({
teamId: 1,
label: "labels/7",
macSettingsStatus: "pending",
osSettings: "pending",
})
).toEqual({
fleet_id: 1,
});
expect(
reconcileMutuallyInclusiveHostParams({
label: "labels/7",
macSettingsStatus: "pending",
osSettings: "pending",
})
).toEqual({
fleet_id: undefined,
});
});
});