From 5b6d14f31424db01cc181bfce599edfedde2cffd Mon Sep 17 00:00:00 2001 From: Hazork Date: Mon, 20 Sep 2021 12:20:35 -0300 Subject: [PATCH] refactor: removed symbol support and more tests --- src/axios/types.ts | 10 +++++--- src/interceptors/response.ts | 4 +-- test/interceptors/response.test.ts | 39 ++++++++++++++++++++++++++++++ test/mocks/axios.ts | 5 ++-- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/axios/types.ts b/src/axios/types.ts index 87ab769..d8259d2 100644 --- a/src/axios/types.ts +++ b/src/axios/types.ts @@ -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; @@ -82,7 +82,7 @@ export type CacheAxiosResponse = AxiosResponse & { * The id used for this request. if config specified an id, the id * will be returned */ - id: string | symbol; + id: string; }; /** @@ -95,7 +95,7 @@ export type CacheRequestConfig = AxiosRequestConfig & { * * @default undefined */ - id?: string | symbol; + id?: string; /** * All cache options for the request. @@ -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>; diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index 4f082ea..2ec75d2 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -91,9 +91,7 @@ export class CacheResponseInterceptor implements AxiosInterceptor { @@ -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'); + }); }); diff --git a/test/mocks/axios.ts b/test/mocks/axios.ts index ba94623..d36f8b1 100644 --- a/test/mocks/axios.ts +++ b/test/mocks/axios.ts @@ -7,7 +7,8 @@ export const axiosMock = { }; export function mockAxios( - options?: Partial & Partial + options: Partial & Partial = {}, + headers: Record = {} ): AxiosCacheInstance { const axios = createCache({ // Defaults to cache every request @@ -20,7 +21,7 @@ export function mockAxios( data: true, status: axiosMock.statusCode, statusText: axiosMock.statusText, - headers: {}, + headers, config });