Skip to content

Commit

Permalink
refactor: removed symbol support and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 20, 2021
1 parent 70e5c07 commit 5b6d14f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/axios/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { Deferred } from '../util/deferred';
import { KeyGenerator } from '../util/key-generator';

export type CacheUpdater =
| ((cached: EmptyStorageValue | CachedStorageValue, newData: any) => CachedStorageValue | void)
| 'delete';
| 'delete'
| ((cached: EmptyStorageValue | CachedStorageValue, newData: any) => CachedStorageValue | void);

export type DefaultCacheRequestConfig = AxiosRequestConfig & {
cache: CacheProperties;
Expand Down Expand Up @@ -82,7 +82,7 @@ export type CacheAxiosResponse<T = any> = AxiosResponse<T> & {
* The id used for this request. if config specified an id, the id
* will be returned
*/
id: string | symbol;
id: string;
};

/**
Expand All @@ -95,7 +95,7 @@ export type CacheRequestConfig = AxiosRequestConfig & {
*
* @default undefined
*/
id?: string | symbol;
id?: string;

/**
* All cache options for the request.
Expand Down Expand Up @@ -124,6 +124,8 @@ export default interface CacheInstance {
/**
* A simple object that holds all deferred objects until it is
* resolved or rejected.
*
* Can be used to listen when a request is cached or not.
*/
waiting: Record<string, Deferred<CachedResponse, void>>;

Expand Down
4 changes: 1 addition & 3 deletions src/interceptors/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ export class CacheResponseInterceptor implements AxiosInterceptor<CacheAxiosResp
const deferred = this.axios.waiting[key];

// Resolve all other requests waiting for this response
if (deferred) {
await deferred.resolve(newCache.data);
}
await deferred?.resolve(newCache.data);

await this.axios.storage.set(key, newCache);

Expand Down
39 changes: 39 additions & 0 deletions test/interceptors/response.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { StatusCodes } from '../../src';
import { axiosMock, mockAxios } from '../mocks/axios';

describe('test request interceptor', () => {
Expand All @@ -20,4 +21,42 @@ describe('test request interceptor', () => {
expect(result.status).toBe(axiosMock.statusCode);
expect(result.statusText).toBe(axiosMock.statusText);
});

it('tests header interpreter integration', async () => {
const axiosNoCache = mockAxios({}, { 'cache-control': 'no-cache' });

// Make first request to cache it
await axiosNoCache.get('', { cache: { interpretHeader: true } });
const resultNoCache = await axiosNoCache.get('');

expect(resultNoCache.status).toBe(axiosMock.statusCode);
expect(resultNoCache.statusText).toBe(axiosMock.statusText);

const axiosCache = mockAxios({}, { 'cache-control': `maxAge=${60 * 60 * 24 * 365}` });

// Make first request to cache it
await axiosCache.get('', { cache: { interpretHeader: true } });
const resultCache = await axiosCache.get('');

expect(resultCache.status).toBe(StatusCodes.CACHED_STATUS_CODE);
expect(resultCache.statusText).toBe(StatusCodes.CACHED_STATUS_TEXT);
});

it('tests update cache integration', async () => {
const axios = mockAxios();

const { id } = await axios.get('key01');

await axios.get('key02', {
cache: {
update: {
[id]: 'delete' as const
}
}
});

const cache = await axios.storage.get(id);

expect(cache.state).toBe('empty');
});
});
5 changes: 3 additions & 2 deletions test/mocks/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const axiosMock = {
};

export function mockAxios(
options?: Partial<CacheInstance> & Partial<CacheProperties>
options: Partial<CacheInstance> & Partial<CacheProperties> = {},
headers: Record<string, string> = {}
): AxiosCacheInstance {
const axios = createCache({
// Defaults to cache every request
Expand All @@ -20,7 +21,7 @@ export function mockAxios(
data: true,
status: axiosMock.statusCode,
statusText: axiosMock.statusText,
headers: {},
headers,
config
});

Expand Down

0 comments on commit 5b6d14f

Please sign in to comment.