mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
fix: a bunch of tests
This commit is contained in:
parent
f1033a5959
commit
6075a0a960
@ -70,7 +70,7 @@ export type CacheProperties = {
|
|||||||
*
|
*
|
||||||
* @default {}
|
* @default {}
|
||||||
*/
|
*/
|
||||||
update: Record<string, CacheUpdater | undefined>;
|
update: Record<string, CacheUpdater>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CacheAxiosResponse = AxiosResponse & {
|
export type CacheAxiosResponse = AxiosResponse & {
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
import { AxiosCacheInstance, CacheProperties } from '../axios';
|
import { AxiosCacheInstance, CacheUpdater } from '../axios';
|
||||||
|
|
||||||
export async function updateCache(
|
export async function updateCache(
|
||||||
axios: AxiosCacheInstance,
|
axios: AxiosCacheInstance,
|
||||||
data: any,
|
data: any,
|
||||||
entries: CacheProperties['update']
|
entries: Record<string, CacheUpdater>
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
for (const [cacheKey, value] of Object.entries(entries)) {
|
for (const [cacheKey, value] of Object.entries(entries)) {
|
||||||
if (value == 'delete') {
|
if (value == 'delete') {
|
||||||
|
|||||||
@ -5,25 +5,25 @@ describe('tests header interpreter', () => {
|
|||||||
const noHeader = defaultHeaderInterpreter({});
|
const noHeader = defaultHeaderInterpreter({});
|
||||||
expect(noHeader).toBeUndefined();
|
expect(noHeader).toBeUndefined();
|
||||||
|
|
||||||
const emptyHeader = defaultHeaderInterpreter({ 'Cache-Control': 'public' });
|
const emptyHeader = defaultHeaderInterpreter({ 'cache-control': 'public' });
|
||||||
expect(emptyHeader).toBeUndefined();
|
expect(emptyHeader).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('tests with cache preventing headers', () => {
|
it('tests with cache preventing headers', () => {
|
||||||
const noStore = defaultHeaderInterpreter({
|
const noStore = defaultHeaderInterpreter({
|
||||||
'Cache-Control': 'no-store'
|
'cache-control': 'no-store'
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(noStore).toBe(false);
|
expect(noStore).toBe(false);
|
||||||
|
|
||||||
const noCache = defaultHeaderInterpreter({
|
const noCache = defaultHeaderInterpreter({
|
||||||
'Cache-Control': 'no-cache'
|
'cache-control': 'no-cache'
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(noCache).toBe(false);
|
expect(noCache).toBe(false);
|
||||||
|
|
||||||
const mustRevalidate = defaultHeaderInterpreter({
|
const mustRevalidate = defaultHeaderInterpreter({
|
||||||
'Cache-Control': 'must-revalidate'
|
'cache-control': 'must-revalidate'
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(mustRevalidate).toBe(false);
|
expect(mustRevalidate).toBe(false);
|
||||||
@ -31,41 +31,45 @@ describe('tests header interpreter', () => {
|
|||||||
|
|
||||||
it('tests with maxAge header for 10 seconds', () => {
|
it('tests with maxAge header for 10 seconds', () => {
|
||||||
const result = defaultHeaderInterpreter({
|
const result = defaultHeaderInterpreter({
|
||||||
'Cache-Control': 'max-age=10'
|
'cache-control': 'max-age=10'
|
||||||
});
|
});
|
||||||
|
|
||||||
// 10 Seconds in milliseconds
|
// 10 Seconds in milliseconds
|
||||||
expect(result).toBe(10 * 1000);
|
expect(result).toBe(10 * 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('tests with Expires and Cache-Control present', () => {
|
it('tests with expires and cache-control present', () => {
|
||||||
const result = defaultHeaderInterpreter({
|
const result = defaultHeaderInterpreter({
|
||||||
'Cache-Control': 'max-age=10',
|
'cache-control': 'max-age=10',
|
||||||
Expires: new Date(new Date().getFullYear() + 1, 1, 1).toISOString()
|
expires: new Date(new Date().getFullYear() + 1, 1, 1).toISOString()
|
||||||
});
|
});
|
||||||
|
|
||||||
// Expires should be ignored
|
// expires should be ignored
|
||||||
// 10 Seconds in milliseconds
|
// 10 Seconds in milliseconds
|
||||||
expect(result).toBe(10 * 1000);
|
expect(result).toBe(10 * 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('tests with past Expires', () => {
|
it('tests with past expires', () => {
|
||||||
const result = defaultHeaderInterpreter({
|
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
|
// Past means cache invalid
|
||||||
expect(result).toBe(false);
|
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 date = new Date(new Date().getFullYear() + 1, 1, 1);
|
||||||
|
|
||||||
const result = defaultHeaderInterpreter({
|
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
|
// the result should be what the date is in milliseconds
|
||||||
// minus the actual epoch milliseconds
|
// minus the actual epoch milliseconds
|
||||||
expect(result).toBeCloseTo(date.getTime() - Date.now());
|
expect(Math.abs((result as number) - approx)).toBeLessThan(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { axiosMock, mockAxios } from '../mocks/axios';
|
import { mockAxios } from '../mocks/axios';
|
||||||
|
|
||||||
describe('test request interceptor', () => {
|
describe('test request interceptor', () => {
|
||||||
it('tests against specified methods', async () => {
|
it('tests against specified methods', async () => {
|
||||||
@ -7,7 +7,7 @@ describe('test request interceptor', () => {
|
|||||||
methods: ['post']
|
methods: ['post']
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await axios.get(axiosMock.url);
|
const response = await axios.get('');
|
||||||
const cacheKey = await axios.generateKey(response.config);
|
const cacheKey = await axios.generateKey(response.config);
|
||||||
const cache = await axios.storage.get(cacheKey);
|
const cache = await axios.storage.get(cacheKey);
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ describe('test request interceptor', () => {
|
|||||||
methods: ['get']
|
methods: ['get']
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await axios.get(axiosMock.url);
|
const response = await axios.get('');
|
||||||
const cacheKey = await axios.generateKey(response.config);
|
const cacheKey = await axios.generateKey(response.config);
|
||||||
const cache = await axios.storage.get(cacheKey);
|
const cache = await axios.storage.get(cacheKey);
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,7 @@
|
|||||||
import axios from 'axios';
|
|
||||||
import { AxiosCacheInstance, CacheProperties, createCache } from '../../src';
|
import { AxiosCacheInstance, CacheProperties, createCache } from '../../src';
|
||||||
import CacheInstance from '../../src/axios/types';
|
import CacheInstance from '../../src/axios/types';
|
||||||
|
|
||||||
export const axiosMock = {
|
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,
|
statusCode: 200,
|
||||||
statusText: '200 Intercepted'
|
statusText: '200 Intercepted'
|
||||||
};
|
};
|
||||||
@ -15,16 +9,13 @@ export const axiosMock = {
|
|||||||
export function mockAxios(
|
export function mockAxios(
|
||||||
options?: Partial<CacheInstance> & Partial<CacheProperties>
|
options?: Partial<CacheInstance> & Partial<CacheProperties>
|
||||||
): AxiosCacheInstance {
|
): AxiosCacheInstance {
|
||||||
// A simple axios that resolves every request with a static response
|
const axios = createCache({
|
||||||
const api = axios.create();
|
|
||||||
|
|
||||||
const cachedApi = createCache(api, {
|
|
||||||
// Defaults to cache every request
|
// Defaults to cache every request
|
||||||
...options
|
...options
|
||||||
});
|
});
|
||||||
|
|
||||||
// Axios interceptors are a stack, so apply this after the cache interceptor
|
// 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) => ({
|
config.adapter = async (config) => ({
|
||||||
data: true,
|
data: true,
|
||||||
status: axiosMock.statusCode,
|
status: axiosMock.statusCode,
|
||||||
@ -36,5 +27,5 @@ export function mockAxios(
|
|||||||
return config;
|
return config;
|
||||||
});
|
});
|
||||||
|
|
||||||
return cachedApi;
|
return axios;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,11 +17,11 @@ describe('Tests cached status code', () => {
|
|||||||
state: 'cached'
|
state: 'cached'
|
||||||
});
|
});
|
||||||
|
|
||||||
const firstResponse = await axios.get(axiosMock.url);
|
const firstResponse = await axios.get('');
|
||||||
expect(firstResponse.status).toBe(axiosMock.statusCode);
|
expect(firstResponse.status).toBe(axiosMock.statusCode);
|
||||||
expect(firstResponse.statusText).toBe(axiosMock.statusText);
|
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.status).not.toBe(axiosMock.statusCode);
|
||||||
expect(secondResponse.statusText).not.toBe(axiosMock.statusText);
|
expect(secondResponse.statusText).not.toBe(axiosMock.statusText);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user