Skip to content

Commit

Permalink
#1532 #1533 - invalidate queries after updating data pubs
Browse files Browse the repository at this point in the history
  • Loading branch information
louise-davies committed May 22, 2024
1 parent 7e361ce commit 7a65d00
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/datagateway-common/src/api/dois.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -301,6 +304,7 @@ describe('doi api functions', () => {
},
{ headers: { Authorization: 'Bearer null' } }
);
expect(resetQueriesSpy).toHaveBeenCalled();
});

it('handles errors correctly', async () => {
Expand Down
26 changes: 26 additions & 0 deletions packages/datagateway-common/src/api/dois.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useQuery,
useMutation,
UseMutationResult,
useQueryClient,
} from 'react-query';
import { useSelector } from 'react-redux';
import { StateType } from '../state/app.types';
Expand Down Expand Up @@ -218,16 +219,41 @@ 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 }) => {
return updateDOI(dataPublicationId, content, doiMetadata, doiMinterUrl);
},
{
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)) ||

Check warning on line 242 in packages/datagateway-common/src/api/dois.tsx

View check run for this annotation

Codecov / codecov/patch

packages/datagateway-common/src/api/dois.tsx#L240-L242

Added lines #L240 - L242 were not covered by tests
// invalidate the data publication info query
(query.queryKey[0] === 'dataPublication' &&
typeof query.queryKey[1] !== 'undefined' &&
JSON.stringify(query.queryKey[1]).includes(dataPublicationId)) ||

Check warning on line 246 in packages/datagateway-common/src/api/dois.tsx

View check run for this annotation

Codecov / codecov/patch

packages/datagateway-common/src/api/dois.tsx#L244-L246

Added lines #L244 - L246 were not covered by tests
// invalidate the data publication content table queries
(query.queryKey[0] === 'dataPublicationContent' &&

Check warning on line 248 in packages/datagateway-common/src/api/dois.tsx

View check run for this annotation

Codecov / codecov/patch

packages/datagateway-common/src/api/dois.tsx#L248

Added line #L248 was not covered by tests
// use double equals to ignore difference between 1 and "1"
// eslint-disable-next-line eqeqeq
query.queryKey[2] == dataPublicationId) ||
(query.queryKey[0] === 'dataPublicationContentCount' &&

Check warning on line 252 in packages/datagateway-common/src/api/dois.tsx

View check run for this annotation

Codecov / codecov/patch

packages/datagateway-common/src/api/dois.tsx#L251-L252

Added lines #L251 - L252 were not covered by tests
// eslint-disable-next-line eqeqeq
query.queryKey[2] == dataPublicationId),

Check warning on line 254 in packages/datagateway-common/src/api/dois.tsx

View check run for this annotation

Codecov / codecov/patch

packages/datagateway-common/src/api/dois.tsx#L254

Added line #L254 was not covered by tests
});
},
}
);
};
Expand Down
6 changes: 3 additions & 3 deletions packages/datagateway-common/src/setupTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ export const createTestQueryClient = (): QueryClient =>
});

export const createReactQueryWrapper = (
history: History = createMemoryHistory()
history: History = createMemoryHistory(),
queryClient: QueryClient = createTestQueryClient()
): WrapperComponent<unknown> => {
const testQueryClient = createTestQueryClient();
const state = {
dgcommon: {
...initialState,
Expand All @@ -101,7 +101,7 @@ export const createReactQueryWrapper = (
const wrapper: WrapperComponent<unknown> = ({ children }) => (
<Provider store={mockStore(state)}>
<Router history={history}>
<QueryClientProvider client={testQueryClient}>
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
</Router>
Expand Down

0 comments on commit 7a65d00

Please sign in to comment.