mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
42 lines
944 B
TypeScript
42 lines
944 B
TypeScript
import { AxiosHeaders } from 'axios';
|
|
import { mock } from 'node:test';
|
|
import type { CacheAxiosResponse } from '../src/cache/axios';
|
|
|
|
export const EMPTY_RESPONSE = Object.freeze({
|
|
headers: {},
|
|
status: 200,
|
|
statusText: '200 OK',
|
|
data: true
|
|
});
|
|
|
|
export function createResponse<R>(
|
|
config: Partial<CacheAxiosResponse<R>>
|
|
): CacheAxiosResponse {
|
|
return {
|
|
...EMPTY_RESPONSE,
|
|
config: { headers: new AxiosHeaders() },
|
|
data: {} as R,
|
|
request: {},
|
|
id: 'empty-id',
|
|
cached: true,
|
|
...config
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Mocks the result of Date.now() to return a current date plus the given ticks.
|
|
*
|
|
* TODO: Migrate to nodejs Date mock timers as soon as possible.
|
|
*
|
|
* @link https://github.com/nodejs/node/pull/48638
|
|
*/
|
|
export function mockDateNow(ticks: number) {
|
|
const old = Date.now;
|
|
|
|
mock.method(Date, 'now', mockedDateNow);
|
|
|
|
function mockedDateNow(this: DateConstructor) {
|
|
return old.call(this) + ticks;
|
|
}
|
|
}
|