Skip to content

Commit

Permalink
test: useFetchSearch when has error or no data
Browse files Browse the repository at this point in the history
  • Loading branch information
Manussakis committed Oct 27, 2023
1 parent 36e20be commit 3395513
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion packages/core/src/react/hooks/__tests__/useFetchSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <SettingsProvider settings={{ sourceUrl: '' }}>{children}</SettingsProvider>;
};
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/react/hooks/useFetchSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function useFetchSearch<
pageInfo: makeErrorCatchProxy<PageInfo>('pageInfo'),
queriedObject: makeErrorCatchProxy<QueriedObject>('queriedObject'),
};
return { error, loading: !data, data: fakeData, isMainQuery };
return { error, loading: false, data: fakeData, isMainQuery };
}

const { result, pageInfo, queriedObject } = data;
Expand Down

0 comments on commit 3395513

Please sign in to comment.