Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Aug 21, 2019
1 parent bbe62f9 commit a77e902
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { cleanup } from '@testing-library/react';
import expect from 'expect';

import ReferenceArrayFieldController from './ReferenceArrayFieldController';
import { DataProviderContext } from '../../dataProvider';
import renderWithRedux from '../../util/renderWithRedux';
import { crudGetManyAccumulate } from '../../actions';

describe('<ReferenceArrayFieldController />', () => {
afterEach(cleanup);
Expand Down Expand Up @@ -33,13 +34,15 @@ describe('<ReferenceArrayFieldController />', () => {
expect(children.mock.calls[0][0]).toEqual({
currentSort: { field: 'id', order: 'ASC' },
loaded: false,
loading: true,
referenceBasePath: '',
data: null,
data: {},
ids: [1, 2],
error: null,
});
});

it('should set the loaded prop to true when at least one related record is found', () => {
it('should set the loaded prop to false when at least one related record is not found', () => {
const children = jest.fn().mockReturnValue('child');

renderWithRedux(
Expand Down Expand Up @@ -70,7 +73,8 @@ describe('<ReferenceArrayFieldController />', () => {

expect(children.mock.calls[0][0]).toEqual({
currentSort: { field: 'id', order: 'ASC' },
loaded: true,
loaded: false,
loading: true,
referenceBasePath: '',
data: {
2: {
Expand All @@ -79,6 +83,7 @@ describe('<ReferenceArrayFieldController />', () => {
},
},
ids: [1, 2],
error: null,
});
});

Expand Down Expand Up @@ -110,12 +115,14 @@ describe('<ReferenceArrayFieldController />', () => {
expect(children.mock.calls[0][0]).toEqual({
currentSort: { field: 'id', order: 'ASC' },
loaded: true,
loading: true,
referenceBasePath: '',
data: {
1: { id: 1, title: 'hello' },
2: { id: 2, title: 'world' },
},
ids: [1, 2],
error: null,
});
});

Expand Down Expand Up @@ -147,27 +154,37 @@ describe('<ReferenceArrayFieldController />', () => {
expect(children.mock.calls[0][0]).toEqual({
currentSort: { field: 'id', order: 'ASC' },
loaded: true,
loading: true,
referenceBasePath: '',
data: {
'abc-1': { id: 'abc-1', title: 'hello' },
'abc-2': { id: 'abc-2', title: 'world' },
},
ids: ['abc-1', 'abc-2'],
error: null,
});
});

it('should dispatch crudGetManyAccumulate', () => {
it('should call the dataProvider with GET_MANY on mount', async () => {
const children = jest.fn().mockReturnValue('child');
const dataProvider = jest.fn();
dataProvider.mockReturnValueOnce(
Promise.resolve({
data: [{ id: 1, title: 'foo' }, { id: 2, title: 'bar' }],
})
);
const { dispatch } = renderWithRedux(
<ReferenceArrayFieldController
record={{ id: 1, barIds: [1, 2] }}
resource="foo"
reference="bar"
source="barIds"
basePath=""
>
{children}
</ReferenceArrayFieldController>,
<DataProviderContext.Provider value={dataProvider}>
<ReferenceArrayFieldController
record={{ id: 1, barIds: [1, 2] }}
resource="foo"
reference="bar"
source="barIds"
basePath=""
>
{children}
</ReferenceArrayFieldController>
</DataProviderContext.Provider>,
{
admin: {
resources: {
Expand All @@ -178,6 +195,9 @@ describe('<ReferenceArrayFieldController />', () => {
},
}
);
expect(dispatch).toBeCalledWith(crudGetManyAccumulate('bar', [1, 2]));
await new Promise(resolve => setTimeout(resolve, 10));
expect(dispatch).toBeCalledTimes(5);
expect(dispatch.mock.calls[0][0].type).toBe('RA/CRUD_GET_MANY');
expect(dataProvider).toBeCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe('<ReferenceArrayField />', () => {
source="barIds"
basePath=""
data={data}
loaded={true}
ids={['abc-1', 'abc-2']}
>
<SingleFieldList>
Expand Down Expand Up @@ -121,6 +122,7 @@ describe('<ReferenceArrayField />', () => {
source="barIds"
basePath=""
data={data}
loaded={true}
ids={[1, 2]}
>
<SingleFieldList>
Expand Down Expand Up @@ -151,6 +153,7 @@ describe('<ReferenceArrayField />', () => {
basePath=""
data={data}
ids={[1, 2]}
loaded={true}
>
<SingleFieldList>
<TextField source="title" />
Expand Down

0 comments on commit a77e902

Please sign in to comment.