Skip to content

Commit

Permalink
test: useFetchPosts 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 6acb796 commit 4e0bdb2
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/core/src/react/hooks/__tests__/useFetchPosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { PostEntity, PostsArchiveParams } from '../../../data';
import { SettingsProvider } from '../../provider';
import { useFetchPosts } from '../useFetchPosts';
import { setHeadlessConfig } from '../../../utils';
import * as useFetchModule from '../useFetch';
import { mockUseFetchErrorResponse } from '../mocks';

describe('useFetchPosts', () => {
const wrapper = ({ children }) => {
Expand Down Expand Up @@ -40,6 +42,33 @@ describe('useFetchPosts', () => {
});
});

it('handles response if has error or there is no data', async () => {
const spyUseFetch = jest
.spyOn(useFetchModule, 'useFetch')
.mockReturnValueOnce(mockUseFetchErrorResponse);
const { result } = renderHook(() => useFetchPosts({}), {
wrapper,
});

const expectedKeys = ['error', 'loading', 'pageType', '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?.posts[0].title).toThrow();
expect(() => result.current.data?.pageInfo[0].title).toThrow();
expect(() => result.current.data?.queriedObject[0].title).toThrow();
expect(result.current.isMainQuery).toBe(true);
});

spyUseFetch.mockRestore();
});

it('returns queried object for category archives', async () => {
const { result } = renderHook(
() =>
Expand Down

0 comments on commit 4e0bdb2

Please sign in to comment.