Skip to content

Commit

Permalink
fix: a bunch of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 19, 2021
1 parent f1033a5 commit 6075a0a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/axios/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type CacheProperties = {
*
* @default {}
*/
update: Record<string, CacheUpdater | undefined>;
update: Record<string, CacheUpdater>;
};

export type CacheAxiosResponse = AxiosResponse & {
Expand Down
4 changes: 2 additions & 2 deletions src/util/update-cache.ts
Original file line number Diff line number Diff line change
@@ -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') {
Expand Down
32 changes: 18 additions & 14 deletions test/header/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,71 @@ 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);
});

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);
});
});
6 changes: 3 additions & 3 deletions test/interceptors/request.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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);

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

Expand Down
15 changes: 3 additions & 12 deletions test/mocks/axios.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
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'
};

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,
Expand All @@ -36,5 +27,5 @@ export function mockAxios(
return config;
});

return cachedApi;
return axios;
}
4 changes: 2 additions & 2 deletions test/util/status-codes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 6075a0a

Please sign in to comment.