Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: async test warnings #543

Merged
merged 2 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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