Skip to content

Commit

Permalink
feat(operations): add tests for getProjectName
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryanporwal authored and edlerd committed Oct 13, 2023
1 parent 76a3dd7 commit 4c0ce92
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/util/operations.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInstanceName } from "./operations";
import { getInstanceName, getProjectName } from "./operations";
import { LxdOperation } from "types/operation";

const craftOperation = (url: string) => {
Expand Down Expand Up @@ -44,3 +44,30 @@ describe("getInstanceName", () => {
expect(name).toBe("testInstance3");
});
});

describe("getProjectName", () => {
it("identifies project name from an instance operation when no project parameter is present", () => {
const operation = craftOperation("/1.0/instances/testInstance1");
const name = getProjectName(operation);

expect(name).toBe("default");
});

it("identifies project name from an instance operation in a custom project", () => {
const operation = craftOperation(
"/1.0/instances/testInstance2?project=fooProject"
);
const name = getProjectName(operation);

expect(name).toBe("fooProject");
});

it("identifies project name from an instance operation in a custom project with other parameters", () => {
const operation = craftOperation(
"/1.0/instances/testInstance2?foo=bar&project=barProject"
);
const name = getProjectName(operation);

expect(name).toBe("barProject");
});
});
6 changes: 4 additions & 2 deletions src/util/operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export const getInstanceName = (operation: LxdOperation): string => {
export const getProjectName = (operation: LxdOperation): string => {
// the url can be
// /1.0/instances/<instance_name>?project=<project_name>
// /1.0/instances/<instance_name>?other=params&project=<project_name>
// /1.0/instances/testInstance1?other=params&project=<project_name>&other=params
// when no project parameter is present, the project will be "default"

return (
operation.resources?.instances
?.filter((item) => item.startsWith("/1.0/instances/"))
.map((item) => item.split("/")[3])
.map((item) => item.split("project=")[1])
.pop()
?.split("=")[1] ?? "default"
?.split("&")[0] ?? "default"
);
};

0 comments on commit 4c0ce92

Please sign in to comment.