chore: use async/await in cache interceptor tests

This commit is contained in:
Thomas Allmer 2021-02-15 13:55:49 +01:00
parent bbffd7105f
commit 1216ce9e85

View file

@ -65,16 +65,11 @@ describe('ajax cache', function describeLibCache() {
}); });
describe('Original ajax instance', () => { describe('Original ajax instance', () => {
it('allows direct ajax calls without cache interceptors configured', () => { it('allows direct ajax calls without cache interceptors configured', async () => {
return ajax await ajax.request('/test');
.request('/test') expect(fetchStub.callCount).to.equal(1);
.then(() => { await ajax.request('/test');
expect(fetchStub.callCount).to.equal(1); expect(fetchStub.callCount).to.equal(2);
})
.then(() => ajax.request('/test'))
.then(() => {
expect(fetchStub.callCount).to.equal(2);
});
}); });
}); });
@ -145,7 +140,7 @@ describe('ajax cache', function describeLibCache() {
}); });
describe('Cached responses', () => { describe('Cached responses', () => {
it('returns the cached object on second call with `useCache: true`', () => { it('returns the cached object on second call with `useCache: true`', async () => {
newCacheId(); newCacheId();
const indexes = addCacheInterceptors(ajax, { const indexes = addCacheInterceptors(ajax, {
@ -154,23 +149,17 @@ describe('ajax cache', function describeLibCache() {
}); });
const ajaxRequestSpy = spy(ajax, 'request'); const ajaxRequestSpy = spy(ajax, 'request');
return ajax await ajax.request('/test');
.request('/test') expect(ajaxRequestSpy.calledOnce).to.be.true;
.then(() => { expect(ajaxRequestSpy.calledWith('/test')).to.be.true;
expect(ajaxRequestSpy.calledOnce).to.be.true; await ajax.request('/test');
expect(ajaxRequestSpy.calledWith('/test')).to.be.true; expect(fetchStub.callCount).to.equal(1);
})
.then(() => ajax.request('/test')) ajaxRequestSpy.restore();
.then(() => { removeCacheInterceptors(ajax, indexes);
expect(fetchStub.callCount).to.equal(1);
})
.finally(() => {
ajaxRequestSpy.restore();
removeCacheInterceptors(ajax, indexes);
});
}); });
it('all calls with non-default `timeToLive` are cached proactively', () => { it('all calls with non-default `timeToLive` are cached proactively', async () => {
newCacheId(); newCacheId();
const indexes = addCacheInterceptors(ajax, { const indexes = addCacheInterceptors(ajax, {
@ -179,36 +168,23 @@ describe('ajax cache', function describeLibCache() {
}); });
const ajaxRequestSpy = spy(ajax, 'request'); const ajaxRequestSpy = spy(ajax, 'request');
return ajax await ajax.request('/test');
.request('/test') expect(ajaxRequestSpy.calledOnce).to.be.true;
.then(() => { expect(ajaxRequestSpy.calledWith('/test')).to.be.true;
expect(ajaxRequestSpy.calledOnce).to.be.true; expect(fetchStub.callCount).to.equal(1);
expect(ajaxRequestSpy.calledWith('/test')).to.be.true; await ajax.request('/test');
}) expect(fetchStub.callCount).to.equal(2);
.then(() => { await ajax.request('/test', {
expect(fetchStub.callCount).to.equal(1); cacheOptions: {
}) useCache: true,
.then(() => ajax.request('/test')) },
.then(() => { });
expect(fetchStub.callCount).to.equal(2); expect(fetchStub.callCount).to.equal(2);
}) ajaxRequestSpy.restore();
.then(() => removeCacheInterceptors(ajax, indexes);
ajax.request('/test', {
cacheOptions: {
useCache: true,
},
}),
)
.then(() => {
expect(fetchStub.callCount).to.equal(2);
})
.finally(() => {
ajaxRequestSpy.restore();
removeCacheInterceptors(ajax, indexes);
});
}); });
it('returns the cached object on second call with `useCache: true`, with querystring parameters', () => { it('returns the cached object on second call with `useCache: true`, with querystring parameters', async () => {
newCacheId(); newCacheId();
const indexes = addCacheInterceptors(ajax, { const indexes = addCacheInterceptors(ajax, {
@ -218,47 +194,34 @@ describe('ajax cache', function describeLibCache() {
const ajaxRequestSpy = spy(ajax, 'request'); const ajaxRequestSpy = spy(ajax, 'request');
return ajax await ajax.request('/test', {
.request('/test', { params: {
params: { q: 'test',
q: 'test', page: 1,
page: 1, },
}, });
}) expect(ajaxRequestSpy.calledOnce).to.be.true;
.then(() => { expect(ajaxRequestSpy.calledWith('/test')).to.be.true;
expect(ajaxRequestSpy.calledOnce).to.be.true; await ajax.request('/test', {
expect(ajaxRequestSpy.calledWith('/test')).to.be.true; params: {
}) q: 'test',
.then(() => page: 1,
ajax.request('/test', { },
params: { });
q: 'test', expect(fetchStub.callCount).to.equal(1);
page: 1, // a request with different param should not be cached
}, await ajax.request('/test', {
}), params: {
) q: 'test',
.then(() => { page: 2,
expect(fetchStub.callCount).to.equal(1); },
}) });
.then(() => expect(fetchStub.callCount).to.equal(2);
// a request with different param should not be cached ajaxRequestSpy.restore();
ajax.request('/test', { removeCacheInterceptors(ajax, indexes);
params: {
q: 'test',
page: 2,
},
}),
)
.then(() => {
expect(fetchStub.callCount).to.equal(2);
})
.finally(() => {
ajaxRequestSpy.restore();
removeCacheInterceptors(ajax, indexes);
});
}); });
it('uses cache when inside `timeToLive: 5000` window', () => { it('uses cache when inside `timeToLive: 5000` window', async () => {
newCacheId(); newCacheId();
const clock = useFakeTimers({ const clock = useFakeTimers({
shouldAdvanceTime: true, shouldAdvanceTime: true,
@ -270,33 +233,22 @@ describe('ajax cache', function describeLibCache() {
}); });
const ajaxRequestSpy = spy(ajax, 'request'); const ajaxRequestSpy = spy(ajax, 'request');
return ajax await ajax.request('/test');
.request('/test') expect(ajaxRequestSpy.calledOnce).to.be.true;
.then(() => { expect(ajaxRequestSpy.calledWith('/test')).to.be.true;
expect(ajaxRequestSpy.calledOnce).to.be.true; expect(fetchStub.callCount).to.equal(1);
expect(ajaxRequestSpy.calledWith('/test')).to.be.true; clock.tick(4900);
expect(fetchStub.callCount).to.equal(1); await ajax.request('/test');
}) expect(fetchStub.callCount).to.equal(1);
.then(() => { clock.tick(5100);
clock.tick(4900); await ajax.request('/test');
}) expect(fetchStub.callCount).to.equal(2);
.then(() => ajax.request('/test')) ajaxRequestSpy.restore();
.then(() => { clock.restore();
expect(fetchStub.callCount).to.equal(1); removeCacheInterceptors(ajax, indexes);
clock.tick(5100);
})
.then(() => ajax.request('/test'))
.then(() => {
expect(fetchStub.callCount).to.equal(2);
})
.finally(() => {
ajaxRequestSpy.restore();
clock.restore();
removeCacheInterceptors(ajax, indexes);
});
}); });
it('uses custom requestIdentificationFn when passed', () => { it('uses custom requestIdentificationFn when passed', async () => {
newCacheId(); newCacheId();
const customRequestIdFn = /** @type {RequestIdentificationFn} */ (request, serializer) => { const customRequestIdFn = /** @type {RequestIdentificationFn} */ (request, serializer) => {
@ -314,20 +266,15 @@ describe('ajax cache', function describeLibCache() {
requestIdentificationFn: reqIdSpy, requestIdentificationFn: reqIdSpy,
}); });
return ajax await ajax.request('/test', { headers: { 'x-id': '1' } });
.request('/test', { headers: { 'x-id': '1' } }) expect(reqIdSpy.calledOnce);
.then(() => { expect(reqIdSpy.returnValues[0]).to.equal(`/test-1`);
expect(reqIdSpy.calledOnce); removeCacheInterceptors(ajax, indexes);
expect(reqIdSpy.returnValues[0]).to.equal(`/test-1`);
})
.finally(() => {
removeCacheInterceptors(ajax, indexes);
});
}); });
}); });
describe('Cache invalidation', () => { describe('Cache invalidation', () => {
it('previously cached data has to be invalidated when regex invalidation rule triggered', () => { it('previously cached data has to be invalidated when regex invalidation rule triggered', async () => {
newCacheId(); newCacheId();
const indexes = addCacheInterceptors(ajax, { const indexes = addCacheInterceptors(ajax, {
@ -336,46 +283,30 @@ describe('ajax cache', function describeLibCache() {
invalidateUrlsRegex: /foo/gi, invalidateUrlsRegex: /foo/gi,
}); });
return ajax await ajax.request('/test'); // new url
.request('/test') expect(fetchStub.callCount).to.equal(1);
.then(() => ajax.request('/test')) await ajax.request('/test'); // cached
.then(() => { expect(fetchStub.callCount).to.equal(1);
expect(fetchStub.callCount).to.equal(1);
}) await ajax.request('/foo-request-1'); // new url
.then(() => ajax.request('/foo-request-1')) expect(fetchStub.callCount).to.equal(2);
.then(() => { await ajax.request('/foo-request-1'); // cached
expect(fetchStub.callCount).to.equal(2); expect(fetchStub.callCount).to.equal(2);
})
.then(() => ajax.request('/foo-request-1')) await ajax.request('/foo-request-3'); // new url
.then(() => { expect(fetchStub.callCount).to.equal(3);
expect(fetchStub.callCount).to.equal(2);
}) await ajax.request('/test', { method: 'POST' }); // clear cache
.then(() => ajax.request('/foo-request-2')) expect(fetchStub.callCount).to.equal(4);
.then(() => { await ajax.request('/foo-request-1'); // not cached anymore
expect(fetchStub.callCount).to.equal(3); expect(fetchStub.callCount).to.equal(5);
}) await ajax.request('/foo-request-2'); // not cached anymore
.then(() => ajax.request('/foo-request-2')) expect(fetchStub.callCount).to.equal(6);
.then(() => {
expect(fetchStub.callCount).to.equal(3); removeCacheInterceptors(ajax, indexes);
})
.then(() => ajax.request('/test', { method: 'POST' }))
.then(() => {
expect(fetchStub.callCount).to.equal(4);
})
.then(() => ajax.request('/foo-request-1'))
.then(() => {
expect(fetchStub.callCount).to.equal(5);
})
.then(() => ajax.request('/foo-request-2'))
.then(() => {
expect(fetchStub.callCount).to.equal(6);
})
.finally(() => {
removeCacheInterceptors(ajax, indexes);
});
}); });
it('previously cached data has to be invalidated when regex invalidation rule triggered and urls are nested', () => { it('previously cached data has to be invalidated when regex invalidation rule triggered and urls are nested', async () => {
newCacheId(); newCacheId();
const indexes = addCacheInterceptors(ajax, { const indexes = addCacheInterceptors(ajax, {
@ -384,53 +315,29 @@ describe('ajax cache', function describeLibCache() {
invalidateUrlsRegex: /posts/gi, invalidateUrlsRegex: /posts/gi,
}); });
return ajax await ajax.request('/test');
.request('/test') await ajax.request('/test'); // cached
.then(() => ajax.request('/test')) expect(fetchStub.callCount).to.equal(1);
.then(() => { await ajax.request('/posts');
expect(fetchStub.callCount).to.equal(1); expect(fetchStub.callCount).to.equal(2);
}) await ajax.request('/posts'); // cached
.then(() => ajax.request('/posts')) expect(fetchStub.callCount).to.equal(2);
.then(() => { await ajax.request('/posts/1');
expect(fetchStub.callCount).to.equal(2); expect(fetchStub.callCount).to.equal(3);
}) await ajax.request('/posts/1'); // cached
.then(() => ajax.request('/posts')) expect(fetchStub.callCount).to.equal(3);
.then(() => { // cleans cache for defined urls
// no new requests, cached await ajax.request('/test', { method: 'POST' });
expect(fetchStub.callCount).to.equal(2); expect(fetchStub.callCount).to.equal(4);
}) await ajax.request('/posts'); // no longer cached => new request
.then(() => ajax.request('/posts/1')) expect(fetchStub.callCount).to.equal(5);
.then(() => { await ajax.request('/posts/1'); // no longer cached => new request
expect(fetchStub.callCount).to.equal(3); expect(fetchStub.callCount).to.equal(6);
})
.then(() => ajax.request('/posts/1')) removeCacheInterceptors(ajax, indexes);
.then(() => {
// no new requests, cached
expect(fetchStub.callCount).to.equal(3);
})
.then(() =>
// cleans cache for defined urls
ajax.request('/test', { method: 'POST' }),
)
.then(() => {
expect(fetchStub.callCount).to.equal(4);
})
.then(() => ajax.request('/posts'))
.then(() => {
// new requests, cache is cleaned
expect(fetchStub.callCount).to.equal(5);
})
.then(() => ajax.request('/posts/1'))
.then(() => {
// new requests, cache is cleaned
expect(fetchStub.callCount).to.equal(6);
})
.finally(() => {
removeCacheInterceptors(ajax, indexes);
});
}); });
it('deletes cache after one hour', () => { it('deletes cache after one hour', async () => {
newCacheId(); newCacheId();
const clock = useFakeTimers({ const clock = useFakeTimers({
shouldAdvanceTime: true, shouldAdvanceTime: true,
@ -442,33 +349,22 @@ describe('ajax cache', function describeLibCache() {
timeToLive: 1000 * 60 * 60, timeToLive: 1000 * 60 * 60,
}); });
return ajax await ajax.request('/test-hour');
.request('/test-hour') expect(ajaxRequestSpy.calledOnce).to.be.true;
.then(() => { expect(ajaxRequestSpy.calledWith('/test-hour')).to.be.true;
expect(ajaxRequestSpy.calledOnce).to.be.true; expect(fetchStub.callCount).to.equal(1);
expect(ajaxRequestSpy.calledWith('/test-hour')).to.be.true; clock.tick(1000 * 60 * 59); // 0:59 hour
expect(fetchStub.callCount).to.equal(1); await ajax.request('/test-hour');
}) expect(fetchStub.callCount).to.equal(1);
.then(() => { clock.tick(1000 * 60 * 2); // +2 minutes => 1:01 hour
clock.tick(1000 * 60 * 59); // 0:59 hour await ajax.request('/test-hour');
}) expect(fetchStub.callCount).to.equal(2);
.then(() => ajax.request('/test-hour')) ajaxRequestSpy.restore();
.then(() => { clock.restore();
expect(fetchStub.callCount).to.equal(1); removeCacheInterceptors(ajax, indexes);
clock.tick(1000 * 60 * 61); // 1:01 hour
})
.then(() => ajax.request('/test-hour'))
.then(() => {
expect(fetchStub.callCount).to.equal(2);
})
.finally(() => {
ajaxRequestSpy.restore();
clock.restore();
removeCacheInterceptors(ajax, indexes);
});
}); });
it('invalidates invalidateUrls endpoints', () => { it('invalidates invalidateUrls endpoints', async () => {
newCacheId(); newCacheId();
const indexes = addCacheInterceptors(ajax, { const indexes = addCacheInterceptors(ajax, {
@ -482,34 +378,21 @@ describe('ajax cache', function describeLibCache() {
}, },
}; };
return ajax await ajax.request('/test-valid-url', { ...actionConfig });
.request('/test-valid-url', { ...actionConfig }) expect(fetchStub.callCount).to.equal(1);
.then(() => { await ajax.request('/test-invalid-url');
expect(fetchStub.callCount).to.equal(1); expect(fetchStub.callCount).to.equal(2);
}) // 'post' will invalidate 'own' cache and the one mentioned in config
.then(() => ajax.request('/test-invalid-url')) await ajax.request('/test-valid-url', { ...actionConfig, method: 'POST' });
.then(() => { expect(fetchStub.callCount).to.equal(3);
expect(fetchStub.callCount).to.equal(2); await ajax.request('/test-invalid-url');
}) // indicates that 'test-invalid-url' cache was removed
.then(() => // because the server registered new request
// 'post' will invalidate 'own' cache and the one mentioned in config expect(fetchStub.callCount).to.equal(4);
ajax.request('/test-valid-url', { ...actionConfig, method: 'POST' }), removeCacheInterceptors(ajax, indexes);
)
.then(() => {
expect(fetchStub.callCount).to.equal(3);
})
.then(() => ajax.request('/test-invalid-url'))
.then(() => {
// indicates that 'test-invalid-url' cache was removed
// because the server registered new request
expect(fetchStub.callCount).to.equal(4);
})
.finally(() => {
removeCacheInterceptors(ajax, indexes);
});
}); });
it('invalidates cache on a post', () => { it('invalidates cache on a post', async () => {
newCacheId(); newCacheId();
const indexes = addCacheInterceptors(ajax, { const indexes = addCacheInterceptors(ajax, {
@ -518,30 +401,21 @@ describe('ajax cache', function describeLibCache() {
}); });
const ajaxRequestSpy = spy(ajax, 'request'); const ajaxRequestSpy = spy(ajax, 'request');
return ajax await ajax.request('/test-post');
.request('/test-post') expect(ajaxRequestSpy.calledOnce).to.be.true;
.then(() => { expect(ajaxRequestSpy.calledWith('/test-post')).to.be.true;
expect(ajaxRequestSpy.calledOnce).to.be.true; expect(fetchStub.callCount).to.equal(1);
expect(ajaxRequestSpy.calledWith('/test-post')).to.be.true; await ajax.request('/test-post', { method: 'POST', body: 'data-post' });
expect(fetchStub.callCount).to.equal(1); expect(ajaxRequestSpy.calledTwice).to.be.true;
}) expect(ajaxRequestSpy.calledWith('/test-post')).to.be.true;
.then(() => ajax.request('/test-post', { method: 'POST', body: 'data-post' })) expect(fetchStub.callCount).to.equal(2);
.then(() => { await ajax.request('/test-post');
expect(ajaxRequestSpy.calledTwice).to.be.true; expect(fetchStub.callCount).to.equal(3);
expect(ajaxRequestSpy.calledWith('/test-post')).to.be.true; ajaxRequestSpy.restore();
expect(fetchStub.callCount).to.equal(2); removeCacheInterceptors(ajax, indexes);
})
.then(() => ajax.request('/test-post'))
.then(() => {
expect(fetchStub.callCount).to.equal(3);
})
.finally(() => {
ajaxRequestSpy.restore();
removeCacheInterceptors(ajax, indexes);
});
}); });
it('caches response but does not return it when expiration time is 0', () => { it('caches response but does not return it when expiration time is 0', async () => {
newCacheId(); newCacheId();
const indexes = addCacheInterceptors(ajax, { const indexes = addCacheInterceptors(ajax, {
@ -551,46 +425,32 @@ describe('ajax cache', function describeLibCache() {
const ajaxRequestSpy = spy(ajax, 'request'); const ajaxRequestSpy = spy(ajax, 'request');
return ajax await ajax.request('/test');
.request('/test') const clock = useFakeTimers();
.then(() => { expect(ajaxRequestSpy.calledOnce).to.be.true;
const clock = useFakeTimers(); expect(ajaxRequestSpy.calledWith('/test')).to.be.true;
expect(ajaxRequestSpy.calledOnce).to.be.true; clock.tick(1);
expect(ajaxRequestSpy.calledWith('/test')).to.be.true; clock.restore();
clock.tick(1); await ajax.request('/test');
clock.restore(); expect(fetchStub.callCount).to.equal(2);
}) ajaxRequestSpy.restore();
.then(() => ajax.request('/test')) removeCacheInterceptors(ajax, indexes);
.then(() => {
expect(fetchStub.callCount).to.equal(2);
})
.finally(() => {
ajaxRequestSpy.restore();
removeCacheInterceptors(ajax, indexes);
});
}); });
it('does not use cache when `useCache: false` in the action', () => { it('does not use cache when `useCache: false` in the action', async () => {
newCacheId(); newCacheId();
getCacheIdentifier = () => 'cacheIdentifier2'; getCacheIdentifier = () => 'cacheIdentifier2';
const ajaxAlwaysRequestSpy = spy(ajax, 'request'); const ajaxAlwaysRequestSpy = spy(ajax, 'request');
const indexes = addCacheInterceptors(ajax, { useCache: true }); const indexes = addCacheInterceptors(ajax, { useCache: true });
return ajax await ajax.request('/test');
.request('/test') expect(ajaxAlwaysRequestSpy.calledOnce, 'calledOnce').to.be.true;
.then(() => { expect(ajaxAlwaysRequestSpy.calledWith('/test'));
expect(ajaxAlwaysRequestSpy.calledOnce, 'calledOnce').to.be.true; await ajax.request('/test', { cacheOptions: { useCache: false } });
expect(ajaxAlwaysRequestSpy.calledWith('/test')); expect(fetchStub.callCount).to.equal(2);
}) ajaxAlwaysRequestSpy.restore();
.then(() => ajax.request('/test', { cacheOptions: { useCache: false } })) removeCacheInterceptors(ajax, indexes);
.then(() => {
expect(fetchStub.callCount).to.equal(2);
})
.finally(() => {
ajaxAlwaysRequestSpy.restore();
removeCacheInterceptors(ajax, indexes);
});
}); });
}); });
}); });