1
1
/**
2
2
* @jest -environment jsdom
3
3
*/
4
- import axios from 'axios' ;
5
- import MockAdapter from 'axios-mock-adapter' ;
6
4
import makeServiceWorkerEnv from 'service-worker-mock' ;
7
5
import fetch from 'node-fetch' ;
8
6
9
7
import { ApiType , setAuthToken , invalidateCaches , CacheTarget } from '../../index' ;
10
8
import { cacheStillValid , EXPIRY_HEADER_KEY } from '../../utils/cacheHandlers' ;
11
9
12
10
import '../../../jest-setup' ;
13
- import { constructFixtureFindTiles } from './fixtures.findTiles' ;
11
+ import { AUTH_TOKEN , mockNetwork } from './testUtils.findTiles' ;
12
+ import { constructFixtureFindTilesCatalog } from './fixtures.S1GRDAWSLayer' ;
14
13
import { constructFixtureGetMap } from './fixtures.getMap' ;
15
14
import { constructFixtureUpdateLayerFromServiceIfNeeded } from './fixtures.BYOCLayer' ;
16
15
17
- const mockNetwork = new MockAdapter ( axios ) ;
18
-
19
- const EXAMPLE_TOKEN = 'TOKEN111' ;
20
-
21
16
describe ( 'Testing caching' , ( ) => {
22
17
beforeEach ( async ( ) => {
23
18
Object . assign ( global , makeServiceWorkerEnv ( ) , fetch ) ; // adds these functions to the global object
24
19
await invalidateCaches ( ) ;
25
- setAuthToken ( undefined ) ;
20
+ setAuthToken ( AUTH_TOKEN ) ;
26
21
} ) ;
27
22
28
23
it ( 'should fetch a request and cache it, where 2nd request is served from the cache' , async ( ) => {
29
24
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles, expectedResultHasMore } =
30
- constructFixtureFindTiles ( { } ) ;
25
+ constructFixtureFindTilesCatalog ( { } ) ;
31
26
const requestsConfig = {
32
27
cache : {
33
28
expiresIn : 60 ,
@@ -47,7 +42,7 @@ describe('Testing caching', () => {
47
42
48
43
it ( 'should make a 2nd request after the cache has expired' , async ( ) => {
49
44
const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles, expectedResultHasMore } =
50
- constructFixtureFindTiles ( { } ) ;
45
+ constructFixtureFindTilesCatalog ( { } ) ;
51
46
const requestsConfig = {
52
47
cache : {
53
48
expiresIn : 1 ,
@@ -86,12 +81,19 @@ describe('Testing caching', () => {
86
81
} ) ;
87
82
88
83
it ( 'test that no responses are cached' , async ( ) => {
89
- const { fromTime, toTime, bbox, layer, mockedResponse } = constructFixtureFindTiles ( { } ) ;
84
+ const { fromTime, toTime, bbox, layer, mockedResponse } = constructFixtureFindTilesCatalog ( { } ) ;
90
85
mockNetwork . reset ( ) ;
91
86
mockNetwork . onPost ( ) . replyOnce ( 200 , mockedResponse ) ;
92
87
mockNetwork . onPost ( ) . replyOnce ( 200 , mockedResponse ) ;
93
88
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 ) ;
95
97
await layer . findTiles ( bbox , fromTime , toTime ) ;
96
98
97
99
expect ( mockNetwork . history . post . length ) . toBe ( 2 ) ;
@@ -101,7 +103,6 @@ describe('Testing caching', () => {
101
103
// arrayBuffer needs to be used, and removing this will cause getMap to fetch a blob, as window.Blob was created with jsdom
102
104
window . Blob = undefined ;
103
105
const { layer, getMapParams, mockedResponse } = constructFixtureGetMap ( ) ;
104
- setAuthToken ( EXAMPLE_TOKEN ) ;
105
106
mockNetwork . reset ( ) ;
106
107
mockNetwork . onPost ( ) . replyOnce ( 200 , mockedResponse ) ;
107
108
mockNetwork . onPost ( ) . replyOnce ( 200 , mockedResponse ) ;
@@ -121,7 +122,6 @@ describe('Testing caching', () => {
121
122
} ,
122
123
} ;
123
124
const { layer, getMapParams, mockedResponse } = constructFixtureGetMap ( ) ;
124
- setAuthToken ( EXAMPLE_TOKEN ) ;
125
125
mockNetwork . reset ( ) ;
126
126
mockNetwork . onPost ( ) . replyOnce ( 200 , mockedResponse ) ;
127
127
mockNetwork . onPost ( ) . replyOnce ( 200 , mockedResponse ) ;
@@ -153,7 +153,6 @@ describe('Testing caching', () => {
153
153
} ,
154
154
} ;
155
155
156
- setAuthToken ( EXAMPLE_TOKEN ) ;
157
156
mockNetwork . reset ( ) ;
158
157
mockNetwork . onPost ( ) . reply ( mockedResponse ) ;
159
158
mockNetwork . onPost ( ) . replyOnce ( 200 , mockedResponse ) ;
@@ -169,13 +168,12 @@ describe('Testing cache targets', () => {
169
168
beforeEach ( async ( ) => {
170
169
Object . assign ( global , makeServiceWorkerEnv ( ) , fetch ) ; // adds these functions to the global object
171
170
await invalidateCaches ( ) ;
172
- setAuthToken ( undefined ) ;
171
+ setAuthToken ( AUTH_TOKEN ) ;
173
172
} ) ;
174
173
175
174
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 ( { } ) ;
179
177
const requestsConfig = {
180
178
cache : {
181
179
expiresIn : 60 ,
@@ -200,9 +198,8 @@ describe('Testing cache targets', () => {
200
198
} ) ;
201
199
202
200
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 ( { } ) ;
206
203
const requestsConfig = {
207
204
cache : {
208
205
expiresIn : 60 ,
@@ -234,9 +231,8 @@ describe('Testing cache targets', () => {
234
231
} ) ;
235
232
236
233
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 ( { } ) ;
240
236
const requestsConfig = {
241
237
cache : {
242
238
expiresIn : 60 ,
@@ -261,9 +257,8 @@ describe('Testing cache targets', () => {
261
257
} ) ;
262
258
263
259
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 ( { } ) ;
267
262
const reqConfigCacheApi = {
268
263
cache : {
269
264
expiresIn : 1 ,
@@ -337,13 +332,12 @@ describe('Testing cache targets when cache_api is not available', () => {
337
332
beforeEach ( async ( ) => {
338
333
Object . assign ( global , { caches : undefined } , fetch ) ; // adds these functions to the global object and removes caches from global object
339
334
await invalidateCaches ( ) ;
340
- setAuthToken ( undefined ) ;
335
+ setAuthToken ( AUTH_TOKEN ) ;
341
336
} ) ;
342
337
343
338
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 ( { } ) ;
347
341
const requestsConfig = {
348
342
cache : {
349
343
expiresIn : 60 ,
@@ -376,9 +370,8 @@ describe('Testing cache targets when cache_api is not available', () => {
376
370
} ) ;
377
371
378
372
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 ( { } ) ;
382
375
const requestsConfig = {
383
376
cache : {
384
377
expiresIn : 60 ,
@@ -411,13 +404,12 @@ describe('Reading from cache twice', () => {
411
404
beforeEach ( async ( ) => {
412
405
Object . assign ( global , makeServiceWorkerEnv ( ) , fetch ) ; // adds these functions to the global object and removes caches from global object
413
406
await invalidateCaches ( ) ;
414
- setAuthToken ( undefined ) ;
407
+ setAuthToken ( AUTH_TOKEN ) ;
415
408
} ) ;
416
409
417
410
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 ( { } ) ;
421
413
const requestsConfig = {
422
414
cache : {
423
415
expiresIn : 60 ,
@@ -442,9 +434,8 @@ describe('Reading from cache twice', () => {
442
434
} ) ;
443
435
444
436
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 ( { } ) ;
448
439
const requestsConfig = {
449
440
cache : {
450
441
expiresIn : 60 ,
@@ -491,7 +482,7 @@ describe('Unit test for aux request caching', () => {
491
482
beforeEach ( async ( ) => {
492
483
Object . assign ( global , makeServiceWorkerEnv ( ) , fetch ) ; // adds these functions to the global object
493
484
await invalidateCaches ( ) ;
494
- setAuthToken ( undefined ) ;
485
+ setAuthToken ( AUTH_TOKEN ) ;
495
486
} ) ;
496
487
497
488
const listOfRequstConfigs = [
@@ -532,7 +523,6 @@ describe('Unit test for aux request caching', () => {
532
523
it . each ( [ ...listOfRequstConfigs ] ) (
533
524
'It should be cache aux request to memory by default' ,
534
525
async ( requestConfig ) => {
535
- setAuthToken ( EXAMPLE_TOKEN ) ;
536
526
const { layer, mockedResponse, expectedLayerParams } = constructFixtureUpdateLayerFromServiceIfNeeded (
537
527
{ } ,
538
528
) ;
@@ -555,7 +545,6 @@ describe('Unit test for aux request caching', () => {
555
545
} ,
556
546
) ;
557
547
it ( 'It should not cache aux request when cache is disabled' , async ( ) => {
558
- setAuthToken ( EXAMPLE_TOKEN ) ;
559
548
const { layer, mockedResponse, expectedLayerParams } = constructFixtureUpdateLayerFromServiceIfNeeded ( { } ) ;
560
549
const requestsConfig = {
561
550
cache : {
0 commit comments