diff --git a/packages/core/src/react/hooks/__tests__/useFetchTerms.tsx b/packages/core/src/react/hooks/__tests__/useFetchTerms.tsx index 5264ca67e..a87c84183 100644 --- a/packages/core/src/react/hooks/__tests__/useFetchTerms.tsx +++ b/packages/core/src/react/hooks/__tests__/useFetchTerms.tsx @@ -1,7 +1,9 @@ -import { renderHook } from '@testing-library/react'; +import { renderHook, waitFor } from '@testing-library/react'; import { expectTypeOf } from 'expect-type'; import { TaxonomyArchiveParams, TermEntity } from '../../../data'; import { useFetchTerms } from '../useFetchTerms'; +import * as useFetchModule from '../useFetch'; +import { mockUseFetchErrorResponse } from '../mocks'; describe('useFetchTerms types', () => { it('allows overriding types', () => { @@ -22,4 +24,28 @@ describe('useFetchTerms types', () => { | undefined >(); }); + + it('handles response if has error or there is no data', async () => { + const spyUseFetch = jest + .spyOn(useFetchModule, 'useFetch') + .mockReturnValueOnce(mockUseFetchErrorResponse); + const { result } = renderHook(() => useFetchTerms({ includeCustomSettings: true })); + + const expectedKeys = ['error', 'loading', 'data', 'isMainQuery']; + const returnedKeys = Object.keys(result.current); + const missingKeys = returnedKeys.filter((key) => !expectedKeys.includes(key)); + + await waitFor(() => { + expect(missingKeys).toHaveLength(0); + expect(spyUseFetch).toHaveBeenCalledTimes(1); + expect(result.current.error).toBe('Not found'); + expect(result.current.loading).toBe(true); + expect(() => result.current.data).not.toThrow(); + expect(() => result.current.data?.terms[0].title).toThrow(); + expect(() => result.current.data?.pageInfo[0].title).toThrow(); + expect(result.current.isMainQuery).toBe(true); + }); + + spyUseFetch.mockRestore(); + }); });