Skip to content

Commit

Permalink
feat: export parseContainmentPath for use in apps
Browse files Browse the repository at this point in the history
  • Loading branch information
dbouwman committed Oct 21, 2024
1 parent 5209cd2 commit 25e848a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 44 deletions.
1 change: 0 additions & 1 deletion packages/common/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export * from "./shareEntityWithGroups";
export * from "./unshareEntityWithGroups";
export * from "./getEntityGroups";
export * from "./getEntityThumbnailUrl";
export * from "./parseContainmentPath";

// For sme reason, if these are exported here,
// they are not actually exported in the final package.
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export * from "./search/_internal/getCatalogGroups";
export * from "./search/getPredicateValues";
export * from "./core/hubHistory";
export * from "./core/deepCatalogContains";
export * from "./core/parseContainmentPath";

import OperationStack from "./OperationStack";
import OperationError from "./OperationError";
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/search/Catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class Catalog implements IHubCatalog {
* Return an array of the entity types available in this Catalog
*/
get availableScopes(): EntityType[] {
return Object.keys(this.scopes || {}) as unknown as EntityType[];
return Object.keys(this.scopes) as unknown as EntityType[];
}

/**
Expand Down
40 changes: 0 additions & 40 deletions packages/common/test/core/_internal/deepContains.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
deepContains,
parsePath,
pathToCatalogInfo,
} from "../../../src/core/_internal/deepContains";
import { ArcGISContextManager } from "../../../src/ArcGISContextManager";
Expand Down Expand Up @@ -166,45 +165,6 @@ describe("deepContains:", () => {
});
});

describe("parsePath:", () => {
// add tests for the parsePath function

it("path needs even number of parts", () => {
const path = "/sites/00b/initiatives";

const result = parsePath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe(
"Path does not contain an even number of parts."
);
});

it("path needs less than 10 parts", () => {
const path =
"sites/00a/initiatives/00b/projects/00c/content/00d/content/00c/content/00e";

const result = parsePath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe("Path is > 5 entities deep.");
});

it("path needs valid paths", () => {
const path = "sites/00a/initiatives/00b/projects/00c/wallaby/00d";

const result = parsePath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe("Path contains invalid segment: wallaby.");
});

it("good path parses clean", () => {
const path = "sites/00a/initiatives/00b/projects/00c";
const result = parsePath(path);
expect(result.valid).toBeTruthy();
expect(result.reason).toBe("");
expect(result.parts).toEqual(path.split("/"));
});
});

describe("pathToCatalogInfo:", () => {
it("converts a path string into an array of IDeepCatalogInfo objects", () => {
const path = "sites/a00a/initiatives/b00b/projects/c00c";
Expand Down
38 changes: 38 additions & 0 deletions packages/common/test/core/parseContainmentPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { parseContainmentPath } from "../../src";

describe("parseContainmentPath:", () => {
it("path needs even number of parts", () => {
const path = "/sites/00b/initiatives";

const result = parseContainmentPath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe(
"Path does not contain an even number of parts."
);
});

it("path needs less than 10 parts", () => {
const path =
"sites/00a/initiatives/00b/projects/00c/content/00d/content/00c/content/00e";

const result = parseContainmentPath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe("Path is > 5 entities deep.");
});

it("path needs valid paths", () => {
const path = "sites/00a/initiatives/00b/projects/00c/wallaby/00d";

const result = parseContainmentPath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe("Path contains invalid segment: wallaby.");
});

it("good path parses clean", () => {
const path = "sites/00a/initiatives/00b/projects/00c";
const result = parseContainmentPath(path);
expect(result.valid).toBeTruthy();
expect(result.reason).toBe("");
expect(result.parts).toEqual(path.split("/"));
});
});
33 changes: 31 additions & 2 deletions packages/common/test/search/Catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ describe("Catalog Class:", () => {
});
it("allows null scopes", () => {
const instance = Catalog.fromJson(cloneObject(noScopeCatalog), context);
expect(instance.scopes).toBeUndefined();
expect(instance.scopes).toEqual({});
expect(instance.getScope("item")).toBeUndefined();
expect(instance.collectionNames).toEqual(["teams"]);
expect(instance.availableScopes).toEqual([]);
});
it("allows null collections", () => {
const noCollectionsCatalog = cloneObject(catalogJson);
Expand Down Expand Up @@ -591,7 +592,7 @@ describe("Catalog Class:", () => {
expect(res.isContained).toBe(false);
expect(hubSearchSpy).toHaveBeenCalledTimes(0);
});
it("executes one search if entity type specified", async () => {
it("executes scope search if entity type specified and scope exists", async () => {
hubSearchSpy = spyOn(HubSearchModule, "hubSearch").and.callFake(() => {
return Promise.resolve({
results: ["results just needs to have an entry"],
Expand All @@ -617,6 +618,34 @@ describe("Catalog Class:", () => {
)
).toBe(true);
});
it("executes collection search if no scope", async () => {
hubSearchSpy = spyOn(HubSearchModule, "hubSearch").and.callFake(() => {
return Promise.resolve({
results: ["results just needs to have an entry"],
});
});
const scopelessCatalog = cloneObject(catalogJson);
delete scopelessCatalog.scopes?.item;
const instance = Catalog.fromJson(cloneObject(scopelessCatalog), context);
const res = await instance.contains("1950189b18a64ab78fc478d97ea502e0", {
entityType: "item",
});
expect(res.isContained).toBe(true);
expect(hubSearchSpy).toHaveBeenCalledTimes(1);
const chkQry = hubSearchSpy.calls.argsFor(0)[0];
const predicates: IPredicate = chkQry.filters.reduce(
(acc: IPredicate[], f: IFilter) => {
return acc.concat(f.predicates);
},
[]
);
// one predicate must have the id
expect(
predicates.some(
(p: IPredicate) => p.id === "1950189b18a64ab78fc478d97ea502e0"
)
).toBe(true);
});
it("subsequent calls use a cache", async () => {
hubSearchSpy = spyOn(HubSearchModule, "hubSearch").and.callFake(() => {
return Promise.resolve({
Expand Down

0 comments on commit 25e848a

Please sign in to comment.