Skip to content

Commit

Permalink
Merge pull request #5 from seegno/bugfix/test-warnings
Browse files Browse the repository at this point in the history
Fix React warnings in tests
  • Loading branch information
Rafael Azevedo authored Aug 19, 2019
2 parents 0ef0531 + c88c5a1 commit 110e42a
Show file tree
Hide file tree
Showing 3 changed files with 569 additions and 380 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,20 @@
"@babel/preset-env": "^7.5.5",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@testing-library/jest-dom": "^4.0.0",
"@testing-library/react": "^8.0.6",
"@testing-library/react-hooks": "^1.1.0",
"@testing-library/jest-dom": "^4.0.1",
"@testing-library/react": "^9.1.3",
"@testing-library/react-hooks": "^2.0.1",
"babel-plugin-module-resolver": "^3.2.0",
"eslint": "^5.16.0",
"eslint-config-seegno": "^15.0.0",
"flow-bin": "^0.103.0",
"flow-typed": "^2.6.0",
"jest": "^24.8.0",
"jest": "^24.9.0",
"lint-staged": "^9.2.1",
"pre-commit": "^1.2.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-test-renderer": "^16.8.6",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-test-renderer": "^16.9.0",
"sort-package-json": "^1.22.1"
},
"peerDependencies": {
Expand Down
25 changes: 15 additions & 10 deletions test/src/hooks/use-form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ describe('useForm hook', () => {
});

describe('submit', () => {
it('should call the `onSubmit` option with the form values and actions', () => {
it('should call the `onSubmit` option with the form values and actions', async () => {
const onSubmit = jest.fn(() => Promise.resolve());
const { rerender, result } = renderHook(() => useForm({
const { result, waitForNextUpdate } = renderHook(() => useForm({
initialValues: { foo: 'bar' },
jsonSchema: { type: 'object' },
onSubmit
Expand All @@ -277,8 +277,7 @@ describe('useForm hook', () => {
result.current.formActions.submit();
});

// Force rerender to ensure effect is called.
rerender();
await waitForNextUpdate();

expect(onSubmit).toHaveBeenCalledTimes(1);
expect(onSubmit).toHaveBeenCalledWith({ foo: 'bar' }, {
Expand Down Expand Up @@ -330,9 +329,9 @@ describe('useForm hook', () => {
expect(result.current.state.isSubmitting).toBe(false);
});

it('should not reset the form values', () => {
it('should not reset the form values', async () => {
const onSubmit = jest.fn(() => Promise.resolve());
const { result } = renderHook(() => useForm({
const { result, waitForNextUpdate } = renderHook(() => useForm({
jsonSchema: { type: 'object' },
onSubmit
}));
Expand All @@ -342,11 +341,13 @@ describe('useForm hook', () => {
result.current.formActions.submit();
});

await waitForNextUpdate();

expect(result.current.state.values).toEqual({ foo: 'bar' });
});

it('should set all fields to touched', () => {
const { result } = renderHook(() => useForm({
it('should set all fields to touched', async () => {
const { result, waitForNextUpdate } = renderHook(() => useForm({
jsonSchema: { type: 'object' },
onSubmit: () => Promise.resolve()
}));
Expand All @@ -356,6 +357,8 @@ describe('useForm hook', () => {
result.current.formActions.submit();
});

await waitForNextUpdate();

expect(result.current.state.meta).toEqual({
foo: expect.objectContaining({
touched: true
Expand All @@ -382,14 +385,14 @@ describe('useForm hook', () => {
expect(result.current.state.errors).toHaveProperty('foo');
});

it('should reset the form if the passed `reset` action is called', () => {
it('should reset the form if the passed `reset` action is called', async () => {
const onSubmit = jest.fn((values, { reset }) => {
reset();

return Promise.resolve();
});

const { result } = renderHook(() => useForm({
const { result, waitForNextUpdate } = renderHook(() => useForm({
jsonSchema: { type: 'object' },
onSubmit
}));
Expand All @@ -399,6 +402,8 @@ describe('useForm hook', () => {
result.current.formActions.submit();
});

await waitForNextUpdate();

expect(result.current.state.values).toEqual({});
});
});
Expand Down
Loading

0 comments on commit 110e42a

Please sign in to comment.