Skip to content

Commit 2fa4425

Browse files
committed
change cache test to mock catalog api instead of index service
1 parent 6ae6547 commit 2fa4425

File tree

1 file changed

+34
-45
lines changed

1 file changed

+34
-45
lines changed

src/layer/__tests__/cache.ts

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
11
/**
22
* @jest-environment jsdom
33
*/
4-
import axios from 'axios';
5-
import MockAdapter from 'axios-mock-adapter';
64
import makeServiceWorkerEnv from 'service-worker-mock';
75
import fetch from 'node-fetch';
86

97
import { ApiType, setAuthToken, invalidateCaches, CacheTarget } from '../../index';
108
import { cacheStillValid, EXPIRY_HEADER_KEY } from '../../utils/cacheHandlers';
119

1210
import '../../../jest-setup';
13-
import { constructFixtureFindTiles } from './fixtures.findTiles';
11+
import { AUTH_TOKEN, mockNetwork } from './testUtils.findTiles';
12+
import { constructFixtureFindTilesCatalog } from './fixtures.S1GRDAWSLayer';
1413
import { constructFixtureGetMap } from './fixtures.getMap';
1514
import { constructFixtureUpdateLayerFromServiceIfNeeded } from './fixtures.BYOCLayer';
1615

17-
const mockNetwork = new MockAdapter(axios);
18-
19-
const EXAMPLE_TOKEN = 'TOKEN111';
20-
2116
describe('Testing caching', () => {
2217
beforeEach(async () => {
2318
Object.assign(global, makeServiceWorkerEnv(), fetch); // adds these functions to the global object
2419
await invalidateCaches();
25-
setAuthToken(undefined);
20+
setAuthToken(AUTH_TOKEN);
2621
});
2722

2823
it('should fetch a request and cache it, where 2nd request is served from the cache', async () => {
2924
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles, expectedResultHasMore } =
30-
constructFixtureFindTiles({});
25+
constructFixtureFindTilesCatalog({});
3126
const requestsConfig = {
3227
cache: {
3328
expiresIn: 60,
@@ -47,7 +42,7 @@ describe('Testing caching', () => {
4742

4843
it('should make a 2nd request after the cache has expired', async () => {
4944
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles, expectedResultHasMore } =
50-
constructFixtureFindTiles({});
45+
constructFixtureFindTilesCatalog({});
5146
const requestsConfig = {
5247
cache: {
5348
expiresIn: 1,
@@ -86,12 +81,19 @@ describe('Testing caching', () => {
8681
});
8782

8883
it('test that no responses are cached', async () => {
89-
const { fromTime, toTime, bbox, layer, mockedResponse } = constructFixtureFindTiles({});
84+
const { fromTime, toTime, bbox, layer, mockedResponse } = constructFixtureFindTilesCatalog({});
9085
mockNetwork.reset();
9186
mockNetwork.onPost().replyOnce(200, mockedResponse);
9287
mockNetwork.onPost().replyOnce(200, mockedResponse);
9388

94-
await layer.findTiles(bbox, fromTime, toTime);
89+
// findTilesUsingCatalog have a 30-minute cache by default
90+
const requestsConfig = {
91+
cache: {
92+
expiresIn: 0,
93+
},
94+
};
95+
96+
await layer.findTiles(bbox, fromTime, toTime, null, null, requestsConfig);
9597
await layer.findTiles(bbox, fromTime, toTime);
9698

9799
expect(mockNetwork.history.post.length).toBe(2);
@@ -101,7 +103,6 @@ describe('Testing caching', () => {
101103
// arrayBuffer needs to be used, and removing this will cause getMap to fetch a blob, as window.Blob was created with jsdom
102104
window.Blob = undefined;
103105
const { layer, getMapParams, mockedResponse } = constructFixtureGetMap();
104-
setAuthToken(EXAMPLE_TOKEN);
105106
mockNetwork.reset();
106107
mockNetwork.onPost().replyOnce(200, mockedResponse);
107108
mockNetwork.onPost().replyOnce(200, mockedResponse);
@@ -121,7 +122,6 @@ describe('Testing caching', () => {
121122
},
122123
};
123124
const { layer, getMapParams, mockedResponse } = constructFixtureGetMap();
124-
setAuthToken(EXAMPLE_TOKEN);
125125
mockNetwork.reset();
126126
mockNetwork.onPost().replyOnce(200, mockedResponse);
127127
mockNetwork.onPost().replyOnce(200, mockedResponse);
@@ -153,7 +153,6 @@ describe('Testing caching', () => {
153153
},
154154
};
155155

156-
setAuthToken(EXAMPLE_TOKEN);
157156
mockNetwork.reset();
158157
mockNetwork.onPost().reply(mockedResponse);
159158
mockNetwork.onPost().replyOnce(200, mockedResponse);
@@ -169,13 +168,12 @@ describe('Testing cache targets', () => {
169168
beforeEach(async () => {
170169
Object.assign(global, makeServiceWorkerEnv(), fetch); // adds these functions to the global object
171170
await invalidateCaches();
172-
setAuthToken(undefined);
171+
setAuthToken(AUTH_TOKEN);
173172
});
174173

175174
it('should cache to cache api', async () => {
176-
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles(
177-
{},
178-
);
175+
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
176+
constructFixtureFindTilesCatalog({});
179177
const requestsConfig = {
180178
cache: {
181179
expiresIn: 60,
@@ -200,9 +198,8 @@ describe('Testing cache targets', () => {
200198
});
201199

202200
it('should cache to memory', async () => {
203-
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles(
204-
{},
205-
);
201+
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
202+
constructFixtureFindTilesCatalog({});
206203
const requestsConfig = {
207204
cache: {
208205
expiresIn: 60,
@@ -234,9 +231,8 @@ describe('Testing cache targets', () => {
234231
});
235232

236233
it('should default to caching to cache_api', async () => {
237-
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles(
238-
{},
239-
);
234+
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
235+
constructFixtureFindTilesCatalog({});
240236
const requestsConfig = {
241237
cache: {
242238
expiresIn: 60,
@@ -261,9 +257,8 @@ describe('Testing cache targets', () => {
261257
});
262258

263259
it('should invalidate caches', async () => {
264-
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles(
265-
{},
266-
);
260+
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
261+
constructFixtureFindTilesCatalog({});
267262
const reqConfigCacheApi = {
268263
cache: {
269264
expiresIn: 1,
@@ -337,13 +332,12 @@ describe('Testing cache targets when cache_api is not available', () => {
337332
beforeEach(async () => {
338333
Object.assign(global, { caches: undefined }, fetch); // adds these functions to the global object and removes caches from global object
339334
await invalidateCaches();
340-
setAuthToken(undefined);
335+
setAuthToken(AUTH_TOKEN);
341336
});
342337

343338
it('should default to memory if window.caches is undefined and no targets were defined', async () => {
344-
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles(
345-
{},
346-
);
339+
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
340+
constructFixtureFindTilesCatalog({});
347341
const requestsConfig = {
348342
cache: {
349343
expiresIn: 60,
@@ -376,9 +370,8 @@ describe('Testing cache targets when cache_api is not available', () => {
376370
});
377371

378372
it('should not use cache if cache-api is specified as target', async () => {
379-
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles(
380-
{},
381-
);
373+
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
374+
constructFixtureFindTilesCatalog({});
382375
const requestsConfig = {
383376
cache: {
384377
expiresIn: 60,
@@ -411,13 +404,12 @@ describe('Reading from cache twice', () => {
411404
beforeEach(async () => {
412405
Object.assign(global, makeServiceWorkerEnv(), fetch); // adds these functions to the global object and removes caches from global object
413406
await invalidateCaches();
414-
setAuthToken(undefined);
407+
setAuthToken(AUTH_TOKEN);
415408
});
416409

417410
it('should read from cache-api twice', async () => {
418-
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles(
419-
{},
420-
);
411+
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
412+
constructFixtureFindTilesCatalog({});
421413
const requestsConfig = {
422414
cache: {
423415
expiresIn: 60,
@@ -442,9 +434,8 @@ describe('Reading from cache twice', () => {
442434
});
443435

444436
it('should read from memory cache twice', async () => {
445-
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles(
446-
{},
447-
);
437+
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
438+
constructFixtureFindTilesCatalog({});
448439
const requestsConfig = {
449440
cache: {
450441
expiresIn: 60,
@@ -491,7 +482,7 @@ describe('Unit test for aux request caching', () => {
491482
beforeEach(async () => {
492483
Object.assign(global, makeServiceWorkerEnv(), fetch); // adds these functions to the global object
493484
await invalidateCaches();
494-
setAuthToken(undefined);
485+
setAuthToken(AUTH_TOKEN);
495486
});
496487

497488
const listOfRequstConfigs = [
@@ -532,7 +523,6 @@ describe('Unit test for aux request caching', () => {
532523
it.each([...listOfRequstConfigs])(
533524
'It should be cache aux request to memory by default',
534525
async (requestConfig) => {
535-
setAuthToken(EXAMPLE_TOKEN);
536526
const { layer, mockedResponse, expectedLayerParams } = constructFixtureUpdateLayerFromServiceIfNeeded(
537527
{},
538528
);
@@ -555,7 +545,6 @@ describe('Unit test for aux request caching', () => {
555545
},
556546
);
557547
it('It should not cache aux request when cache is disabled', async () => {
558-
setAuthToken(EXAMPLE_TOKEN);
559548
const { layer, mockedResponse, expectedLayerParams } = constructFixtureUpdateLayerFromServiceIfNeeded({});
560549
const requestsConfig = {
561550
cache: {

0 commit comments

Comments
 (0)