Skip to content

Commit

Permalink
fix: async test warnings (#543)
Browse files Browse the repository at this point in the history
fix: async test warnings
  • Loading branch information
streamich authored Aug 20, 2019
2 parents 3782dc7 + 4a7fe73 commit 7af237e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/__tests__/useAsync.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ describe('useAsync', () => {
return 'new value';
};

beforeEach(() => {
beforeEach(done => {
callCount = 0;

hook = renderHook(({ fn }) => useAsync(fn, [fn]), {
initialProps: { fn: initialFn },
});

hook.waitForNextUpdate().then(done);
});

it('renders the first value', () => {
Expand Down Expand Up @@ -140,7 +143,7 @@ describe('useAsync', () => {
return `counter is ${counter} and callCount is ${callCount}`;
};

beforeEach(() => {
beforeEach(done => {
callCount = 0;
hook = renderHook(
({ fn, counter }) => {
Expand All @@ -154,6 +157,8 @@ describe('useAsync', () => {
},
}
);

hook.waitForNextUpdate().then(done);
});

it('initial renders the first passed pargs', () => {
Expand Down
23 changes: 12 additions & 11 deletions src/__tests__/useAsyncFn.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// does not automatically invoke the function
// and it can take arguments.

import { renderHook } from '@testing-library/react-hooks';
import { act, renderHook } from '@testing-library/react-hooks';
import useAsyncFn, { AsyncState } from '../useAsyncFn';

type AdderFn = (a: number, b: number) => Promise<number>;
Expand All @@ -17,27 +17,26 @@ describe('useAsyncFn', () => {

describe('the callback can be awaited and return the value', () => {
let hook;
let callCount = 0;
const adder = async (a: number, b: number): Promise<number> => {
callCount++;
return a + b;
};

beforeEach(() => {
// NOTE: renderHook isn't good at inferring array types
hook = renderHook<{ fn: AdderFn }, [AsyncState<number>, AdderFn]>(({ fn }) => useAsyncFn(fn), {
initialProps: {
fn: adder,
},
initialProps: { fn: adder },
});
});

it('awaits the result', async () => {
expect.assertions(3);

const [s, callback] = hook.result.current;
const [, callback] = hook.result.current;
let result;

const result = await callback(5, 7);
await act(async () => {
result = await callback(5, 7);
});

expect(result).toEqual(12);

Expand Down Expand Up @@ -78,13 +77,15 @@ describe('useAsyncFn', () => {
it('resolves a value derived from args', async () => {
expect.assertions(4);

const [s, callback] = hook.result.current;
const [, callback] = hook.result.current;

callback(2, 7);
act(() => {
callback(2, 7);
});
hook.rerender({ fn: adder });
await hook.waitForNextUpdate();

const [state, c] = hook.result.current;
const [state] = hook.result.current;

expect(callCount).toEqual(1);
expect(state.loading).toEqual(false);
Expand Down

0 comments on commit 7af237e

Please sign in to comment.