fix: a bunch of tests

This commit is contained in:
Hazork 2021-09-19 19:29:05 -03:00
parent f1033a5959
commit 6075a0a960
6 changed files with 29 additions and 34 deletions

View File

@ -70,7 +70,7 @@ export type CacheProperties = {
*
* @default {}
*/
update: Record<string, CacheUpdater | undefined>;
update: Record<string, CacheUpdater>;
};
export type CacheAxiosResponse = AxiosResponse & {

View File

@ -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<string, CacheUpdater>
): Promise<void> {
for (const [cacheKey, value] of Object.entries(entries)) {
if (value == 'delete') {

View File

@ -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);
});
});

View File

@ -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);

View File

@ -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<CacheInstance> & Partial<CacheProperties>
): 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;
}

View File

@ -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);