Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getQuerystringSearchQuery doesn't accept path as an input #6301

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions packages/client/src/restapi/querystring-search/get.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ import { renderHook, waitFor } from '@testing-library/react';
import { createWrapper } from '../../testUtils';
import { useQuery } from '@tanstack/react-query';
import { setup, teardown } from '../../resetFixture';
import { beforeEach } from 'vitest';
import { expect, test } from 'vitest';
import { beforeEach, afterEach, describe, test, expect } from 'vitest';
import PloneClient from '../../client';
import { v4 as uuid } from 'uuid';
import { createContent } from '../content/add';

const cli = PloneClient.initialize({
apiPath: 'http://localhost:55001/plone',
});

const { login, getQuerystringSearchQuery } = cli;
await login({ username: 'admin', password: 'secret' });

beforeEach(async () => {
await setup();
await login({ username: 'admin', password: 'secret' });
});

afterEach(async () => {
Expand All @@ -30,7 +28,6 @@ describe('[GET] QuerystringSearch', () => {
'@type': 'Document',
title: `get-search${randomId}`,
};

await createContent({ path: '/', data: contentData, config: cli.config });

const querystringSearchData = {
Expand All @@ -54,7 +51,41 @@ describe('[GET] QuerystringSearch', () => {
);

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

expect(result.current.data?.items_total).toBeGreaterThan(0);
});

test('Hook - Successful - with path', async () => {
const randomId = uuid();
const contentData = {
'@type': 'Document',
title: `get-search${randomId}`,
};
const path = '/test-folder';
await createContent({ path: '/', data: { '@type': 'Folder', id: 'test-folder' }, config: cli.config });
await createContent({ path, data: contentData, config: cli.config });

const querystringSearchData = {
query: [
{
i: 'portal_type',
o: 'plone.app.querystring.operation.selection.any',
v: ['Document'],
},
],
};

const { result } = renderHook(
() =>
useQuery(
getQuerystringSearchQuery({ query: querystringSearchData.query, path }),
),
{
wrapper: createWrapper(),
},
);

await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.data?.items_total).toBe(1);
expect(result.current.data?.items[0]['@id']).toContain(path);
});
});
16 changes: 10 additions & 6 deletions packages/client/src/restapi/querystring-search/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,38 @@ export type QuerystringSearchArgs = z.infer<
typeof getQuerystringSearchSchema
> & {
config: PloneClientConfig;
path?: string;
};

export const getQuerystringSearch = async ({
query,
config,
path, // Add path parameter
}: QuerystringSearchArgs): Promise<GetQuerystringSearchResponse> => {
const validatedArgs = getQuerystringSearchSchema.parse({
query,
path, // Include path in validation
});

const queryObject = { query: validatedArgs.query };
const queryObject = {
query: validatedArgs.query,
path: validatedArgs.path, // Include path in queryObject if it exists
};
const querystring = JSON.stringify(queryObject);
const encodedQuery = encodeURIComponent(querystring);

const options: ApiRequestParams = {
config,
params: {
...(encodedQuery && { query: encodedQuery }),
},
};

return apiRequest('get', '/@querystring-search', options);
};

export const getQuerystringSearchQuery = ({
query,
config,
path,
}: QuerystringSearchArgs) => ({
queryKey: ['get', 'querystringSearch'],
queryFn: () => getQuerystringSearch({ query, config }),
queryKey: ['get', 'querystringSearch', path],
queryFn: () => getQuerystringSearch({ query, config, path }),
});
1 change: 1 addition & 0 deletions packages/client/src/validation/querystring-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export const querystringSearchDataSchema = z.object({
sort_order: z.string().optional(),
fullobjects: z.boolean().optional(),
query: z.array(query),
path: z.string().optional(),
});
1 change: 1 addition & 0 deletions packages/types/news/6254.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions packages/volto/news/6254.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added optional 'path' parameter to querystringSearch functionality. This enables scoped searches,
allowing for more efficient and context-specific queries. Updated tests and schema to support
this new feature. @askadityapandey