Skip to content

Commit

Permalink
test: useFetchTerms with error or no data
Browse files Browse the repository at this point in the history
  • Loading branch information
Manussakis committed Nov 14, 2023
1 parent 4e0bdb2 commit 99c8198
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion packages/core/src/react/hooks/__tests__/useFetchTerms.tsx
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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();
});
});

0 comments on commit 99c8198

Please sign in to comment.