From 1da956a71352830683e43e3702e1581a1f9870b9 Mon Sep 17 00:00:00 2001 From: Gabriel Manussakis Date: Tue, 14 Nov 2023 17:22:07 -0300 Subject: [PATCH] test: useFetchAppSettings with error or no data --- .../hooks/__tests__/useFetchAppSettings.tsx | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/core/src/react/hooks/__tests__/useFetchAppSettings.tsx b/packages/core/src/react/hooks/__tests__/useFetchAppSettings.tsx index ec2332791..5b059e1d1 100644 --- a/packages/core/src/react/hooks/__tests__/useFetchAppSettings.tsx +++ b/packages/core/src/react/hooks/__tests__/useFetchAppSettings.tsx @@ -1,7 +1,9 @@ -import { renderHook } from '@testing-library/react'; +import { renderHook, waitFor } from '@testing-library/react'; import { expectTypeOf } from 'expect-type'; import { AppEntity, EndpointParams } from '../../../data'; import { useFetchAppSettings } from '../useFetchAppSettings'; +import * as useFetchModule from '../useFetch'; +import { mockUseFetchErrorResponse } from '../mocks'; describe('useFetchAppSettings types', () => { it('allows overriding types', () => { @@ -23,4 +25,22 @@ describe('useFetchAppSettings 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(() => useFetchAppSettings({ includeCustomSettings: true })); + + await waitFor(() => { + 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).toThrow(); + expect(result.current.isMainQuery).toBe(true); + }); + + spyUseFetch.mockRestore(); + }); });