Add caching tests for when getCacheIdentifier is asynchronous

This commit is contained in:
Alex Thirlwall 2024-05-07 16:47:50 +02:00 committed by Thijs Louisse
parent c5ffe9cffc
commit a8105ec278
2 changed files with 141 additions and 66 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/ajax': patch
---
Add caching tests for when getCacheIdentifier is asynchronous

View file

@ -451,6 +451,8 @@ describe('Ajax', () => {
let cacheId; let cacheId;
/** @type {() => string} */ /** @type {() => string} */
let getCacheIdentifier; let getCacheIdentifier;
/** @type {() => Promise<string>} */
let getCacheIdentifierAsync;
const newCacheId = () => { const newCacheId = () => {
if (!cacheId) { if (!cacheId) {
@ -463,6 +465,7 @@ describe('Ajax', () => {
beforeEach(() => { beforeEach(() => {
getCacheIdentifier = () => String(cacheId); getCacheIdentifier = () => String(cacheId);
getCacheIdentifierAsync = () => Promise.resolve(String(cacheId));
}); });
it('does not add cache interceptors when useCache is turned off', () => { it('does not add cache interceptors when useCache is turned off', () => {
@ -503,24 +506,8 @@ describe('Ajax', () => {
expect(customAjax._responseInterceptors.length).to.equal(1); expect(customAjax._responseInterceptors.length).to.equal(1);
}); });
describe('Caching interceptors: synchronous getCacheIdentifier tests', async () => { const cachingTests = {};
/** cachingTests.works = async (/** @type {Ajax} */ customAjax) => {
* @type {Ajax}
*/
let customAjax;
beforeEach(async () => {
newCacheId();
customAjax = new Ajax({
cacheOptions: {
useCache: true,
maxAge: 100,
getCacheIdentifier,
},
});
});
it('works', async () => {
await customAjax.fetch('/foo'); await customAjax.fetch('/foo');
const secondResponse = await customAjax.fetch('/foo'); const secondResponse = await customAjax.fetch('/foo');
@ -528,9 +515,9 @@ describe('Ajax', () => {
expect(await secondResponse.text()).to.equal('mock response'); expect(await secondResponse.text()).to.equal('mock response');
expect(secondResponse.headers.get('X-Custom-Header')).to.equal('y-custom-value'); expect(secondResponse.headers.get('X-Custom-Header')).to.equal('y-custom-value');
expect(secondResponse.headers.get('Content-Type')).to.equal('application/json'); expect(secondResponse.headers.get('Content-Type')).to.equal('application/json');
}); };
it('resets the cache if the cache ID changes', async () => { cachingTests.resetOnCacheIDChange = async (/** @type {Ajax} */ customAjax) => {
/* Three calls to the same endpoint should result in a /* Three calls to the same endpoint should result in a
single fetchStubCall due to caching */ single fetchStubCall due to caching */
await customAjax.fetch('/foo'); await customAjax.fetch('/foo');
@ -544,17 +531,17 @@ describe('Ajax', () => {
/* The newCacheId call should reset the cache, thereby adding an /* The newCacheId call should reset the cache, thereby adding an
extra call to the fetchStub call count. */ extra call to the fetchStub call count. */
expect(fetchStub.callCount).to.equal(2); expect(fetchStub.callCount).to.equal(2);
}); };
it('Completes pending requests when the cache ID changes', async () => { cachingTests.completePendingOnCacheIDChange = async (/** @type {Ajax} */ customAjax) => {
const requestOne = customAjax.fetch('/foo').then(() => 'completedRequestOne'); const requestOne = customAjax.fetch('/foo').then(() => 'completedRequestOne');
newCacheId(); newCacheId();
const requestTwo = customAjax.fetch('/foo').then(() => 'completedRequestTwo'); const requestTwo = customAjax.fetch('/foo').then(() => 'completedRequestTwo');
expect(await requestOne).to.equal('completedRequestOne'); expect(await requestOne).to.equal('completedRequestOne');
expect(await requestTwo).to.equal('completedRequestTwo'); expect(await requestTwo).to.equal('completedRequestTwo');
}); };
it('works with fetchJson', async () => { cachingTests.worksWithFetchJson = async (/** @type {Ajax} */ customAjax) => {
fetchStub.returns(Promise.resolve(new Response('{"a":1,"b":2}', responseInit()))); fetchStub.returns(Promise.resolve(new Response('{"a":1,"b":2}', responseInit())));
const firstResponse = await customAjax.fetchJson('/foo'); const firstResponse = await customAjax.fetchJson('/foo');
@ -567,9 +554,9 @@ describe('Ajax', () => {
expect(secondResponse.body).to.deep.equal({ a: 1, b: 2 }); expect(secondResponse.body).to.deep.equal({ a: 1, b: 2 });
expect(secondResponse.response.headers.get('X-Custom-Header')).to.equal('y-custom-value'); expect(secondResponse.response.headers.get('X-Custom-Header')).to.equal('y-custom-value');
expect(secondResponse.response.headers.get('Content-Type')).to.equal('application/json'); expect(secondResponse.response.headers.get('Content-Type')).to.equal('application/json');
}); };
it('is invalidated on non-get method', async () => { cachingTests.invalidatesOnNonGetMethod = async (/** @type {Ajax} */ customAjax) => {
await customAjax.fetch('/foo'); await customAjax.fetch('/foo');
const secondResponse = await customAjax.fetch('/foo', { method: 'POST' }); const secondResponse = await customAjax.fetch('/foo', { method: 'POST' });
@ -583,9 +570,9 @@ describe('Ajax', () => {
expect(await thirdResponse.text()).to.equal('mock response'); expect(await thirdResponse.text()).to.equal('mock response');
expect(thirdResponse.headers.get('X-Custom-Header')).to.equal('y-custom-value'); expect(thirdResponse.headers.get('X-Custom-Header')).to.equal('y-custom-value');
expect(thirdResponse.headers.get('Content-Type')).to.equal('application/json'); expect(thirdResponse.headers.get('Content-Type')).to.equal('application/json');
}); };
it('is invalidated after TTL has passed', async () => { cachingTests.invalidatesOnTTLPassed = async (/** @type {Ajax} */ customAjax) => {
const clock = useFakeTimers({ const clock = useFakeTimers({
shouldAdvanceTime: true, shouldAdvanceTime: true,
}); });
@ -607,6 +594,89 @@ describe('Ajax', () => {
expect(thirdResponse.headers.get('Content-Type')).to.equal('application/json'); expect(thirdResponse.headers.get('Content-Type')).to.equal('application/json');
clock.restore(); clock.restore();
};
describe('Caching interceptors: cache tests with synchronous getCacheIdentifier', async () => {
/**
* @type {Ajax}
*/
let customAjax;
beforeEach(async () => {
newCacheId();
customAjax = new Ajax({
cacheOptions: {
useCache: true,
maxAge: 100,
getCacheIdentifier,
},
});
});
it('works', async () => {
await cachingTests.works(customAjax);
});
it('resets the cache if the cache ID changes', async () => {
await cachingTests.resetOnCacheIDChange(customAjax);
});
it('Completes pending requests when the cache ID changes', async () => {
await cachingTests.completePendingOnCacheIDChange(customAjax);
});
it('works with fetchJson', async () => {
await cachingTests.worksWithFetchJson(customAjax);
});
it('is invalidated on non-get method', async () => {
await cachingTests.invalidatesOnNonGetMethod(customAjax);
});
it('is invalidated after TTL has passed', async () => {
await cachingTests.invalidatesOnTTLPassed(customAjax);
});
});
describe('Caching interceptors: cache tests with asynchronous getCacheIdentifier', async () => {
/**
* @type {Ajax}
*/
let customAjax;
beforeEach(async () => {
newCacheId();
customAjax = new Ajax({
cacheOptions: {
useCache: true,
maxAge: 100,
getCacheIdentifier: getCacheIdentifierAsync,
},
});
});
it('works', async () => {
await cachingTests.works(customAjax);
});
it('resets the cache if the cache ID changes', async () => {
await cachingTests.resetOnCacheIDChange(customAjax);
});
it('Completes pending requests when the cache ID changes', async () => {
await cachingTests.completePendingOnCacheIDChange(customAjax);
});
it('works with fetchJson', async () => {
await cachingTests.worksWithFetchJson(customAjax);
});
it('is invalidated on non-get method', async () => {
await cachingTests.invalidatesOnNonGetMethod(customAjax);
});
it('is invalidated after TTL has passed', async () => {
await cachingTests.invalidatesOnTTLPassed(customAjax);
}); });
}); });
}); });