Skip to content

Commit

Permalink
support depth in getNavigationQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrehault committed Oct 12, 2024
1 parent 78ae6dd commit e6c50d5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
17 changes: 17 additions & 0 deletions packages/client/src/restapi/navigation/get.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,21 @@ describe('[GET] Navigation', () => {

expect(result.current.error).toBeDefined();
});

test('Depth parameter', async () => {
const path = '/';
const depth = 3;
const { result } = renderHook(
() => useQuery(getNavigationQuery({ path, depth })),
{
wrapper: createWrapper(),
},
);

await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect(result.current.data?.['@id']).toBe(

Check failure on line 55 in packages/client/src/restapi/navigation/get.test.tsx

View workflow job for this annotation

GitHub Actions / @plone/client

src/restapi/navigation/get.test.tsx > [GET] Navigation > Depth parameter

AssertionError: expected 'http://localhost:55001/plone/@navigat…' to be 'http://localhost:55001/plone/@navigat…' // Object.is equality - Expected + Received - http://localhost:55001/plone/@navigation?expand.navigation.depth=3 + http://localhost:55001/plone/@navigation ❯ src/restapi/navigation/get.test.tsx:55:42

Check failure on line 55 in packages/client/src/restapi/navigation/get.test.tsx

View workflow job for this annotation

GitHub Actions / @plone/client

src/restapi/navigation/get.test.tsx > [GET] Navigation > Depth parameter

AssertionError: expected 'http://localhost:55001/plone/@navigat…' to be 'http://localhost:55001/plone/@navigat…' // Object.is equality Expected: "http://localhost:55001/plone/@navigation?expand.navigation.depth=3" Received: "http://localhost:55001/plone/@navigation" ❯ src/restapi/navigation/get.test.tsx:55:42
'http://localhost:55001/plone/@navigation?expand.navigation.depth=3',
);
});
});
18 changes: 14 additions & 4 deletions packages/client/src/restapi/navigation/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { z } from 'zod';

const getNavigationSchema = z.object({
path: z.string(),
depth: z.number().optional(),
});

export type NavigationArgs = z.infer<typeof getNavigationSchema> & {
Expand All @@ -13,23 +14,32 @@ export type NavigationArgs = z.infer<typeof getNavigationSchema> & {

export const getNavigation = async ({
path,
depth,
config,
}: NavigationArgs): Promise<NavigationResponse> => {
const validatedArgs = getNavigationSchema.parse({
path,
depth,
});

const options: ApiRequestParams = {
config,
params: {},
};

const navigationPath = `${validatedArgs.path}/@navigation`;
let navigationPath = `${validatedArgs.path}/@navigation`;
if (validatedArgs.depth) {
navigationPath += `?expand.navigation.depth=${validatedArgs.depth}`;
}

return apiRequest('get', navigationPath, options);
};

export const getNavigationQuery = ({ path, config }: NavigationArgs) => ({
queryKey: [path, 'get', 'navigation'],
queryFn: () => getNavigation({ path, config }),
export const getNavigationQuery = ({
path,
depth,
config,
}: NavigationArgs) => ({
queryKey: [path, depth, 'get', 'navigation'],
queryFn: () => getNavigation({ path, depth, config }),
});

0 comments on commit e6c50d5

Please sign in to comment.