From aecc4d37508518b06d44243906db4037b42ccd06 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Fri, 18 Oct 2024 10:41:46 -0300 Subject: [PATCH] code --- test/interceptors/request.test.ts | 46 +++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/test/interceptors/request.test.ts b/test/interceptors/request.test.ts index 1bcaee4..8966702 100644 --- a/test/interceptors/request.test.ts +++ b/test/interceptors/request.test.ts @@ -2,7 +2,10 @@ import assert from 'node:assert'; import { describe, it, mock } from 'node:test'; import { setTimeout } from 'node:timers/promises'; import type { AxiosAdapter, AxiosResponse } from 'axios'; -import type { CacheRequestConfig, InternalCacheRequestConfig } from '../../src/cache/axios.js'; +import type { + CacheRequestConfig, + InternalCacheRequestConfig +} from '../../src/cache/axios.js'; import { Header } from '../../src/header/headers.js'; import { buildMemoryStorage } from '../../src/index.js'; import type { LoadingStorageValue } from '../../src/storage/types.js'; @@ -110,7 +113,10 @@ describe('Request Interceptor', () => { }); it('Cache expiration', async () => { - const axios = mockAxios({}, { [Header.CacheControl]: 'max-age=1,stale-while-revalidate=10' }); + const axios = mockAxios( + {}, + { [Header.CacheControl]: 'max-age=1,stale-while-revalidate=10' } + ); await axios.get('http://test.com', { cache: { interpretHeader: true } @@ -261,7 +267,9 @@ describe('Request Interceptor', () => { adapter: async (config: InternalCacheRequestConfig) => { await setTimeout(10); - const response = (await (axios.defaults.adapter as AxiosAdapter)(config)) as AxiosResponse; + const response = (await (axios.defaults.adapter as AxiosAdapter)( + config + )) as AxiosResponse; // Changes the response to be different from `true` (default) response.data = 'overridden response'; @@ -416,7 +424,10 @@ describe('Request Interceptor', () => { assert.equal(req1.cached, false); assert.equal(req1.stale, undefined); - const [req2, req3] = await Promise.all([axios.get('some-other'), axios.get('some-other')]); + const [req2, req3] = await Promise.all([ + axios.get('some-other'), + axios.get('some-other') + ]); assert.equal(req2.cached, false); assert.equal(req2.stale, undefined); @@ -438,14 +449,20 @@ describe('Request Interceptor', () => { assert.equal(req1.cached, false); assert.equal(req1.stale, undefined); - const [req2, req3] = await Promise.all([axios.get('some-other'), axios.get('some-other')]); + const [req2, req3] = await Promise.all([ + axios.get('some-other'), + axios.get('some-other') + ]); assert.equal(req2.cached, false); assert.equal(req2.stale, undefined); assert.ok(req3.cached); assert.equal(req3.stale, false); - const [req4, req5] = await Promise.all([axios.get('other/url'), axios.get('other/url')]); + const [req4, req5] = await Promise.all([ + axios.get('other/url'), + axios.get('other/url') + ]); assert.equal(req4.cached, false); assert.equal(req4.stale, undefined); @@ -471,4 +488,21 @@ describe('Request Interceptor', () => { }) ); }); + + it('clone works with sequential requests', async () => { + const axios = mockAxios( + { + storage: buildMemoryStorage('double') + }, + undefined, + undefined, + () => ({ a: 1 }) + ); + + for (let i = 0; i < 10; i++) { + const result = await axios.get<{ a: 1 }>('/url'); + result.data.a++; + assert.equal(result.data.a, 2); + } + }); });