From f83f632395ea3301ad7ef0860fef0c61267c98bf Mon Sep 17 00:00:00 2001 From: JulienM Date: Wed, 11 Mar 2020 11:58:10 +0100 Subject: [PATCH] fix tests --- .../ReferenceArrayInputController.spec.tsx | 25 +++++++++++++------ .../input/useReferenceArrayInputController.ts | 13 +++++++--- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/ra-core/src/controller/input/ReferenceArrayInputController.spec.tsx b/packages/ra-core/src/controller/input/ReferenceArrayInputController.spec.tsx index d7b4d6e56d0..5daef56b700 100644 --- a/packages/ra-core/src/controller/input/ReferenceArrayInputController.spec.tsx +++ b/packages/ra-core/src/controller/input/ReferenceArrayInputController.spec.tsx @@ -27,7 +27,8 @@ describe('', () => { input={{ value: [1, 2] }} > {children} - + , + { admin: { resources: { tags: { data: {} } } } } ); expect(queryByText('true')).not.toBeNull(); @@ -43,7 +44,8 @@ describe('', () => { input={{ value: [1, 2] }} > {children} - + , + { admin: { resources: { tags: { data: {} } } } } ); expect(queryByText('true')).not.toBeNull(); }); @@ -110,6 +112,7 @@ describe('', () => { , { admin: { + resources: { tags: { data: {} } }, references: { possibleValues: { 'posts@tag_ids': { error: 'boom' }, @@ -234,6 +237,7 @@ describe('', () => { , { admin: { + resources: { tags: { data: { 5: {}, 6: {} } } }, references: { possibleValues: { 'posts@tag_ids': [], @@ -288,7 +292,8 @@ describe('', () => { const { dispatch } = renderWithRedux( {children} - + , + { admin: { resources: { tags: { data: {} } } } } ); expect(dispatch.mock.calls[0][0]).toEqual({ type: CRUD_GET_MATCHING, @@ -425,7 +430,8 @@ describe('', () => { input={{ value: [5, 6] }} > {children} - + , + { admin: { resources: { tags: { data: { 5: {}, 6: {} } } } } } ); await wait(() => { expect(dispatch).toHaveBeenCalledWith({ @@ -449,7 +455,8 @@ describe('', () => { input={{ value: [5] }} > {children} - + , + { admin: { resources: { tags: { data: { 5: {} } } } } } ); fireEvent.click(getByLabelText('Filter')); @@ -477,7 +484,8 @@ describe('', () => { input={{ value: [5] }} > {children} - + , + { admin: { resources: { tags: { data: { 5: {} } } } } } ); rerender( @@ -614,9 +622,10 @@ describe('', () => { input={{ value: [5, 6] }} > {children} - + , + { admin: { resources: { tags: { data: { 5: {}, 6: {} } } } } } ); - + console.log('wait'); await wait(); expect(dispatch).toHaveBeenCalledWith({ type: CRUD_GET_MANY, diff --git a/packages/ra-core/src/controller/input/useReferenceArrayInputController.ts b/packages/ra-core/src/controller/input/useReferenceArrayInputController.ts index ea2b70cb507..d74f4417d79 100644 --- a/packages/ra-core/src/controller/input/useReferenceArrayInputController.ts +++ b/packages/ra-core/src/controller/input/useReferenceArrayInputController.ts @@ -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);