Skip to content

Commit b669d6f

Browse files
committed
add assertions on onSuccess and onError
1 parent c5b5263 commit b669d6f

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

test/useAsync.test.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,59 @@ describe('useAync', () => {
5757
it('should resolve a successful request', async () => {
5858
fetch.mockResponseOnce(JSON.stringify(fakeResults));
5959

60+
const onSuccess = jest.fn();
61+
const onError = jest.fn();
62+
6063
const { result, waitForNextUpdate } = renderHook<
6164
StarwarsHeroArgs,
6265
UseAsyncReturn<StarwarsHero[]>
63-
>(p => useAsync<StarwarsHero[], any>(p.asyncFunction, []), {
64-
initialProps: { ...props },
65-
});
66+
>(
67+
p =>
68+
useAsync<StarwarsHero[], any>(p.asyncFunction, [], {
69+
onSuccess: () => onSuccess(),
70+
onError: () => onError(),
71+
}),
72+
{
73+
initialProps: { ...props },
74+
}
75+
);
6676

6777
await waitForNextUpdate();
6878

6979
expect(result.current.result).toEqual(fakeResults);
7080
expect(result.current.loading).toBe(false);
7181
expect(result.current.error).toBeUndefined();
82+
expect(onSuccess).toHaveBeenCalled();
83+
expect(onError).not.toHaveBeenCalled();
7284
});
7385

7486
it('should set error detail for unsuccessful request', async () => {
7587
fetch.mockReject(new Error('something went wrong'));
7688

89+
const onSuccess = jest.fn();
90+
const onError = jest.fn();
91+
7792
const { result, waitForNextUpdate } = renderHook<
7893
StarwarsHeroArgs,
7994
UseAsyncReturn<StarwarsHero[]>
80-
>(p => useAsync<StarwarsHero[], any>(p.asyncFunction, []), {
81-
initialProps: { ...props },
82-
});
95+
>(
96+
p =>
97+
useAsync<StarwarsHero[], any>(p.asyncFunction, [], {
98+
onSuccess: () => onSuccess(),
99+
onError: () => onError(),
100+
}),
101+
{
102+
initialProps: { ...props },
103+
}
104+
);
83105

84106
await waitForNextUpdate();
85107

86108
expect(result.current.error).toBeDefined();
87109
expect(result.current.error!.message).toBe('something went wrong');
88110
expect(result.current.loading).toBe(false);
89111
expect(result.current.result).toBeUndefined();
112+
expect(onSuccess).not.toHaveBeenCalled();
113+
expect(onError).toHaveBeenCalled();
90114
});
91115
});

0 commit comments

Comments
 (0)