Skip to content

Commit

Permalink
Wrap 'useInitialValues' in try/catch and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
danetsao committed Apr 24, 2024
1 parent f2a8ec9 commit da918ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
61 changes: 40 additions & 21 deletions src/hooks/useInitialValues.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,50 @@ jest.mock('../api/queries');
(useLocations as any).mockReturnValue({ isSuccess: false });
(useTransferAccounts as any).mockReturnValue({ isSuccess: false });

test('initial values hook', async () => {
const { result, rerender } = renderHook(() => useInitialValues(), {
wrapper: ({ children }: { children: React.ReactNode }) => withIntlConfiguration(<div>{children}</div>),
describe('useInitialValues', () => {
test('initial values hook', async () => {
const { result, rerender } = renderHook(() => useInitialValues(), {
wrapper: ({ children }: { children: React.ReactNode }) => withIntlConfiguration(<div>{children}</div>),
});

await waitFor(() => expect(result.current).toBeNull());

(useCurrentConfig as any).mockReturnValue({ isSuccess: true });
rerender();
expect(result.current).toBeNull();

(useFeeFineTypes as any).mockReturnValue({ isSuccess: true });
rerender();
expect(result.current).toBeNull();

(useLocations as any).mockReturnValue({ isSuccess: true });
rerender();
expect(result.current).toBeNull();

(useTransferAccounts as any).mockReturnValue({ isSuccess: true });
rerender();
await waitFor(() => expect(result.current).toEqual('values'));

// should not change back
(useTransferAccounts as any).mockReturnValue({ isSuccess: false });
rerender();
await waitFor(() => expect(result.current).toEqual('values'));
});

await waitFor(() => expect(result.current).toBeNull());
test('initial values hook with invalid data', async () => {
const { result, rerender } = renderHook(() => useInitialValues(), {
wrapper: ({ children }: { children: React.ReactNode }) => withIntlConfiguration(<div>{children}</div>),
});

(useCurrentConfig as any).mockReturnValue({ isSuccess: true });
rerender();
expect(result.current).toBeNull();
(useTransferAccounts as any).mockReturnValue({ isSuccess: false });

(useFeeFineTypes as any).mockReturnValue({ isSuccess: true });
rerender();
expect(result.current).toBeNull();
// should be null on fail
rerender();
await waitFor(() => expect(result.current).toBeNull());

(useLocations as any).mockReturnValue({ isSuccess: true });
rerender();
expect(result.current).toBeNull();
(useTransferAccounts as any).mockReturnValue({ isSuccess: true });
rerender();

(useTransferAccounts as any).mockReturnValue({ isSuccess: true });
rerender();
await waitFor(() => expect(result.current).toEqual('values'));

// should not change back
(useTransferAccounts as any).mockReturnValue({ isSuccess: false });
rerender();
await waitFor(() => expect(result.current).toEqual('values'));
await waitFor(() => expect(result.current).toEqual('values'));
});
});
11 changes: 7 additions & 4 deletions src/hooks/useInitialValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ export default function useInitialValues() {
if (!currentConfig.isSuccess || !feeFineTypes.isSuccess || !locations.isSuccess || !transferAccounts.isSuccess) {
return;
}

setInitialValues(
dtoToFormValues(currentConfig.data, localeWeekdays, feeFineTypes.data, locations.data, transferAccounts.data, stripes, intl)
);
try {
setInitialValues(
dtoToFormValues(currentConfig.data, localeWeekdays, feeFineTypes.data, locations.data, transferAccounts.data, stripes, intl)
);
} catch (e) {
setInitialValues(null);
}
}, [
currentConfig.isSuccess,
localeWeekdays,
Expand Down

0 comments on commit da918ac

Please sign in to comment.