From 7507056511d40d84b5ce8c0dcce312f6113c4b9e Mon Sep 17 00:00:00 2001 From: Agata Drapala Date: Mon, 17 Feb 2025 11:42:48 +0100 Subject: [PATCH] testing two non-empty terms Signed-off-by: Agata Drapala --- .../src/context/SearchContext.test.tsx | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/plugins/search-react/src/context/SearchContext.test.tsx b/plugins/search-react/src/context/SearchContext.test.tsx index 796eca351f..6eb60e4d0c 100644 --- a/plugins/search-react/src/context/SearchContext.test.tsx +++ b/plugins/search-react/src/context/SearchContext.test.tsx @@ -445,31 +445,41 @@ describe('SearchContext', () => { .mockResolvedValueOnce({ results: [], numberOfResults: 3, + }) + .mockResolvedValueOnce({ + results: [], + numberOfResults: 1, }); + // first api call with empty term const { result } = renderHook(() => useSearch(), { wrapper: Wrapper, }); await waitFor(() => { expect(result.current).toEqual(expect.objectContaining(initialState)); + expect(searchApiMock.query).toHaveBeenLastCalledWith({ + term: '', + types: ['*'], + filters: {}, + }); + expect(analyticsApiMock.captureEvent).not.toHaveBeenCalled(); }); - const term = 'term'; - + // second api call with term 'eva' await act(async () => { - result.current.setTerm(term); + result.current.setTerm('eva'); }); await waitFor(() => { expect(searchApiMock.query).toHaveBeenLastCalledWith({ - term: 'term', + term: 'eva', types: ['*'], filters: {}, }); expect(analyticsApiMock.captureEvent).toHaveBeenCalledWith({ action: 'search', - subject: 'term', + subject: 'eva', value: 3, context: { extension: 'App', @@ -478,25 +488,28 @@ describe('SearchContext', () => { }, }); }); - }); - it('does not capture analytics events if term is empty', async () => { - searchApiMock.query.mockResolvedValue({ - results: [], - numberOfResults: 10, - }); - - renderHook(() => useSearch(), { - wrapper: Wrapper, + // third api call with term 'eva.m' + await act(async () => { + result.current.setTerm('eva.m'); }); await waitFor(() => { - expect(searchApiMock.query).toHaveBeenCalledWith({ - term: '', + expect(searchApiMock.query).toHaveBeenLastCalledWith({ + term: 'eva.m', types: ['*'], filters: {}, }); - expect(analyticsApiMock.captureEvent).not.toHaveBeenCalled(); + expect(analyticsApiMock.captureEvent).toHaveBeenCalledWith({ + action: 'search', + subject: 'eva.m', + value: 1, + context: { + extension: 'App', + pluginId: 'root', + routeRef: 'unknown', + }, + }); }); });