diff --git a/plugins/search-react/src/context/SearchContext.test.tsx b/plugins/search-react/src/context/SearchContext.test.tsx index 0272176150..68c79d3a5e 100644 --- a/plugins/search-react/src/context/SearchContext.test.tsx +++ b/plugins/search-react/src/context/SearchContext.test.tsx @@ -108,40 +108,58 @@ describe('SearchContext', () => { expect(result.current).toEqual(expect.objectContaining(initialState)); }); - it('Resets cursor when term is set (and different from previous)', async () => { - const { result, waitForNextUpdate } = renderHook(() => useSearch(), { - wrapper, - initialProps: { - initialState: { - ...initialState, - pageCursor: 'SOMEPAGE', + describe('Resets cursor', () => { + it('When term is cleared', async () => { + const { result, waitForNextUpdate } = renderHook(() => useSearch(), { + wrapper, + initialProps: { + initialState: { + ...initialState, + term: 'first term', + pageCursor: 'SOMEPAGE', + }, }, - }, + }); + + await waitForNextUpdate(); + + expect(result.current.term).toEqual('first term'); + expect(result.current.pageCursor).toEqual('SOMEPAGE'); + + act(() => { + result.current.setTerm(''); + }); + + await waitForNextUpdate(); + + expect(result.current.pageCursor).toBeUndefined(); }); - await waitForNextUpdate(); + it('When term is set (and different from previous)', async () => { + const { result, waitForNextUpdate } = renderHook(() => useSearch(), { + wrapper, + initialProps: { + initialState: { + ...initialState, + term: 'first term', + pageCursor: 'SOMEPAGE', + }, + }, + }); - expect(result.current.pageCursor).toEqual('SOMEPAGE'); + await waitForNextUpdate(); - act(() => { - result.current.setTerm('first term'); + expect(result.current.term).toEqual('first term'); + expect(result.current.pageCursor).toEqual('SOMEPAGE'); + + act(() => { + result.current.setTerm('second term'); + }); + + await waitForNextUpdate(); + + expect(result.current.pageCursor).toBeUndefined(); }); - - act(() => { - result.current.setPageCursor('OTHERPAGE'); - }); - - await waitForNextUpdate(); - - expect(result.current.pageCursor).toEqual('OTHERPAGE'); - - act(() => { - result.current.setTerm('second term'); - }); - - await waitForNextUpdate(); - - expect(result.current.pageCursor).toEqual(undefined); }); describe('Performs search (and sets results)', () => { diff --git a/plugins/search-react/src/context/SearchContext.tsx b/plugins/search-react/src/context/SearchContext.tsx index 8a0f962121..faad50f37a 100644 --- a/plugins/search-react/src/context/SearchContext.tsx +++ b/plugins/search-react/src/context/SearchContext.tsx @@ -150,10 +150,11 @@ export const SearchContextProvider = (props: SearchContextProviderProps) => { useEffect(() => { // Any time a term is reset, we want to start from page 0. - if (term && prevTerm && term !== prevTerm) { + // Only reset the term if it has been modified by the user at least once, the initial state must not reset the term. + if (prevTerm !== undefined && term !== prevTerm) { setPageCursor(undefined); } - }, [term, prevTerm, initialState.pageCursor]); + }, [term, prevTerm, setPageCursor]); const value: SearchContextValue = { result,