-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFR] Reference hooks #3228
Merged
Merged
[RFR] Reference hooks #3228
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
845e374
refactor ReferenceInputController into a functional component
ThieryMichel d510560
refactor ReferenceFieldController to use react-redux hook
ThieryMichel 0f17549
rename misleading TestContext store props to initialState
ThieryMichel 13c98ef
extract useReference hooks from ReferenceFieldController
ThieryMichel 5ac8cce
use useReference in ReferenceField
ThieryMichel 4598b33
use renderWithRedux when applicable
ThieryMichel 7714a2f
code review
ThieryMichel 3d25ac1
fix misleading example
ThieryMichel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
248 changes: 161 additions & 87 deletions
248
packages/ra-core/src/controller/field/ReferenceFieldController.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,192 +1,266 @@ | ||
import React from 'react'; | ||
import assert from 'assert'; | ||
import { shallow } from 'enzyme'; | ||
import { render, cleanup } from 'react-testing-library'; | ||
|
||
import { UnconnectedReferenceFieldController as ReferenceFieldController } from './ReferenceFieldController'; | ||
import ReferenceFieldController from './ReferenceFieldController'; | ||
import renderWithRedux from '../../util/renderWithRedux'; | ||
import { crudGetManyAccumulate } from '../../actions'; | ||
|
||
const defaultState = { | ||
admin: { | ||
resources: { posts: { data: { 123: { id: 123, title: 'foo' } } } }, | ||
}, | ||
}; | ||
|
||
describe('<ReferenceFieldController />', () => { | ||
afterEach(cleanup); | ||
it('should call crudGetManyAccumulate on componentDidMount if reference source is defined', () => { | ||
const crudGetManyAccumulate = jest.fn(); | ||
shallow( | ||
const { dispatch } = renderWithRedux( | ||
<ReferenceFieldController | ||
children={jest.fn()} // eslint-disable-line react/no-children-prop | ||
children={jest.fn().mockReturnValue(<span>children</span>)} // eslint-disable-line react/no-children-prop | ||
record={{ id: 1, postId: 123 }} | ||
source="postId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
reference="posts" | ||
basePath="" | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
/> | ||
/>, | ||
defaultState | ||
); | ||
assert.equal(crudGetManyAccumulate.mock.calls.length, 1); | ||
expect(dispatch).toBeCalledTimes(1); | ||
expect(dispatch).toBeCalledWith(crudGetManyAccumulate('posts', [123])); | ||
}); | ||
|
||
it('should not call crudGetManyAccumulate on componentDidMount if reference source is null or undefined', () => { | ||
const crudGetManyAccumulate = jest.fn(); | ||
shallow( | ||
const { dispatch } = renderWithRedux( | ||
<ReferenceFieldController | ||
children={jest.fn()} // eslint-disable-line react/no-children-prop | ||
children={jest.fn().mockReturnValue(<span>children</span>)} // eslint-disable-line react/no-children-prop | ||
record={{ id: 1, postId: null }} | ||
source="postId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
reference="posts" | ||
basePath="" | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
/> | ||
/>, | ||
defaultState | ||
); | ||
assert.equal(crudGetManyAccumulate.mock.calls.length, 0); | ||
expect(dispatch).toBeCalledTimes(0); | ||
}); | ||
it('should render a link to the Edit page of the related record by default', () => { | ||
const children = jest.fn(); | ||
const crudGetManyAccumulate = jest.fn(); | ||
shallow( | ||
|
||
it('should pass resourceLinkPath and referenceRecord to its children', () => { | ||
const children = jest.fn().mockReturnValue(<span>children</span>); | ||
renderWithRedux( | ||
<ReferenceFieldController | ||
record={{ id: 1, postId: 123 }} | ||
source="postId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
reference="posts" | ||
resource="comments" | ||
basePath="/comments" | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceFieldController> | ||
</ReferenceFieldController>, | ||
defaultState | ||
); | ||
assert.equal(children.mock.calls[0][0].resourceLinkPath, '/posts/123'); | ||
|
||
expect(children).toBeCalledWith({ | ||
isLoading: false, | ||
referenceRecord: { id: 123, title: 'foo' }, | ||
resourceLinkPath: '/posts/123', | ||
}); | ||
}); | ||
it('should render a link to the Edit page of the related record when the resource contains slashes', () => { | ||
const children = jest.fn(); | ||
const crudGetManyAccumulate = jest.fn(); | ||
shallow( | ||
|
||
it('should accept slashes in resource name', () => { | ||
const children = jest.fn().mockReturnValue(<span>children</span>); | ||
renderWithRedux( | ||
<ReferenceFieldController | ||
record={{ id: 1, postId: 123 }} | ||
source="postId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
reference="prefix/posts" | ||
resource="prefix/comments" | ||
basePath="/prefix/comments" | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceFieldController> | ||
); | ||
assert.equal( | ||
children.mock.calls[0][0].resourceLinkPath, | ||
'/prefix/posts/123' | ||
</ReferenceFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
'prefix/posts': { | ||
data: { 123: { id: 123, title: 'foo' } }, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
expect(children).toBeCalledWith({ | ||
isLoading: false, | ||
referenceRecord: { id: 123, title: 'foo' }, | ||
resourceLinkPath: '/prefix/posts/123', | ||
}); | ||
}); | ||
it('should render a link to the Edit page of the related record when the resource is named edit or show', () => { | ||
const children = jest.fn(); | ||
const crudGetManyAccumulate = jest.fn(); | ||
shallow( | ||
|
||
it('should accept edit as resource name', () => { | ||
const children = jest.fn().mockReturnValue(<span>children</span>); | ||
renderWithRedux( | ||
<ReferenceFieldController | ||
record={{ id: 1, fooId: 123 }} | ||
source="fooId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
reference="edit" | ||
resource="show" | ||
basePath="/show" | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceFieldController> | ||
</ReferenceFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
edit: { | ||
data: { 123: { id: 123, title: 'foo' } }, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
assert.equal(children.mock.calls[0][0].resourceLinkPath, '/edit/123'); | ||
|
||
shallow( | ||
expect(children).toBeCalledWith({ | ||
isLoading: false, | ||
referenceRecord: { id: 123, title: 'foo' }, | ||
resourceLinkPath: '/edit/123', | ||
}); | ||
}); | ||
|
||
it('should accept show as resource name', () => { | ||
const children = jest.fn().mockReturnValue(<span>children</span>); | ||
renderWithRedux( | ||
<ReferenceFieldController | ||
record={{ id: 1, fooId: 123 }} | ||
source="fooId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
reference="show" | ||
resource="edit" | ||
basePath="/edit" | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceFieldController> | ||
</ReferenceFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
show: { | ||
data: { 123: { id: 123, title: 'foo' } }, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
assert.equal(children.mock.calls[1][0].resourceLinkPath, '/show/123'); | ||
|
||
expect(children).toBeCalledWith({ | ||
isLoading: false, | ||
referenceRecord: { id: 123, title: 'foo' }, | ||
resourceLinkPath: '/show/123', | ||
}); | ||
}); | ||
|
||
it('should render a link to the Show page of the related record when the linkType is show', () => { | ||
const children = jest.fn(); | ||
const crudGetManyAccumulate = jest.fn(); | ||
shallow( | ||
const children = jest.fn().mockReturnValue(<span>children</span>); | ||
renderWithRedux( | ||
<ReferenceFieldController | ||
record={{ id: 1, postId: 123 }} | ||
source="postId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
resource="comments" | ||
reference="posts" | ||
basePath="/comments" | ||
linkType="show" | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceFieldController> | ||
); | ||
assert.equal( | ||
children.mock.calls[0][0].resourceLinkPath, | ||
'/posts/123/show' | ||
</ReferenceFieldController>, | ||
defaultState | ||
); | ||
|
||
expect(children).toBeCalledWith({ | ||
isLoading: false, | ||
referenceRecord: { id: 123, title: 'foo' }, | ||
resourceLinkPath: '/posts/123/show', | ||
}); | ||
}); | ||
it('should render a link to the Show page of the related record when the resource is named edit or show and linkType is show', () => { | ||
const children = jest.fn(); | ||
const crudGetManyAccumulate = jest.fn(); | ||
shallow( | ||
|
||
it('should accept edit as resource name when linkType is show', () => { | ||
const children = jest.fn().mockReturnValue(<span>children</span>); | ||
renderWithRedux( | ||
<ReferenceFieldController | ||
record={{ id: 1, fooId: 123 }} | ||
source="fooId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
reference="edit" | ||
resource="show" | ||
basePath="/show" | ||
linkType="show" | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceFieldController> | ||
); | ||
assert.equal( | ||
children.mock.calls[0][0].resourceLinkPath, | ||
'/edit/123/show' | ||
</ReferenceFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
edit: { | ||
data: { 123: { id: 123, title: 'foo' } }, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
shallow( | ||
expect(children).toBeCalledWith({ | ||
isLoading: false, | ||
referenceRecord: { id: 123, title: 'foo' }, | ||
resourceLinkPath: '/edit/123/show', | ||
}); | ||
}); | ||
|
||
it('should accept show as resource name when linkType is show', () => { | ||
const children = jest.fn().mockReturnValue(<span>children</span>); | ||
renderWithRedux( | ||
<ReferenceFieldController | ||
record={{ id: 1, fooId: 123 }} | ||
source="fooId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
reference="show" | ||
resource="edit" | ||
basePath="/edit" | ||
linkType="show" | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
linkType="show" | ||
> | ||
{children} | ||
</ReferenceFieldController> | ||
</ReferenceFieldController>, | ||
{ | ||
admin: { | ||
resources: { | ||
show: { | ||
data: { 123: { id: 123, title: 'foo' } }, | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
assert.equal( | ||
children.mock.calls[1][0].resourceLinkPath, | ||
'/show/123/show' | ||
); | ||
expect(children).toBeCalledWith({ | ||
isLoading: false, | ||
referenceRecord: { id: 123, title: 'foo' }, | ||
resourceLinkPath: '/show/123/show', | ||
}); | ||
}); | ||
it('should render no link when the linkType is false', () => { | ||
const children = jest.fn(); | ||
const crudGetManyAccumulate = jest.fn(); | ||
shallow( | ||
|
||
it('should set resourceLinkPath to false when the linkType is false', () => { | ||
const children = jest.fn().mockReturnValue(<span>children</span>); | ||
renderWithRedux( | ||
<ReferenceFieldController | ||
record={{ id: 1, fooId: 123 }} | ||
source="fooId" | ||
referenceRecord={{ id: 123, title: 'foo' }} | ||
reference="bar" | ||
record={{ id: 1, postId: 123 }} | ||
source="postId" | ||
reference="posts" | ||
basePath="/foo" | ||
linkType={false} | ||
crudGetManyAccumulate={crudGetManyAccumulate} | ||
> | ||
{children} | ||
</ReferenceFieldController> | ||
</ReferenceFieldController>, | ||
defaultState | ||
); | ||
assert.equal(children.mock.calls[0][0].resourceLinkPath, false); | ||
|
||
expect(children).toBeCalledWith({ | ||
isLoading: false, | ||
referenceRecord: { id: 123, title: 'foo' }, | ||
resourceLinkPath: false, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you use your shiny new
renderWithRedux
? Same for all tests using TestContextThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was afraid of the potential rebase ....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What rebase ?