Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienMattiussi committed Mar 11, 2020
1 parent eade146 commit f83f632
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('<ReferenceArrayInputController />', () => {
input={{ value: [1, 2] }}
>
{children}
</ReferenceArrayInputController>
</ReferenceArrayInputController>,
{ admin: { resources: { tags: { data: {} } } } }
);

expect(queryByText('true')).not.toBeNull();
Expand All @@ -43,7 +44,8 @@ describe('<ReferenceArrayInputController />', () => {
input={{ value: [1, 2] }}
>
{children}
</ReferenceArrayInputController>
</ReferenceArrayInputController>,
{ admin: { resources: { tags: { data: {} } } } }
);
expect(queryByText('true')).not.toBeNull();
});
Expand Down Expand Up @@ -110,6 +112,7 @@ describe('<ReferenceArrayInputController />', () => {
</ReferenceArrayInputController>,
{
admin: {
resources: { tags: { data: {} } },
references: {
possibleValues: {
'posts@tag_ids': { error: 'boom' },
Expand Down Expand Up @@ -234,6 +237,7 @@ describe('<ReferenceArrayInputController />', () => {
</ReferenceArrayInputController>,
{
admin: {
resources: { tags: { data: { 5: {}, 6: {} } } },
references: {
possibleValues: {
'posts@tag_ids': [],
Expand Down Expand Up @@ -288,7 +292,8 @@ describe('<ReferenceArrayInputController />', () => {
const { dispatch } = renderWithRedux(
<ReferenceArrayInputController {...defaultProps} allowEmpty>
{children}
</ReferenceArrayInputController>
</ReferenceArrayInputController>,
{ admin: { resources: { tags: { data: {} } } } }
);
expect(dispatch.mock.calls[0][0]).toEqual({
type: CRUD_GET_MATCHING,
Expand Down Expand Up @@ -425,7 +430,8 @@ describe('<ReferenceArrayInputController />', () => {
input={{ value: [5, 6] }}
>
{children}
</ReferenceArrayInputController>
</ReferenceArrayInputController>,
{ admin: { resources: { tags: { data: { 5: {}, 6: {} } } } } }
);
await wait(() => {
expect(dispatch).toHaveBeenCalledWith({
Expand All @@ -449,7 +455,8 @@ describe('<ReferenceArrayInputController />', () => {
input={{ value: [5] }}
>
{children}
</ReferenceArrayInputController>
</ReferenceArrayInputController>,
{ admin: { resources: { tags: { data: { 5: {} } } } } }
);

fireEvent.click(getByLabelText('Filter'));
Expand Down Expand Up @@ -477,7 +484,8 @@ describe('<ReferenceArrayInputController />', () => {
input={{ value: [5] }}
>
{children}
</ReferenceArrayInputController>
</ReferenceArrayInputController>,
{ admin: { resources: { tags: { data: { 5: {} } } } } }
);

rerender(
Expand Down Expand Up @@ -614,9 +622,10 @@ describe('<ReferenceArrayInputController />', () => {
input={{ value: [5, 6] }}
>
{children}
</ReferenceArrayInputController>
</ReferenceArrayInputController>,
{ admin: { resources: { tags: { data: { 5: {}, 6: {} } } } } }
);

console.log('wait');
await wait();
expect(dispatch).toHaveBeenCalledWith({
type: CRUD_GET_MANY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ const useReferenceArrayInputController = ({
const newIdsToFetch = difference(input.value, inputValue.current);
// Only get from store ids selected and already fetched
const newIdsToGetFromStore = difference(input.value, newIdsToFetch);
//Always change states to avoid keeping previous values no more selected
setIdsToFetch(newIdsToFetch);
setIdsToGetFromStore(newIdsToGetFromStore);

// Change states each time input values changes to avoid keeping previous values no more selected
if (!isEqual(idsToFetch, newIdsToFetch)) {
setIdsToFetch(newIdsToFetch);
}
if (!isEqual(idsToGetFromStore, newIdsToGetFromStore)) {
setIdsToGetFromStore(newIdsToGetFromStore);
}

inputValue.current = input.value;
}, [input.value, setIdsToFetch]);
}, [idsToFetch, idsToGetFromStore, input.value, setIdsToFetch]);

const [pagination, setPagination] = useState({ page: 1, perPage });
const [sort, setSort] = useState(defaultSort);
Expand Down

0 comments on commit f83f632

Please sign in to comment.