Skip to content

Commit

Permalink
fix(): findItemsBySlug short-circuits to resolve empty array when slu…
Browse files Browse the repository at this point in the history
…g is falsey (#1420)
  • Loading branch information
rweber-esri authored Feb 22, 2024
1 parent 85469a8 commit f255f6e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
48 changes: 18 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions packages/common/src/items/slugs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ export function getItemBySlug(
* @param requestOptions
* @returns
*/
export function findItemsBySlug(
export async function findItemsBySlug(
slugInfo: {
slug: string;
exclude?: string;
},
requestOptions: IRequestOptions
): Promise<IItem[]> {
if (!slugInfo.slug) {
return [];
}
const filter = slugInfo.slug.startsWith(`${TYPEKEYWORD_SLUG_PREFIX}|`)
? slugInfo.slug
: [TYPEKEYWORD_SLUG_PREFIX, slugInfo.slug].join("|");
Expand All @@ -133,13 +136,12 @@ export function findItemsBySlug(
if (slugInfo.exclude) {
opts.q = `NOT id:${slugInfo.exclude}`;
}
return searchItems(opts)
.then((response) => {
return response.results;
})
.catch((e) => {
throw e;
});
try {
const response = await searchItems(opts);
return response.results;
} catch (e) {
throw e;
}
}

/**
Expand Down
15 changes: 14 additions & 1 deletion packages/common/test/items/slugs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ describe("slug utils: ", () => {
});

describe("findItemsBySlug:", () => {
it("short-circuits with an empty array when slugInfo.slug is falsey", async () => {
const searchSpy = spyOn(portalModule, "searchItems").and.returnValue(
Promise.resolve({ results: [] })
);

const results = await slugModule.findItemsBySlug(
{ slug: "", exclude: "bc3" },
{
authentication: MOCK_AUTH,
}
);
expect(results).toEqual([]);
expect(searchSpy.calls.count()).toBe(0);
});
it("excludes specific item", async () => {
const searchSpy = spyOn(portalModule, "searchItems").and.returnValue(
Promise.resolve({
Expand All @@ -166,7 +180,6 @@ describe("slug utils: ", () => {
}
);
expect(results[0].id).toBe("3ef");
// check if
expect(searchSpy.calls.count()).toBe(1);
const args = searchSpy.calls.argsFor(0)[0] as unknown as ISearchOptions;
expect(args.filter).toBe(`typekeywords:"slug|foo-bar"`);
Expand Down

0 comments on commit f255f6e

Please sign in to comment.