-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3294 from marmelab/useReferenceArrayField
[RFR] add UseReferenceArrayField hook
- Loading branch information
Showing
5 changed files
with
245 additions
and
141 deletions.
There are no files selected for viewing
181 changes: 120 additions & 61 deletions
181
packages/ra-core/src/controller/field/ReferenceArrayFieldController.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,183 @@ | ||
import React from 'react'; | ||
import assert from 'assert'; | ||
import { shallow } from 'enzyme'; | ||
import { UnconnectedReferenceArrayFieldController as ReferenceArrayFieldController } from './ReferenceArrayFieldController'; | ||
import { cleanup } from 'react-testing-library'; | ||
|
||
describe('<ReferenceArrayFieldController />', () => { | ||
const crudGetManyAccumulate = jest.fn(); | ||
import ReferenceArrayFieldController from './ReferenceArrayFieldController'; | ||
import renderWithRedux from '../../util/renderWithRedux'; | ||
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(); | ||
const children = jest.fn().mockReturnValue('child'); | ||
|
||
shallow( | ||
renderWithRedux( | ||
<ReferenceArrayFieldController | ||
record={{ id: 1, barIds: [1, 2] }} | ||
resource="foo" | ||
reference="bar" | ||
source="barIds" | ||
basePath="" | ||
data={null} | ||
ids={[1, 2]} | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
record={{ id: 1, barIds: [1, 2] }} | ||
source="barIds" | ||
> | ||
{children} | ||
</ReferenceArrayFieldController> | ||
</ReferenceArrayFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
bar: { | ||
data: {}, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
assert.equal(children.mock.calls[0][0].loadedOnce, false); | ||
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(); | ||
const children = jest.fn().mockReturnValue('child'); | ||
|
||
shallow( | ||
renderWithRedux( | ||
<ReferenceArrayFieldController | ||
record={{ id: 1, barIds: [1, 2] }} | ||
resource="foo" | ||
reference="bar" | ||
source="barIds" | ||
basePath="" | ||
data={{ 1: { id: 1 } }} | ||
ids={[1, 2]} | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceArrayFieldController> | ||
</ReferenceArrayFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
bar: { | ||
data: { | ||
2: { | ||
id: 2, | ||
title: 'hello', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
assert.equal(children.mock.calls[0][0].loadedOnce, true); | ||
expect(children.mock.calls[0][0]).toEqual({ | ||
currentSort: { field: 'id', order: 'ASC' }, | ||
loadedOnce: true, | ||
referenceBasePath: '', | ||
data: { | ||
2: { | ||
id: 2, | ||
title: 'hello', | ||
}, | ||
}, | ||
ids: [1, 2], | ||
}); | ||
}); | ||
|
||
it('should set the data prop to the loaded data when it has been fetched', () => { | ||
const children = jest.fn(); | ||
const data = { | ||
1: { id: 1, title: 'hello' }, | ||
2: { id: 2, title: 'world' }, | ||
}; | ||
shallow( | ||
const children = jest.fn().mockReturnValue('child'); | ||
renderWithRedux( | ||
<ReferenceArrayFieldController | ||
record={{ id: 1, barIds: [1, 2] }} | ||
resource="foo" | ||
reference="bar" | ||
source="barIds" | ||
basePath="" | ||
data={data} | ||
ids={[1, 2]} | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceArrayFieldController> | ||
</ReferenceArrayFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
bar: { | ||
data: { | ||
1: { id: 1, title: 'hello' }, | ||
2: { id: 2, title: 'world' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
assert.equal(children.mock.calls[0][0].loadedOnce, true); | ||
assert.deepEqual(children.mock.calls[0][0].data, data); | ||
assert.deepEqual(children.mock.calls[0][0].ids, [1, 2]); | ||
expect(children.mock.calls[0][0]).toEqual({ | ||
currentSort: { field: 'id', order: 'ASC' }, | ||
loadedOnce: true, | ||
referenceBasePath: '', | ||
data: { | ||
1: { id: 1, title: 'hello' }, | ||
2: { id: 2, title: 'world' }, | ||
}, | ||
ids: [1, 2], | ||
}); | ||
}); | ||
|
||
it('should support record with string identifier', () => { | ||
const children = jest.fn(); | ||
const data = { | ||
'abc-1': { id: 'abc-1', title: 'hello' }, | ||
'abc-2': { id: 'abc-2', title: 'world' }, | ||
}; | ||
shallow( | ||
const children = jest.fn().mockReturnValue('child'); | ||
renderWithRedux( | ||
<ReferenceArrayFieldController | ||
record={{ id: 1, barIds: ['abc-1', 'abc-2'] }} | ||
resource="foo" | ||
reference="bar" | ||
source="barIds" | ||
basePath="" | ||
data={data} | ||
ids={['abc-1', 'abc-2']} | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceArrayFieldController> | ||
</ReferenceArrayFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
bar: { | ||
data: { | ||
'abc-1': { id: 'abc-1', title: 'hello' }, | ||
'abc-2': { id: 'abc-2', title: 'world' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
assert.equal(children.mock.calls[0][0].loadedOnce, true); | ||
assert.deepEqual(children.mock.calls[0][0].data, data); | ||
assert.deepEqual(children.mock.calls[0][0].ids, ['abc-1', 'abc-2']); | ||
expect(children.mock.calls[0][0]).toEqual({ | ||
currentSort: { field: 'id', order: 'ASC' }, | ||
loadedOnce: true, | ||
referenceBasePath: '', | ||
data: { | ||
'abc-1': { id: 'abc-1', title: 'hello' }, | ||
'abc-2': { id: 'abc-2', title: 'world' }, | ||
}, | ||
ids: ['abc-1', 'abc-2'], | ||
}); | ||
}); | ||
|
||
it('should support record with number identifier', () => { | ||
const children = jest.fn(); | ||
const data = { | ||
1: { id: 1, title: 'hello' }, | ||
2: { id: 2, title: 'world' }, | ||
}; | ||
shallow( | ||
it('should dispatch crudGetManyAccumulate', () => { | ||
const children = jest.fn().mockReturnValue('child'); | ||
const { dispatch } = renderWithRedux( | ||
<ReferenceArrayFieldController | ||
record={{ id: 1, barIds: [1, 2] }} | ||
resource="foo" | ||
reference="bar" | ||
source="barIds" | ||
basePath="" | ||
data={data} | ||
ids={[1, 2]} | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceArrayFieldController> | ||
</ReferenceArrayFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
bar: { | ||
data: {}, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
assert.equal(children.mock.calls[0][0].loadedOnce, true); | ||
assert.deepEqual(children.mock.calls[0][0].data, data); | ||
assert.deepEqual(children.mock.calls[0][0].ids, [1, 2]); | ||
expect(dispatch).toBeCalledWith(crudGetManyAccumulate('bar', [1, 2])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.