Skip to content

Commit

Permalink
Support loading more results in Vector search client
Browse files Browse the repository at this point in the history
This lets users scroll to load additional search results. To match the
old search more closely, we’ll also want to reduce the number of results
initially shown (should be fewer than the number of loaded results), but
that can be done separately.

Bug: T322333
Change-Id: I67fac3b209d6a1ab2661e1e1c0681edd8472ac6c
Depends-On: Iadade9cbf48457cfeabc78439624602ec3f98782
  • Loading branch information
micgro42 authored and lucaswerkmeister committed Dec 9, 2022
1 parent 95036aa commit 8457001
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
3 changes: 3 additions & 0 deletions repo/resources/wikibase.vector.searchClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
const vectorSearchClient = {
fetchByTitle: ( q, limit = 10, _showDescription = true ) => {
return fetchResults( q, limit );
},
loadMore: ( q, offset, limit = 10, _showDescription = true ) => {
return fetchResults( q, limit, offset );
}
};

Expand Down
39 changes: 31 additions & 8 deletions repo/tests/jest/wikibase.vector.searchClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ describe( 'Vector Search Client', () => {
]
} ];

it( 'test construction and fetchByTitle behavior', async () => {
beforeEach( () => {
// ensure that require( searchClient.js ) has the side-effect we want to test each time
jest.resetModules();
} );

it.each( [
[ 'fetchByTitle' ],
[ 'loadMore' ]
] )( 'test construction and %s behavior', async ( name ) => {
const fakeApiInstance = {
get: jest.fn().mockResolvedValue( {
search: mockApiResults
Expand All @@ -62,14 +70,10 @@ describe( 'Vector Search Client', () => {
const vectorSearchClient = global.mw.config.set.mock.calls[ 0 ][ 1 ];

const exampleSearchString = 'abc';
const vectorOffset = 20;
const vectorLimit = 10;

const apiController = vectorSearchClient.fetchByTitle(
exampleSearchString,
vectorLimit,
true
);
expect( fakeApiInstance.get ).toHaveBeenCalledWith( {
const expectedParams = {
action: 'wbsearchentities',
search: exampleSearchString,
limit: vectorLimit,
Expand All @@ -78,7 +82,26 @@ describe( 'Vector Search Client', () => {
type: 'item',
format: 'json',
errorformat: 'plaintext'
} );
};
let apiController;
if ( name === 'fetchByTitle' ) {
apiController = vectorSearchClient.fetchByTitle(
exampleSearchString,
vectorLimit,
true
);
} else if ( name === 'loadMore' ) {
apiController = vectorSearchClient.loadMore(
exampleSearchString,
vectorOffset,
vectorLimit,
true
);
expectedParams.continue = vectorOffset;
} else {
throw new Error( `Unexpected test name ${name}` );
}
expect( fakeApiInstance.get ).toHaveBeenCalledWith( expectedParams );

const actualTransformedResult = await apiController.fetch;
expect( actualTransformedResult ).toStrictEqual( {
Expand Down

0 comments on commit 8457001

Please sign in to comment.