From 845700125d900ec7379d72edd12ff633ad98e226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Gro=C3=9Fe?= Date: Mon, 14 Nov 2022 16:04:30 +0100 Subject: [PATCH] Support loading more results in Vector search client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../resources/wikibase.vector.searchClient.js | 3 ++ .../jest/wikibase.vector.searchClient.spec.js | 39 +++++++++++++++---- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/repo/resources/wikibase.vector.searchClient.js b/repo/resources/wikibase.vector.searchClient.js index 7fbbd443c97..72f4edaba11 100644 --- a/repo/resources/wikibase.vector.searchClient.js +++ b/repo/resources/wikibase.vector.searchClient.js @@ -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 ); } }; diff --git a/repo/tests/jest/wikibase.vector.searchClient.spec.js b/repo/tests/jest/wikibase.vector.searchClient.spec.js index 84eba4c6902..8ce27e1455b 100644 --- a/repo/tests/jest/wikibase.vector.searchClient.spec.js +++ b/repo/tests/jest/wikibase.vector.searchClient.spec.js @@ -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 @@ -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, @@ -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( {