Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/libs/core-api/src/axiosHelpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import axiosRetry from 'axios-retry';
import { setupCache } from 'axios-cache-adapter';
// import { setupCache } from 'axios-cache-adapter';
import backoff from './utils/backoff';

// Exported only for unit test coverage
Expand All @@ -10,12 +10,12 @@ export const customCacheInvalidate = async (config, request) => {
}
};

const cache = setupCache({
// 15 minute cache
maxAge: 15 * 60 * 1000,
// Invalidate only when a specific option is passed through config
invalidate: customCacheInvalidate,
});
// const cache = setupCache({
// // 15 minute cache
// maxAge: 15 * 60 * 1000,
// // Invalidate only when a specific option is passed through config
// invalidate: customCacheInvalidate,
// });

export const getHeaderWithoutAuth = (optionalHeaders = {}) => {
return {
Expand Down Expand Up @@ -46,7 +46,7 @@ export const createAxiosCancelable = ({
const canceler = axios.CancelToken.source();
const auth = getHeaderWithoutAuth();
if (withCache) {
auth.adapter = cache.adapter;
// auth.adapter = cache.adapter;
}
const client = axios.create(auth);
axiosRetry(client, backoff(min, max));
Expand Down
106 changes: 53 additions & 53 deletions packages/libs/core-api/src/axiosHelpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,58 +119,58 @@ describe('axiosHelpers', () => {

expect(backoff).toHaveBeenCalledWith(expectedMin, expectedMax);
});
describe('cache', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('when withCache is true, axios create is called with adapter', () => {
createAxiosCancelable({ withCache: true });

expect(axios.create).toBeCalledWith(
expect.objectContaining({
adapter: expect.any(Function),
}),
);
});

it('when withCache is false, axios create is called without adapter', () => {
createAxiosCancelable({ withCache: false });

expect(axios.create).not.toBeCalledWith(
expect.objectContaining({
adapter: expect.any(Function),
}),
);
});

it('when withCache not specified, axios create is called without adapter', () => {
createAxiosCancelable();

expect(axios.create).not.toBeCalledWith(
expect.objectContaining({
adapter: expect.any(Function),
}),
);
});

it('when invalidate function on cache is called, it looks for clearCacheEntry in the request before invalidating the cache', async () => {
const mockRemoveItem = jest.fn();
const mockUuid = 'mockUuid';
const mockConfig = {
store: {
removeItem: mockRemoveItem,
},
uuid: mockUuid,
};

customCacheInvalidate(mockConfig, {});
expect(mockRemoveItem).not.toHaveBeenCalled();
const mockRequest = { clearCacheEntry: true };

customCacheInvalidate(mockConfig, mockRequest);
expect(mockRemoveItem).toHaveBeenCalledWith(mockUuid);
});
});
// describe('cache', () => {
// beforeEach(() => {
// jest.clearAllMocks();
// });

// it('when withCache is true, axios create is called with adapter', () => {
// createAxiosCancelable({ withCache: true });

// expect(axios.create).toBeCalledWith(
// expect.objectContaining({
// adapter: expect.any(Function),
// }),
// );
// });

// it('when withCache is false, axios create is called without adapter', () => {
// createAxiosCancelable({ withCache: false });

// expect(axios.create).not.toBeCalledWith(
// expect.objectContaining({
// adapter: expect.any(Function),
// }),
// );
// });

// it('when withCache not specified, axios create is called without adapter', () => {
// createAxiosCancelable();

// expect(axios.create).not.toBeCalledWith(
// expect.objectContaining({
// adapter: expect.any(Function),
// }),
// );
// });

// it('when invalidate function on cache is called, it looks for clearCacheEntry in the request before invalidating the cache', async () => {
// const mockRemoveItem = jest.fn();
// const mockUuid = 'mockUuid';
// const mockConfig = {
// store: {
// removeItem: mockRemoveItem,
// },
// uuid: mockUuid,
// };

// customCacheInvalidate(mockConfig, {});
// expect(mockRemoveItem).not.toHaveBeenCalled();
// const mockRequest = { clearCacheEntry: true };

// customCacheInvalidate(mockConfig, mockRequest);
// expect(mockRemoveItem).toHaveBeenCalledWith(mockUuid);
// });
// });
});
});