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