Skip to content

Commit

Permalink
Merge pull request #1576 from ral-facilities/feature/download-type-st…
Browse files Browse the repository at this point in the history
…atus-cache-#1519

Feature/download type status cache #1519
  • Loading branch information
kaperoo authored Aug 24, 2023
2 parents 230b50c + 8dc8dd0 commit 6e0ba51
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/datagateway-download/src/downloadApiHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,16 @@ describe('Download Cart API react-query hooks test', () => {
describe('useDownloadTypeStatuses', () => {
const downloadTypes = ['https', 'globus'];

let queryClient: QueryClient;

beforeAll(() => {
queryClient = new QueryClient();
});

afterEach(() => {
queryClient.clear();
});

it('should query statuses of download types', async () => {
axios.get = jest.fn().mockResolvedValue({
data: {
Expand Down Expand Up @@ -1218,6 +1228,43 @@ describe('Download Cart API react-query hooks test', () => {
}
);
});

it('should refetch data on every hook call', async () => {
axios.get = jest.fn().mockResolvedValue({
data: {
disabled: false,
message: '',
},
});

const wrapper = createReactQueryWrapper();

const { result, waitFor } = renderHook(
() =>
useDownloadTypeStatuses({
downloadTypes: ['https'],
}),
{ wrapper }
);

await waitFor(() => result.current.every((query) => query.isSuccess));

expect(result.current[0].isStale).toBe(true);
expect(axios.get).toHaveBeenCalledTimes(1);

const { result: newResult } = renderHook(
() =>
useDownloadTypeStatuses({
downloadTypes: ['https'],
}),
{ wrapper }
);

await waitFor(() => newResult.current.every((query) => query.isSuccess));

expect(newResult.current[0].isStale).toBe(true);
expect(axios.get).toHaveBeenCalledTimes(2);
});
});

describe('useDownloadPercentageComplete', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/datagateway-download/src/downloadApiHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ export const useDownloadTypeStatuses = <TData = DownloadTypeStatus>({
if (error) handleQueryError(type);
},
...queryOptions,
cacheTime: 0,
staleTime: 0,
}));

// I have spent hours on this trying to make the type work,
Expand Down

0 comments on commit 6e0ba51

Please sign in to comment.