11/**
22 * @jest -environment jsdom
33 */
4- import axios from 'axios' ;
5- import MockAdapter from 'axios-mock-adapter' ;
64import makeServiceWorkerEnv from 'service-worker-mock' ;
75import fetch from 'node-fetch' ;
86
97import { ApiType , setAuthToken , invalidateCaches , CacheTarget } from '../../index' ;
108import { cacheStillValid , EXPIRY_HEADER_KEY } from '../../utils/cacheHandlers' ;
119
1210import '../../../jest-setup' ;
13- import { constructFixtureFindTiles } from './fixtures.findTiles' ;
11+ import { AUTH_TOKEN , mockNetwork } from './testUtils.findTiles' ;
12+ import { constructFixtureFindTilesCatalog } from './fixtures.S1GRDAWSLayer' ;
1413import { constructFixtureGetMap } from './fixtures.getMap' ;
1514import { constructFixtureUpdateLayerFromServiceIfNeeded } from './fixtures.BYOCLayer' ;
1615
17- const mockNetwork = new MockAdapter ( axios ) ;
18-
1916const EXAMPLE_TOKEN = 'TOKEN111' ;
2017
2118describe ( 'Testing caching' , ( ) => {
2219 beforeEach ( async ( ) => {
2320 Object . assign ( global , makeServiceWorkerEnv ( ) , fetch ) ; // adds these functions to the global object
2421 await invalidateCaches ( ) ;
25- setAuthToken ( undefined ) ;
22+ setAuthToken ( AUTH_TOKEN ) ;
2623 } ) ;
2724
2825 it ( 'should fetch a request and cache it, where 2nd request is served from the cache' , async ( ) => {
2926 const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles, expectedResultHasMore } =
30- constructFixtureFindTiles ( { } ) ;
27+ constructFixtureFindTilesCatalog ( { } ) ;
3128 const requestsConfig = {
3229 cache : {
3330 expiresIn : 60 ,
@@ -47,7 +44,7 @@ describe('Testing caching', () => {
4744
4845 it ( 'should make a 2nd request after the cache has expired' , async ( ) => {
4946 const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles, expectedResultHasMore } =
50- constructFixtureFindTiles ( { } ) ;
47+ constructFixtureFindTilesCatalog ( { } ) ;
5148 const requestsConfig = {
5249 cache : {
5350 expiresIn : 1 ,
@@ -86,12 +83,19 @@ describe('Testing caching', () => {
8683 } ) ;
8784
8885 it ( 'test that no responses are cached' , async ( ) => {
89- const { fromTime, toTime, bbox, layer, mockedResponse } = constructFixtureFindTiles ( { } ) ;
86+ const { fromTime, toTime, bbox, layer, mockedResponse } = constructFixtureFindTilesCatalog ( { } ) ;
9087 mockNetwork . reset ( ) ;
9188 mockNetwork . onPost ( ) . replyOnce ( 200 , mockedResponse ) ;
9289 mockNetwork . onPost ( ) . replyOnce ( 200 , mockedResponse ) ;
9390
94- await layer . findTiles ( bbox , fromTime , toTime ) ;
91+ // findTilesUsingCatalog have a 30-minute cache by default
92+ const requestsConfig = {
93+ cache : {
94+ expiresIn : 0 ,
95+ } ,
96+ } ;
97+
98+ await layer . findTiles ( bbox , fromTime , toTime , null , null , requestsConfig ) ;
9599 await layer . findTiles ( bbox , fromTime , toTime ) ;
96100
97101 expect ( mockNetwork . history . post . length ) . toBe ( 2 ) ;
@@ -169,13 +173,12 @@ describe('Testing cache targets', () => {
169173 beforeEach ( async ( ) => {
170174 Object . assign ( global , makeServiceWorkerEnv ( ) , fetch ) ; // adds these functions to the global object
171175 await invalidateCaches ( ) ;
172- setAuthToken ( undefined ) ;
176+ setAuthToken ( AUTH_TOKEN ) ;
173177 } ) ;
174178
175179 it ( 'should cache to cache api' , async ( ) => {
176- const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles (
177- { } ,
178- ) ;
180+ const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
181+ constructFixtureFindTilesCatalog ( { } ) ;
179182 const requestsConfig = {
180183 cache : {
181184 expiresIn : 60 ,
@@ -200,9 +203,8 @@ describe('Testing cache targets', () => {
200203 } ) ;
201204
202205 it ( 'should cache to memory' , async ( ) => {
203- const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles (
204- { } ,
205- ) ;
206+ const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
207+ constructFixtureFindTilesCatalog ( { } ) ;
206208 const requestsConfig = {
207209 cache : {
208210 expiresIn : 60 ,
@@ -234,9 +236,8 @@ describe('Testing cache targets', () => {
234236 } ) ;
235237
236238 it ( 'should default to caching to cache_api' , async ( ) => {
237- const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles (
238- { } ,
239- ) ;
239+ const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
240+ constructFixtureFindTilesCatalog ( { } ) ;
240241 const requestsConfig = {
241242 cache : {
242243 expiresIn : 60 ,
@@ -261,9 +262,8 @@ describe('Testing cache targets', () => {
261262 } ) ;
262263
263264 it ( 'should invalidate caches' , async ( ) => {
264- const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles (
265- { } ,
266- ) ;
265+ const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
266+ constructFixtureFindTilesCatalog ( { } ) ;
267267 const reqConfigCacheApi = {
268268 cache : {
269269 expiresIn : 1 ,
@@ -337,13 +337,12 @@ describe('Testing cache targets when cache_api is not available', () => {
337337 beforeEach ( async ( ) => {
338338 Object . assign ( global , { caches : undefined } , fetch ) ; // adds these functions to the global object and removes caches from global object
339339 await invalidateCaches ( ) ;
340- setAuthToken ( undefined ) ;
340+ setAuthToken ( AUTH_TOKEN ) ;
341341 } ) ;
342342
343343 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- ) ;
344+ const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
345+ constructFixtureFindTilesCatalog ( { } ) ;
347346 const requestsConfig = {
348347 cache : {
349348 expiresIn : 60 ,
@@ -376,9 +375,8 @@ describe('Testing cache targets when cache_api is not available', () => {
376375 } ) ;
377376
378377 it ( 'should not use cache if cache-api is specified as target' , async ( ) => {
379- const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles (
380- { } ,
381- ) ;
378+ const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
379+ constructFixtureFindTilesCatalog ( { } ) ;
382380 const requestsConfig = {
383381 cache : {
384382 expiresIn : 60 ,
@@ -411,13 +409,12 @@ describe('Reading from cache twice', () => {
411409 beforeEach ( async ( ) => {
412410 Object . assign ( global , makeServiceWorkerEnv ( ) , fetch ) ; // adds these functions to the global object and removes caches from global object
413411 await invalidateCaches ( ) ;
414- setAuthToken ( undefined ) ;
412+ setAuthToken ( AUTH_TOKEN ) ;
415413 } ) ;
416414
417415 it ( 'should read from cache-api twice' , async ( ) => {
418- const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles (
419- { } ,
420- ) ;
416+ const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
417+ constructFixtureFindTilesCatalog ( { } ) ;
421418 const requestsConfig = {
422419 cache : {
423420 expiresIn : 60 ,
@@ -442,9 +439,8 @@ describe('Reading from cache twice', () => {
442439 } ) ;
443440
444441 it ( 'should read from memory cache twice' , async ( ) => {
445- const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } = constructFixtureFindTiles (
446- { } ,
447- ) ;
442+ const { fromTime, toTime, bbox, layer, mockedResponse, expectedResultTiles } =
443+ constructFixtureFindTilesCatalog ( { } ) ;
448444 const requestsConfig = {
449445 cache : {
450446 expiresIn : 60 ,
0 commit comments