Skip to content

Commit

Permalink
Merge pull request #4050 from marmelab/fix-test-warnings
Browse files Browse the repository at this point in the history
Fix console errors in tests
  • Loading branch information
djhi authored Nov 26, 2019
2 parents ee91801 + b2a7c62 commit be27f96
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,14 @@ describe('<ReferenceManyFieldController />', () => {
</ReferenceManyFieldController>
);

const { rerender, dispatch } = renderWithRedux(<ControllerWrapper />);
const { rerender, dispatch } = renderWithRedux(<ControllerWrapper />, {
admin: {
resources: {
bar: {},
foo: {},
},
},
});

expect(dispatch).toBeCalledTimes(3); // CRUD_GET_MANY_REFERENCE, CRUD_GET_MANY_REFERENCE_LOADING, FETCH_START
rerender(<ControllerWrapper sort={{ field: 'id', order: 'ASC' }} />);
Expand Down
5 changes: 3 additions & 2 deletions packages/ra-core/src/controller/useEditController.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import expect from 'expect';
import { act, cleanup } from '@testing-library/react';
import { act, cleanup, wait } from '@testing-library/react';

import EditController from './EditController';
import renderWithRedux from '../util/renderWithRedux';
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('useEditController', () => {
expect(crudGetOneAction.meta.resource).toEqual('posts');
});

it('should grab the record from the store based on the id', () => {
it('should grab the record from the store based on the id', async () => {
const { getByText } = renderWithRedux(
<EditController {...defaultProps}>
{({ record }) => <div>{record && record.title}</div>}
Expand All @@ -48,6 +48,7 @@ describe('useEditController', () => {
},
}
);
await wait();
expect(getByText('hello')).toBeDefined();
});

Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/controller/useListController.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('useListController', () => {
inputProps={{
'aria-label': 'search',
}}
value={filterValues.q}
value={filterValues.q || ''}
onChange={event => {
setFilters({ q: event.target.value });
}}
Expand Down
2 changes: 2 additions & 0 deletions packages/ra-core/src/dataProvider/Mutation.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ describe('Mutation', () => {
});

it('supports declarative onFailure side effects', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
let dispatchSpy;
let historyForAssertions: History;

Expand Down Expand Up @@ -234,6 +235,7 @@ describe('Mutation', () => {
});

it('supports onFailure side effects using hooks', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
let dispatchSpy;
const dataProvider = {
mytype: jest.fn(() =>
Expand Down
3 changes: 3 additions & 0 deletions packages/ra-core/src/dataProvider/Query.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe('Query', () => {
});

it('should update the error state after an error response', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
const dataProvider = {
getList: jest.fn(() =>
Promise.reject({ message: 'provider error' })
Expand Down Expand Up @@ -383,6 +384,7 @@ describe('Query', () => {
});

it('supports declarative onFailure side effects', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
let dispatchSpy;
let historyForAssertions: History;

Expand Down Expand Up @@ -450,6 +452,7 @@ describe('Query', () => {
});

it('supports onFailure function for side effects', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
let dispatchSpy;
const dataProvider = {
getList: jest.fn(() =>
Expand Down
5 changes: 4 additions & 1 deletion packages/ra-core/src/dataProvider/useGetMany.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ describe('useGetMany', () => {
});

it('should set the error state when the dataProvider fails', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
const hookValue = jest.fn();
const dataProvider = {
getMany: jest.fn(() => Promise.reject(new Error('failed'))),
Expand Down Expand Up @@ -364,6 +365,7 @@ describe('useGetMany', () => {
});

it('should execute failure side effects on failure', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
const onFailure = jest.fn();
const dataProvider = {
getMany: jest.fn(() => Promise.reject(new Error('failed'))),
Expand All @@ -382,7 +384,8 @@ describe('useGetMany', () => {
expect(onFailure.mock.calls.pop()[0]).toEqual(new Error('failed'));
});

it('should execute success side effects once for each hook call', async () => {
it('should execute failure side effects once for each hook call', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
const onFailure = jest.fn();
const dataProvider = {
getMany: jest.fn(() => Promise.reject(new Error('failed'))),
Expand Down
1 change: 1 addition & 0 deletions packages/ra-core/src/dataProvider/useMutation.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ describe('useMutation', () => {
});

it('should update the error state after an error response', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
const dataProvider = {
mytype: jest.fn(() =>
Promise.reject({ message: 'provider error' })
Expand Down
21 changes: 15 additions & 6 deletions packages/ra-core/src/reducer/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ import references, {
import ui from './ui';
import customQueries from './customQueries';

const defaultReducer = () => null;

export default combineReducers({
resources,
customQueries,
loading,
notifications,
references,
ui,
/**
* ts-jest does some aggressive module mocking when unit testing reducers individually.
* To avoid 'No reducer provided for key "..."' warnings,
* we pass default reducers. Sorry for legibility.
*
* @see https://stackoverflow.com/questions/43375079/redux-warning-only-appearing-in-tests
*/
resources: resources || defaultReducer,
customQueries: customQueries || defaultReducer,
loading: loading || defaultReducer,
notifications: notifications || defaultReducer,
references: references || defaultReducer,
ui: ui || defaultReducer,
});

export const getPossibleReferenceValues = (state, props) =>
Expand Down
13 changes: 11 additions & 2 deletions packages/ra-core/src/reducer/admin/references/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import possibleValues, {
getPossibleReferenceValues as pvGetPossibleReferenceValues,
} from './possibleValues';

const defaultReducer = () => null;

export default combineReducers({
oneToMany,
possibleValues,
/**
* ts-jest does some aggressive module mocking when unit testing reducers individually.
* To avoid 'No reducer provided for key "..."' warnings,
* we pass default reducers. Sorry for legibility.
*
* @see https://stackoverflow.com/questions/43375079/redux-warning-only-appearing-in-tests
*/
oneToMany: oneToMany || defaultReducer,
possibleValues: possibleValues || defaultReducer,
});

export const getPossibleReferenceValues = (state, props) =>
Expand Down
19 changes: 14 additions & 5 deletions packages/ra-core/src/reducer/admin/resource/list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import params from './params';
import selectedIds from './selectedIds';
import total from './total';

const defaultReducer = () => null;

export default combineReducers({
ids,
loadedOnce,
params,
selectedIds,
total,
/**
* ts-jest does some aggressive module mocking when unit testing reducers individually.
* To avoid 'No reducer provided for key "..."' warnings,
* we pass default reducers. Sorry for legibility.
*
* @see https://stackoverflow.com/questions/43375079/redux-warning-only-appearing-in-tests
*/
ids: ids || defaultReducer,
loadedOnce: loadedOnce || defaultReducer,
params: params || defaultReducer,
selectedIds: selectedIds || defaultReducer,
total: total || defaultReducer,
});
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/field/ImageField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('<ImageField />', () => {
<ImageField
{...defaultProps}
source="foo"
record={{ foo: true }}
record={{ foo: 'http://example.com' }}
className="foo"
/>
);
Expand Down
2 changes: 2 additions & 0 deletions packages/ra-ui-materialui/src/field/ReferenceField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ describe('<ReferenceField />', () => {
});

it('should not display a loader if the dataProvider query fails', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
const dataProvider = {
getMany: jest.fn(() => Promise.reject(new Error())),
};
Expand Down Expand Up @@ -194,6 +195,7 @@ describe('<ReferenceField />', () => {
});

it('should display an error icon if the dataProvider call fails', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
const dataProvider = {
getMany: jest.fn(() => Promise.reject('boo')),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('<NullableBooleanInput />', () => {
const defaultProps = {
source: 'isPublished',
resource: 'posts',
value: '',
};

it('should give three different choices for true, false or unknown', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/ra-ui-materialui/src/list/List.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe('<List />', () => {
selectedIds: [],
setPage: () => null,
total: 100,
translate: x => x,
version: 1,
};

Expand Down
1 change: 1 addition & 0 deletions packages/ra-ui-materialui/src/list/Pagination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('<Pagination />', () => {
});

it('should not display a pagination limit on an out of bounds page', () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
const { queryByText } = render(
<ThemeProvider theme={theme}>
<Pagination {...defaultProps} total={10} page={2} />
Expand Down

0 comments on commit be27f96

Please sign in to comment.