From 33955130630d2381d9c1f282dc9b0c4e9be11f0a Mon Sep 17 00:00:00 2001 From: Gabriel Manussakis Date: Fri, 27 Oct 2023 14:15:29 -0300 Subject: [PATCH] test: useFetchSearch when has error or no data --- .../react/hooks/__tests__/useFetchSearch.tsx | 37 ++++++++++++++++++- .../core/src/react/hooks/useFetchSearch.ts | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/core/src/react/hooks/__tests__/useFetchSearch.tsx b/packages/core/src/react/hooks/__tests__/useFetchSearch.tsx index d90de9a7c..5909617f2 100644 --- a/packages/core/src/react/hooks/__tests__/useFetchSearch.tsx +++ b/packages/core/src/react/hooks/__tests__/useFetchSearch.tsx @@ -6,8 +6,10 @@ import { PostEntity, PostsArchiveParams } from '../../../data'; import { SettingsProvider } from '../../provider'; import { useFetchSearch } from '../useFetchSearch'; import { setHeadlessConfig } from '../../../utils'; +import * as useFetchModule from '../useFetch'; +import * as utils from '../util'; -describe('useFetchPosts', () => { +describe('useFetchSearch', () => { const wrapper = ({ children }) => { return {children}; }; @@ -29,6 +31,39 @@ describe('useFetchPosts', () => { }); }); + it('handles response if has error or there is no data', async () => { + const spyUseFetch = jest.spyOn(useFetchModule, 'useFetch').mockReturnValueOnce({ + error: 'Not found', + params: {}, + data: undefined, + isMainQuery: true, + mutate: jest.fn(), + isLoading: false, + isValidating: false, + }); + const spyMakeErrorCatchProxy = jest.spyOn(utils, 'makeErrorCatchProxy').mockReturnValue({}); + const { result } = renderHook(() => useFetchSearch({}), { + wrapper, + }); + + await waitFor(() => { + expect(spyUseFetch).toHaveBeenCalledTimes(1); + expect(spyMakeErrorCatchProxy).toHaveBeenCalledTimes(3); + expect(result.current.error).toBe('Not found'); + expect(result.current.loading).toBe(false); + expect(() => result.current.data).not.toThrow(); + expect(result.current.data).toStrictEqual({ + posts: {}, + pageInfo: {}, + queriedObject: {}, + }); + expect(result.current.isMainQuery).toBe(true); + }); + + spyUseFetch.mockRestore(); + spyMakeErrorCatchProxy.mockRestore(); + }); + it('fetches data properly', async () => { const { result } = renderHook(() => useFetchSearch({ per_page: 2 }, {}, '/ipsum'), { wrapper, diff --git a/packages/core/src/react/hooks/useFetchSearch.ts b/packages/core/src/react/hooks/useFetchSearch.ts index 02d1152ca..858fca561 100644 --- a/packages/core/src/react/hooks/useFetchSearch.ts +++ b/packages/core/src/react/hooks/useFetchSearch.ts @@ -50,7 +50,7 @@ export function useFetchSearch< pageInfo: makeErrorCatchProxy('pageInfo'), queriedObject: makeErrorCatchProxy('queriedObject'), }; - return { error, loading: !data, data: fakeData, isMainQuery }; + return { error, loading: false, data: fakeData, isMainQuery }; } const { result, pageInfo, queriedObject } = data;