Skip to content

Commit

Permalink
refactor: init request and response tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 13, 2021
1 parent ee36bd5 commit 84dc522
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/interceptors/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import { CACHED_RESPONSE_STATUS, CACHED_RESPONSE_STATUS_TEXT } from '../util/sta
export function applyRequestInterceptor(axios: AxiosCacheInstance): void {
axios.interceptors.request.use(async (config) => {
// Only cache specified methods
if (
config.cache?.methods &&
!config.cache.methods.some((method) => (config.method || 'get').toLowerCase() == method)
) {
const allowedMethods = config.cache?.methods || axios.defaults.cache.methods;

if (!allowedMethods.some((method) => (config.method || 'get').toLowerCase() == method)) {
return config;
}

Expand Down
2 changes: 1 addition & 1 deletion src/interceptors/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function applyResponseInterceptor(axios: AxiosCacheInstance): void {
const key = axios.generateKey(response.config);
const cache = await axios.storage.get(key);

// Response is empty or was already cached
// Response shouldn't be cached or was already cached
if (cache.state !== 'loading') {
return response;
}
Expand Down
5 changes: 4 additions & 1 deletion src/util/cache-predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export function checkPredicateObject(
}
} else {
const [start, end] = statusCheck;
if (response.status <= start || response.status >= end) {
if (
response.status < start || //
response.status > end
) {
return false;
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/interceptors/request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { axiosMock, mockAxios } from '../mocks/axios';

describe('test request interceptor', () => {
it('tests against specified methods', async () => {
const axios = mockAxios({
// only cache post methods
methods: ['post']
});

const response = await axios.get(axiosMock.url);
const cacheKey = await axios.generateKey(response.config);
const cache = await axios.storage.get(cacheKey);

expect(cache.state).toBe('empty');
});

it('tests specified methods', async () => {
const axios = mockAxios({
// only cache get methods
methods: ['get']
});

const response = await axios.get(axiosMock.url);
const cacheKey = await axios.generateKey(response.config);
const cache = await axios.storage.get(cacheKey);

expect(cache.state).toBe('cached');
});
});
7 changes: 7 additions & 0 deletions test/interceptors/response.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// import { mockAxios } from '../mocks/axios';

describe('test request interceptor', () => {
it('tests', () => {
//const axios = mockAxios();
});
});
1 change: 0 additions & 1 deletion test/mocks/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export function mockAxios(

const cachedApi = createCache(api, {
// Defaults to cache every request
// cachePredicate: () => true,
...options
});

Expand Down

0 comments on commit 84dc522

Please sign in to comment.