Skip to content

Commit

Permalink
use useRefeenceArray hhoks into ReferenceArrayField
Browse files Browse the repository at this point in the history
  • Loading branch information
ThieryMichel committed Jun 2, 2019
1 parent eb15b3b commit efe7347
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,80 @@ import { crudGetManyAccumulate } from '../../actions';

describe('<ReferenceArrayFieldController />', () => {
afterEach(cleanup);
// it('should set the loadedOnce prop to false when related records are not yet fetched', () => {
// const children = jest.fn().mockReturnValue('child');
it('should set the loadedOnce prop to false when related records are not yet fetched', () => {
const children = jest.fn().mockReturnValue('child');

// renderWithRedux(
// <ReferenceArrayFieldController
// resource="foo"
// reference="bar"
// basePath=""
// record={{ id: 1, barIds: [1, 2] }}
// source="barIds"
// >
// {children}
// </ReferenceArrayFieldController>,
// {
// admin: {
// resources: {
// bar: {
// data: {},
// },
// },
// },
// }
// );
// expect(children.mock.calls[0][0]).toEqual({
// currentSort: { field: 'id', order: 'ASC' },
// loadedOnce: false,
// referenceBasePath: '',
// data: null,
// ids: [1, 2],
// });
// });
renderWithRedux(
<ReferenceArrayFieldController
resource="foo"
reference="bar"
basePath=""
record={{ id: 1, barIds: [1, 2] }}
source="barIds"
>
{children}
</ReferenceArrayFieldController>,
{
admin: {
resources: {
bar: {
data: {},
},
},
},
}
);
expect(children.mock.calls[0][0]).toEqual({
currentSort: { field: 'id', order: 'ASC' },
loadedOnce: false,
referenceBasePath: '',
data: null,
ids: [1, 2],
});
});

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

// renderWithRedux(
// <ReferenceArrayFieldController
// record={{ id: 1, barIds: [1, 2] }}
// resource="foo"
// reference="bar"
// source="barIds"
// basePath=""
// >
// {children}
// </ReferenceArrayFieldController>,
// {
// admin: {
// resources: {
// bar: {
// data: {
// 2: {
// id: 2,
// value: 'reference2',
// },
// },
// },
// },
// },
// }
// );
renderWithRedux(
<ReferenceArrayFieldController
record={{ id: 1, barIds: [1, 2] }}
resource="foo"
reference="bar"
source="barIds"
basePath=""
>
{children}
</ReferenceArrayFieldController>,
{
admin: {
resources: {
bar: {
data: {
2: {
id: 2,
value: 'reference2',
},
},
},
},
},
}
);

// expect(children.mock.calls[0][0]).toEqual({
// currentSort: { field: 'id', order: 'ASC' },
// loadedOnce: true,
// referenceBasePath: '',
// data: {
// 2: {
// id: 2,
// value: 'reference2',
// },
// },
// ids: [1, 2],
// });
// });
expect(children.mock.calls[0][0]).toEqual({
currentSort: { field: 'id', order: 'ASC' },
loadedOnce: true,
referenceBasePath: '',
data: {
2: {
id: 2,
value: 'reference2',
},
},
ids: [1, 2],
});
});

it('should set the data prop to the loaded data when it has been fetched', () => {
const children = jest.fn().mockReturnValue('child');
Expand Down
2 changes: 2 additions & 0 deletions packages/ra-core/src/controller/field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import ReferenceArrayFieldController from './ReferenceArrayFieldController';
import ReferenceFieldController from './ReferenceFieldController';
import ReferenceManyFieldController from './ReferenceManyFieldController';
import useReference from './useReference';
import useReferenceArray from './useReferenceArray';

export {
useReferenceArray,
ReferenceArrayFieldController,
ReferenceFieldController,
useReference,
Expand Down
15 changes: 6 additions & 9 deletions packages/ra-ui-materialui/src/field/ReferenceArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Children } from 'react';
import PropTypes from 'prop-types';
import LinearProgress from '@material-ui/core/LinearProgress';
import { withStyles, createStyles } from '@material-ui/core/styles';
import { ReferenceArrayFieldController } from 'ra-core';
import { useReferenceArray } from 'ra-core';
import { fieldPropTypes } from './types';

const styles = createStyles({
Expand Down Expand Up @@ -85,14 +85,11 @@ export const ReferenceArrayField = ({ children, ...props }) => {
}

return (
<ReferenceArrayFieldController {...props}>
{controllerProps => (
<ReferenceArrayFieldView
{...props}
{...{ children, ...controllerProps }}
/>
)}
</ReferenceArrayFieldController>
<ReferenceArrayFieldView
{...props}
{...useReferenceArray(props)}
children={children}
/>
);
};

Expand Down

0 comments on commit efe7347

Please sign in to comment.