diff --git a/packages/datagateway-common/src/api/dois.test.tsx b/packages/datagateway-common/src/api/dois.test.tsx index d776540c2..131c42844 100644 --- a/packages/datagateway-common/src/api/dois.test.tsx +++ b/packages/datagateway-common/src/api/dois.test.tsx @@ -6,7 +6,7 @@ import { useIsCartMintable, useUpdateDOI, } from '.'; -import { createReactQueryWrapper } from '../setupTests'; +import { createReactQueryWrapper, createTestQueryClient } from '../setupTests'; import { InvalidateTokenType } from '../state/actions/actions.types'; import { setLogger } from 'react-query'; import log from 'loglevel'; @@ -269,8 +269,11 @@ describe('doi api functions', () => { }, }); + const queryClient = createTestQueryClient(); + const resetQueriesSpy = jest.spyOn(queryClient, 'resetQueries'); + const { result } = renderHook(() => useUpdateDOI(), { - wrapper: createReactQueryWrapper(), + wrapper: createReactQueryWrapper(undefined, queryClient), }); await act(async () => { @@ -301,6 +304,7 @@ describe('doi api functions', () => { }, { headers: { Authorization: 'Bearer null' } } ); + expect(resetQueriesSpy).toHaveBeenCalled(); }); it('handles errors correctly', async () => { diff --git a/packages/datagateway-common/src/api/dois.tsx b/packages/datagateway-common/src/api/dois.tsx index fdf192a13..666e36e4e 100644 --- a/packages/datagateway-common/src/api/dois.tsx +++ b/packages/datagateway-common/src/api/dois.tsx @@ -5,6 +5,7 @@ import { useQuery, useMutation, UseMutationResult, + useQueryClient, } from 'react-query'; import { useSelector } from 'react-redux'; import { StateType } from '../state/app.types'; @@ -218,9 +219,11 @@ export const useUpdateDOI = (): UseMutationResult< doiMetadata: DoiMetadata; } > => { + const queryClient = useQueryClient(); const doiMinterUrl = useSelector( (state: StateType) => state.dgcommon.urls.doiMinterUrl ); + const username = readSciGatewayToken().username; return useMutation( ({ dataPublicationId, content, doiMetadata }) => { @@ -228,6 +231,29 @@ export const useUpdateDOI = (): UseMutationResult< }, { onError: handleDOIAPIError, + onSuccess: (_data, { dataPublicationId, content, doiMetadata }) => { + // resetQueries instead of invalidateQueries as otherwise invalidateQueries shows out-of-date data + queryClient.resetQueries({ + predicate: (query) => + // invalidate the my DOIs page query + (query.queryKey[0] === 'dataPublication' && + username !== null && + typeof query.queryKey[2] !== 'undefined' && + JSON.stringify(query.queryKey[2]).includes(username)) || + // invalidate the data publication info query + (query.queryKey[0] === 'dataPublication' && + typeof query.queryKey[1] !== 'undefined' && + JSON.stringify(query.queryKey[1]).includes(dataPublicationId)) || + // invalidate the data publication content table queries + (query.queryKey[0] === 'dataPublicationContent' && + // use double equals to ignore difference between 1 and "1" + // eslint-disable-next-line eqeqeq + query.queryKey[2] == dataPublicationId) || + (query.queryKey[0] === 'dataPublicationContentCount' && + // eslint-disable-next-line eqeqeq + query.queryKey[2] == dataPublicationId), + }); + }, } ); }; diff --git a/packages/datagateway-common/src/setupTests.tsx b/packages/datagateway-common/src/setupTests.tsx index c3984f80e..3295f0249 100644 --- a/packages/datagateway-common/src/setupTests.tsx +++ b/packages/datagateway-common/src/setupTests.tsx @@ -80,9 +80,9 @@ export const createTestQueryClient = (): QueryClient => }); export const createReactQueryWrapper = ( - history: History = createMemoryHistory() + history: History = createMemoryHistory(), + queryClient: QueryClient = createTestQueryClient() ): WrapperComponent => { - const testQueryClient = createTestQueryClient(); const state = { dgcommon: { ...initialState, @@ -101,7 +101,7 @@ export const createReactQueryWrapper = ( const wrapper: WrapperComponent = ({ children }) => ( - + {children}