From 6075a0a960912d70775442bf01ae80de1b4f3d9d Mon Sep 17 00:00:00 2001 From: Hazork Date: Sun, 19 Sep 2021 19:29:05 -0300 Subject: [PATCH] fix: a bunch of tests --- src/axios/types.ts | 2 +- src/util/update-cache.ts | 4 ++-- test/header/interpreter.test.ts | 32 +++++++++++++++++-------------- test/interceptors/request.test.ts | 6 +++--- test/mocks/axios.ts | 15 +++------------ test/util/status-codes.test.ts | 4 ++-- 6 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/axios/types.ts b/src/axios/types.ts index bfe1dbe..c80b514 100644 --- a/src/axios/types.ts +++ b/src/axios/types.ts @@ -70,7 +70,7 @@ export type CacheProperties = { * * @default {} */ - update: Record; + update: Record; }; export type CacheAxiosResponse = AxiosResponse & { diff --git a/src/util/update-cache.ts b/src/util/update-cache.ts index 8cd8eea..58bd453 100644 --- a/src/util/update-cache.ts +++ b/src/util/update-cache.ts @@ -1,9 +1,9 @@ -import { AxiosCacheInstance, CacheProperties } from '../axios'; +import { AxiosCacheInstance, CacheUpdater } from '../axios'; export async function updateCache( axios: AxiosCacheInstance, data: any, - entries: CacheProperties['update'] + entries: Record ): Promise { for (const [cacheKey, value] of Object.entries(entries)) { if (value == 'delete') { diff --git a/test/header/interpreter.test.ts b/test/header/interpreter.test.ts index fda4f46..2c178b4 100644 --- a/test/header/interpreter.test.ts +++ b/test/header/interpreter.test.ts @@ -5,25 +5,25 @@ describe('tests header interpreter', () => { const noHeader = defaultHeaderInterpreter({}); expect(noHeader).toBeUndefined(); - const emptyHeader = defaultHeaderInterpreter({ 'Cache-Control': 'public' }); + const emptyHeader = defaultHeaderInterpreter({ 'cache-control': 'public' }); expect(emptyHeader).toBeUndefined(); }); it('tests with cache preventing headers', () => { const noStore = defaultHeaderInterpreter({ - 'Cache-Control': 'no-store' + 'cache-control': 'no-store' }); expect(noStore).toBe(false); const noCache = defaultHeaderInterpreter({ - 'Cache-Control': 'no-cache' + 'cache-control': 'no-cache' }); expect(noCache).toBe(false); const mustRevalidate = defaultHeaderInterpreter({ - 'Cache-Control': 'must-revalidate' + 'cache-control': 'must-revalidate' }); expect(mustRevalidate).toBe(false); @@ -31,41 +31,45 @@ describe('tests header interpreter', () => { it('tests with maxAge header for 10 seconds', () => { const result = defaultHeaderInterpreter({ - 'Cache-Control': 'max-age=10' + 'cache-control': 'max-age=10' }); // 10 Seconds in milliseconds expect(result).toBe(10 * 1000); }); - it('tests with Expires and Cache-Control present', () => { + it('tests with expires and cache-control present', () => { const result = defaultHeaderInterpreter({ - 'Cache-Control': 'max-age=10', - Expires: new Date(new Date().getFullYear() + 1, 1, 1).toISOString() + 'cache-control': 'max-age=10', + expires: new Date(new Date().getFullYear() + 1, 1, 1).toISOString() }); - // Expires should be ignored + // expires should be ignored // 10 Seconds in milliseconds expect(result).toBe(10 * 1000); }); - it('tests with past Expires', () => { + it('tests with past expires', () => { const result = defaultHeaderInterpreter({ - Expires: new Date(new Date().getFullYear() - 1, 1, 1).toISOString() + expires: new Date(new Date().getFullYear() - 1, 1, 1).toISOString() }); // Past means cache invalid expect(result).toBe(false); }); - it('tests with future Expires', () => { + it('tests with future expires', () => { const date = new Date(new Date().getFullYear() + 1, 1, 1); + const result = defaultHeaderInterpreter({ - Expires: date.toISOString() + expires: date.toISOString() }); + const approx = date.getTime() - Date.now(); + + expect(typeof result).toBe('number'); // the result should be what the date is in milliseconds // minus the actual epoch milliseconds - expect(result).toBeCloseTo(date.getTime() - Date.now()); + expect(Math.abs((result as number) - approx)).toBeLessThan(1); }); }); diff --git a/test/interceptors/request.test.ts b/test/interceptors/request.test.ts index 64bcc24..a352aaf 100644 --- a/test/interceptors/request.test.ts +++ b/test/interceptors/request.test.ts @@ -1,4 +1,4 @@ -import { axiosMock, mockAxios } from '../mocks/axios'; +import { mockAxios } from '../mocks/axios'; describe('test request interceptor', () => { it('tests against specified methods', async () => { @@ -7,7 +7,7 @@ describe('test request interceptor', () => { methods: ['post'] }); - const response = await axios.get(axiosMock.url); + const response = await axios.get(''); const cacheKey = await axios.generateKey(response.config); const cache = await axios.storage.get(cacheKey); @@ -20,7 +20,7 @@ describe('test request interceptor', () => { methods: ['get'] }); - const response = await axios.get(axiosMock.url); + const response = await axios.get(''); const cacheKey = await axios.generateKey(response.config); const cache = await axios.storage.get(cacheKey); diff --git a/test/mocks/axios.ts b/test/mocks/axios.ts index db56575..ba94623 100644 --- a/test/mocks/axios.ts +++ b/test/mocks/axios.ts @@ -1,13 +1,7 @@ -import axios from 'axios'; import { AxiosCacheInstance, CacheProperties, createCache } from '../../src'; import CacheInstance from '../../src/axios/types'; export const axiosMock = { - /** - * Simple request url to be used, doesn't matter at all because network - * requests are intercepted by a custom adapter. - */ - url: 'https://github.com/ArthurFiorette/axios-cache-interceptor/', statusCode: 200, statusText: '200 Intercepted' }; @@ -15,16 +9,13 @@ export const axiosMock = { export function mockAxios( options?: Partial & Partial ): AxiosCacheInstance { - // A simple axios that resolves every request with a static response - const api = axios.create(); - - const cachedApi = createCache(api, { + const axios = createCache({ // Defaults to cache every request ...options }); // Axios interceptors are a stack, so apply this after the cache interceptor - cachedApi.interceptors.request.use((config) => { + axios.interceptors.request.use((config) => { config.adapter = async (config) => ({ data: true, status: axiosMock.statusCode, @@ -36,5 +27,5 @@ export function mockAxios( return config; }); - return cachedApi; + return axios; } diff --git a/test/util/status-codes.test.ts b/test/util/status-codes.test.ts index c5d3778..f13350c 100644 --- a/test/util/status-codes.test.ts +++ b/test/util/status-codes.test.ts @@ -17,11 +17,11 @@ describe('Tests cached status code', () => { state: 'cached' }); - const firstResponse = await axios.get(axiosMock.url); + const firstResponse = await axios.get(''); expect(firstResponse.status).toBe(axiosMock.statusCode); expect(firstResponse.statusText).toBe(axiosMock.statusText); - const secondResponse = await axios.get(axiosMock.url); + const secondResponse = await axios.get(''); expect(secondResponse.status).not.toBe(axiosMock.statusCode); expect(secondResponse.statusText).not.toBe(axiosMock.statusText);