Skip to content

Commit a8708c8

Browse files
committed
add test for sync fetch function
1 parent 72c8b3b commit a8708c8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

test/useAsync.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@ describe('useAync', () => {
4646
expect(onError).not.toHaveBeenCalled();
4747
});
4848

49+
// TODO legacy: should we remove this behavior?
50+
it('should resolve a successful synchronous request', async () => {
51+
const onSuccess = jest.fn();
52+
const onError = jest.fn();
53+
54+
const { result, waitForNextUpdate } = renderHook(() =>
55+
useAsync(
56+
// @ts-ignore: not allowed by TS on purpose, but still allowed at runtime
57+
() => fakeResults,
58+
[],
59+
{
60+
onSuccess: () => onSuccess(),
61+
onError: () => onError(),
62+
}
63+
)
64+
);
65+
66+
expect(result.current.loading).toBe(true);
67+
68+
await waitForNextUpdate();
69+
70+
expect(result.current.result).toEqual(fakeResults);
71+
expect(result.current.loading).toBe(false);
72+
expect(result.current.error).toBeUndefined();
73+
expect(onSuccess).toHaveBeenCalled();
74+
expect(onError).not.toHaveBeenCalled();
75+
});
76+
4977
it('should set error detail for unsuccessful request', async () => {
5078
const onSuccess = jest.fn();
5179
const onError = jest.fn();

0 commit comments

Comments
 (0)