-
Notifications
You must be signed in to change notification settings - Fork 830
/
Copy pathtest-PrecacheController.mjs
749 lines (618 loc) · 26 KB
/
test-PrecacheController.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {expect} from 'chai';
import sinon from 'sinon';
import {reset as iDBReset} from 'shelving-mock-indexeddb';
import expectError from '../../../../infra/testing/expectError';
import generateTestVariants from '../../../../infra/testing/generate-variant-tests';
import {prodOnly, devOnly} from '../../../../infra/testing/env-it';
import {_private} from '../../../../packages/workbox-core/index.mjs';
import {logger} from '../../../../packages/workbox-core/_private/logger.mjs';
import PrecacheController from '../../../../packages/workbox-precaching/controllers/PrecacheController.mjs';
import {fetchWrapper} from '../../../../packages/workbox-core/_private/fetchWrapper.mjs';
import {cacheWrapper} from '../../../../packages/workbox-core/_private/cacheWrapper.mjs';
const {cacheNames} = _private;
describe(`[workbox-precaching] PrecacheController`, function() {
const sandbox = sinon.createSandbox();
beforeEach(async function() {
let usedCacheNames = await caches.keys();
await Promise.all(usedCacheNames.map((cacheName) => {
return caches.delete(cacheName);
}));
iDBReset();
// Run this in the `beforeEach` hook as well as the afterEach hook due to
// a mocha bug where `afterEach` hooks aren't run for skipped tests.
// https://github.com/mochajs/mocha/issues/2546
sandbox.restore();
});
after(function() {
sandbox.restore();
});
describe(`constructor`, function() {
it(`should construct without any inputs`, async function() {
expect(() => {
new PrecacheController();
}).to.not.throw();
});
it(`should construct with a valid cache name`, async function() {
expect(() => {
new PrecacheController(`test-cache-name`);
}).to.not.throw();
});
// TODO Add tests on bad cachename input
});
describe(`addToCacheList()`, function() {
const badTopLevelInputs = [
{},
true,
false,
123,
'',
null,
undefined,
];
generateTestVariants(`should throw when passing in non-array values in dev`, badTopLevelInputs, async function(variant) {
if (process.env.NODE_ENV == 'production') return this.skip();
const precacheController = new PrecacheController();
return expectError(() => {
precacheController.addToCacheList(variant);
}, 'not-an-array');
});
const badNestedInputs = [
true,
false,
123,
null,
undefined,
[],
'',
{},
];
generateTestVariants(`should throw when passing in invalid inputs in the array in dev`, badNestedInputs, async function(variant) {
if (process.env.NODE_ENV == 'production') return this.skip();
const precacheController = new PrecacheController();
return expectError(() => {
precacheController.addToCacheList([variant]);
}, 'add-to-cache-list-unexpected-type', (err) => {
expect(err.details.entry).to.deep.equal(variant);
});
});
const unrevisionedEntryGroups = {
'string entries': [
'/',
'/hello.html',
'/styles/hello.css',
'/scripts/controllers/hello.js',
],
'url only object entries': [
{url: '/'},
{url: '/hello.html'},
{url: '/styles/hello.css'},
{url: '/scripts/controllers/hello.js'},
],
};
Object.keys(unrevisionedEntryGroups).map((groupName) => {
const inputGroup = unrevisionedEntryGroups[groupName];
devOnly.it(`should add ${groupName} to cache list in dev`, async function() {
const precacheController = new PrecacheController();
precacheController.addToCacheList(inputGroup);
expect(precacheController._entriesToCacheMap.size).to.equal(inputGroup.length);
inputGroup.forEach((inputValue) => {
const urlValue = inputValue.url || inputValue;
const entry = precacheController._entriesToCacheMap.get(urlValue);
expect(entry._entryId).to.equal(urlValue);
expect(entry._revision).to.equal(urlValue);
});
});
it(`should remove duplicate ${groupName}`, async function() {
const precacheController = new PrecacheController();
const inputUrls = [
...inputGroup,
...inputGroup,
];
precacheController.addToCacheList(inputUrls);
expect(precacheController._entriesToCacheMap.size).to.equal(inputGroup.length);
inputGroup.forEach((inputValue) => {
const urlValue = inputValue.url || inputValue;
const entry = precacheController._entriesToCacheMap.get(urlValue);
expect(entry._entryId).to.equal(urlValue);
expect(entry._revision).to.equal(urlValue);
});
});
});
it(`should add url + revision objects to cache list`, async function() {
const precacheController = new PrecacheController();
const inputObjects = [
{url: '/', revision: '123'},
{url: '/hello.html', revision: '123'},
{url: '/styles/hello.css', revision: '123'},
{url: '/scripts/controllers/hello.js', revision: '123'},
];
precacheController.addToCacheList(inputObjects);
expect(precacheController._entriesToCacheMap.size).to.equal(inputObjects.length);
inputObjects.forEach((inputObject) => {
const entry = precacheController._entriesToCacheMap.get(inputObject.url);
expect(entry._entryId).to.equal(inputObject.url);
expect(entry._revision).to.equal(inputObject.revision);
});
});
it(`should remove duplicate url + revision object entries`, async function() {
const precacheController = new PrecacheController();
const singleObject = {url: '/duplicate.html', revision: '123'};
const inputObjects = [
singleObject,
singleObject,
];
precacheController.addToCacheList(inputObjects);
expect(precacheController._entriesToCacheMap.size).to.equal(1);
const entry = precacheController._entriesToCacheMap.get(singleObject.url);
expect(entry._entryId).to.equal(singleObject.url);
expect(entry._revision).to.equal(singleObject.revision);
});
it(`should throw on conflicting entries with different revisions`, async function() {
const firstEntry = {url: '/duplicate.html', revision: '123'};
const secondEntry = {url: '/duplicate.html', revision: '456'};
return expectError(() => {
const precacheController = new PrecacheController();
const inputObjects = [
firstEntry,
secondEntry,
];
precacheController.addToCacheList(inputObjects);
}, 'add-to-cache-list-conflicting-entries', (err) => {
expect(err.details.firstEntry).to.deep.equal(firstEntry);
expect(err.details.secondEntry).to.deep.equal(secondEntry);
});
});
});
describe('install()', function() {
it('should be fine when calling with empty precache list', async function() {
const precacheController = new PrecacheController();
return precacheController.install();
});
devOnly.it('should not print warnings if suppressWarnings is passed in', async function() {
const precacheController = new PrecacheController();
precacheController.addToCacheList(['/']);
await precacheController.install({
suppressWarnings: true,
});
expect(logger.warn.callCount).to.equal(0);
await precacheController.install({
suppressWarnings: false,
});
expect(logger.warn.callCount).to.be.gt(0);
});
it('should precache assets (with cache busting via search params)', async function() {
const precacheController = new PrecacheController();
const cacheList = [
'/index.1234.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
{url: '/scripts/stress.js?test=search&foo=bar', revision: '1234'},
];
precacheController.addToCacheList(cacheList);
// Reset as addToCacheList will log.
logger.log.resetHistory();
const updateInfo = await precacheController.install();
expect(updateInfo.updatedEntries.length).to.equal(cacheList.length);
expect(updateInfo.notUpdatedEntries.length).to.equal(0);
const cache = await caches.open(`${cacheNames.getPrecacheName()}-temp`);
const keys = await cache.keys();
expect(keys.length).to.equal(cacheList.length);
const urls = cacheList.map((entry) => entry.url || entry);
await Promise.all(urls.map(async (url) => {
const cachedResponse = await cache.match(url);
expect(cachedResponse).to.exist;
}));
if (process.env.NODE_ENV != 'production') {
// Make sure we print some debug info.
expect(logger.log.callCount).to.be.gt(0);
}
});
prodOnly.it('should not log install details in production', async function() {
const precacheController = new PrecacheController();
precacheController.addToCacheList([
'/index.1234.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
]);
await precacheController.install();
expect(logger.log.callCount).to.equal(0);
});
it(`should clean redirected precache entries`, async function() {
const fetchStub = sandbox.stub(global, 'fetch');
fetchStub.callsFake(() => {
const response = new Response('Redirected Response');
response.redirected = true;
return response;
});
const precacheController = new PrecacheController();
precacheController.addToCacheList([
'/index.1234.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
]);
await precacheController.install();
});
it(`should use the desired cache name`, async function() {
const precacheController = new PrecacheController(`test-cache-name`);
const cacheList = [
{url: '/scripts/index.js', revision: '1234'},
{url: '/index.html', revision: '1234'},
];
precacheController.addToCacheList(cacheList);
await precacheController.install();
const cache = await caches.open(`test-cache-name-temp`);
const keys = await cache.keys();
expect(keys.length).to.equal(cacheList.length);
for (let i = 0; i < cacheList.length; i++) {
let cachedResponse = await cache.match(cacheList[i].url);
expect(cachedResponse).to.exist;
}
});
it('should only precache assets that have changed', async function() {
/*
First precache some entries
*/
const precacheControllerOne = new PrecacheController();
const cacheListOne = [
'/index.1234.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
{url: '/scripts/stress.js?test=search&foo=bar', revision: '1234'},
];
const urlsListOne = cacheListOne.map((entry) => entry.url || entry);
precacheControllerOne.addToCacheList(cacheListOne);
// Reset as addToCacheList will log.
logger.log.resetHistory();
const updateInfo = await precacheControllerOne.install();
expect(updateInfo.updatedEntries.length).to.equal(cacheListOne.length);
expect(updateInfo.notUpdatedEntries.length).to.equal(0);
if (process.env.NODE_ENV != 'production') {
// Make sure we print some debug info.
expect(logger.log.callCount).to.be.gt(0);
}
const tempCacheName = `${cacheNames.getPrecacheName()}-temp`;
let tempCache = await caches.open(tempCacheName);
let finalCache = await caches.open(cacheNames.getPrecacheName());
// Make sure the files are cached in the temp cache and no the final cache
const tempKeysOne = await tempCache.keys();
const finalCacheKeysOne = await finalCache.keys();
expect(tempKeysOne.length).to.equal(cacheListOne.length);
expect(finalCacheKeysOne.length).to.equal(0);
await Promise.all(urlsListOne.map(async (url) => {
const cachedResponse = await tempCache.match(url);
expect(cachedResponse).to.exist;
}));
await precacheControllerOne.activate();
// Ensure temp cache is empty
tempCache = await caches.open(tempCache);
let requests = await tempCache.keys();
expect(requests.length).to.equal(0);
// The cache mock needs the cache to be re-opened to have up-to-date keys.
finalCache = await caches.open(cacheNames.getPrecacheName());
const finalCacheKeysOneActivate = await finalCache.keys();
expect(finalCacheKeysOneActivate.length).to.equal(cacheListOne.length);
// Make sure the files are cached in the final cache
await Promise.all(urlsListOne.map(async (url) => {
const cachedResponse = await finalCache.match(url);
expect(cachedResponse).to.exist;
}));
/*
THEN precache the same URLs but two with different revisions
*/
const precacheControllerTwo = new PrecacheController();
const cacheListTwo = [
'/index.4321.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
{url: '/scripts/stress.js?test=search&foo=bar', revision: '4321'},
];
const urlsListTwo = cacheListTwo.map((entry) => entry.url || entry);
precacheControllerTwo.addToCacheList(cacheListTwo);
// Reset as addToCacheList will log.
logger.log.resetHistory();
const updateInfoTwo = await precacheControllerTwo.install();
expect(updateInfoTwo.updatedEntries.length).to.equal(2);
expect(updateInfoTwo.notUpdatedEntries.length).to.equal(2);
// The cache mock needs the cache to be re-opened to have up-to-date keys.
tempCache = await caches.open(`${cacheNames.getPrecacheName()}-temp`);
finalCache = await caches.open(cacheNames.getPrecacheName());
const tempKeysTwo = await tempCache.keys();
const finalKeysTwo = await finalCache.keys();
// Temp cache will contain the two new URLs that need to be cached.
// The final cache should be untouched until the activate step.
// It would be in the activate event that 'index.1234.html' would
// be removed from the cache and indexedDB.
expect(tempKeysTwo.length).to.equal(updateInfoTwo.updatedEntries.length);
expect(finalKeysTwo.length).to.equal(cacheListOne.length);
await Promise.all(updateInfoTwo.updatedEntries.map(async (precacheEntry) => {
const cachedResponse = await tempCache.match(precacheEntry._cacheRequest);
expect(cachedResponse).to.exist;
}));
await Promise.all(urlsListOne.map(async (url) => {
const cachedResponse = await finalCache.match(url);
expect(cachedResponse).to.exist;
}));
if (process.env.NODE_ENV != 'production') {
// Make sure we print some debug info.
expect(logger.log.callCount).to.be.gt(0);
}
await precacheControllerTwo.activate();
// Ensure temp cache is empty
tempCache = await caches.open(tempCache);
requests = await tempCache.keys();
expect(requests.length).to.equal(0);
// Cache mock needs this to update keys
finalCache = await caches.open(cacheNames.getPrecacheName());
const finalKeysTwoActivate = await finalCache.keys();
expect(finalKeysTwoActivate.length).to.equal(urlsListTwo.length);
await Promise.all(urlsListTwo.map(async (url) => {
const cachedResponse = await finalCache.match(url);
expect(cachedResponse).to.exist;
}));
});
it('it should precache with plugins', async function() {
sandbox.spy(fetchWrapper, 'fetch');
sandbox.spy(cacheWrapper, 'put');
const precacheController = new PrecacheController();
const cacheList = [
'/index.1234.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
{url: '/scripts/stress.js?test=search&foo=bar', revision: '1234'},
];
precacheController.addToCacheList(cacheList);
const testPlugins = [{
name: 'plugin1',
}, {
name: 'plugin2',
}];
await precacheController.install({
plugins: testPlugins,
});
expect(fetchWrapper.fetch.args[0][0].plugins).to.equal(testPlugins);
expect(cacheWrapper.put.args[0][0].plugins).to.equal(testPlugins);
await precacheController.activate({
plugins: testPlugins,
});
expect(cacheWrapper.put.args[1][0].plugins).to.equal(testPlugins);
});
it(`it should set credentials: 'same-origin' on the precaching requests`, async function() {
sandbox.spy(fetchWrapper, 'fetch');
const precacheController = new PrecacheController();
const cacheList = [
'/index.1234.html',
];
precacheController.addToCacheList(cacheList);
await precacheController.install();
const {request} = fetchWrapper.fetch.args[0][0];
expect(request.credentials).to.eql('same-origin');
});
it(`it should fail installation when a response with a status of 400 is received`, async function() {
sandbox.stub(fetchWrapper, 'fetch').resolves(new Response('', {
status: 400,
}));
const precacheController = new PrecacheController();
const cacheList = [
'/will-be-error.html',
];
precacheController.addToCacheList(cacheList);
return expectError(
() => precacheController.install(),
'bad-precaching-response'
);
});
it(`it should successfully install when an opaque response is received`, async function() {
sandbox.stub(fetchWrapper, 'fetch').resolves(new Response('', {
status: 0,
}));
const precacheController = new PrecacheController();
const cacheList = [
'/will-be-opaque.html',
];
precacheController.addToCacheList(cacheList);
// This should succeed.
await precacheController.install();
});
it(`it should successfully install when a response with a status of 400 is received, if a cacheWillUpdate plugin allows it`, async function() {
sandbox.stub(fetchWrapper, 'fetch').resolves(new Response('', {
status: 400,
}));
const precacheController = new PrecacheController();
const cacheList = [
'/will-be-error.html',
];
precacheController.addToCacheList(cacheList);
const plugins = [{
cacheWillUpdate: ({response}) => {
if (response.status === 400) {
return response;
}
return null;
},
}];
// This should succeed.
await precacheController.install({plugins});
});
});
describe(`activate()`, function() {
it(`should remove out of date entry`, async function() {
const cache = await caches.open(cacheNames.getPrecacheName());
/*
First precache some entries
*/
const precacheControllerOne = new PrecacheController();
const cacheListOne = [
{url: '/scripts/index.js', revision: '1234'},
];
precacheControllerOne.addToCacheList(cacheListOne);
await precacheControllerOne.install();
const cleanupDetailsOne = await precacheControllerOne.activate();
expect(cleanupDetailsOne.deletedCacheRequests.length).to.equal(0);
expect(cleanupDetailsOne.deletedRevisionDetails.length).to.equal(0);
const precacheControllerTwo = new PrecacheController();
const cacheListTwo = [];
precacheControllerTwo.addToCacheList(cacheListTwo);
await precacheControllerTwo.install();
const cleanupDetailsTwo = await precacheControllerTwo.activate();
expect(cleanupDetailsTwo.deletedCacheRequests.length).to.equal(1);
expect(cleanupDetailsTwo.deletedCacheRequests[0]).to.equal('/scripts/index.js');
expect(cleanupDetailsTwo.deletedRevisionDetails.length).to.equal(1);
expect(cleanupDetailsTwo.deletedRevisionDetails[0]).to.equal('/scripts/index.js');
const keysTwo = await cache.keys();
expect(keysTwo.length).to.equal(cacheListTwo.length);
const entries = await precacheControllerTwo._precacheDetailsModel._getAllEntries();
expect(entries).to.deep.equal([]);
});
it(`should remove out of date entries`, async function() {
const cache = await caches.open(cacheNames.getPrecacheName());
/*
First precache some entries
*/
const precacheControllerOne = new PrecacheController();
const cacheListOne = [
'/index.1234.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
{url: '/scripts/stress.js?test=search&foo=bar', revision: '1234'},
];
precacheControllerOne.addToCacheList(cacheListOne);
await precacheControllerOne.install();
// Reset as addToCacheList and install will log.
logger.log.resetHistory();
const cleanupDetailsOne = await precacheControllerOne.activate();
expect(cleanupDetailsOne.deletedCacheRequests.length).to.equal(0);
expect(cleanupDetailsOne.deletedRevisionDetails.length).to.equal(0);
// Make sure we print some debug info.
expect(logger.log.callCount).to.equal(0);
/*
THEN precache the same URLs but two with different revisions
*/
const precacheControllerTwo = new PrecacheController();
const cacheListTwo = [
'/index.4321.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
{url: '/scripts/stress.js?test=search&foo=bar', revision: '4321'},
];
precacheControllerTwo.addToCacheList(cacheListTwo);
await precacheControllerTwo.install();
// Reset as addToCacheList and install will log.
logger.log.resetHistory();
const cleanupDetailsTwo = await precacheControllerTwo.activate();
expect(cleanupDetailsTwo.deletedCacheRequests.length).to.equal(1);
expect(cleanupDetailsTwo.deletedCacheRequests[0]).to.equal('/index.1234.html');
expect(cleanupDetailsTwo.deletedRevisionDetails.length).to.equal(1);
expect(cleanupDetailsTwo.deletedRevisionDetails[0]).to.equal('/index.1234.html');
const keysTwo = await cache.keys();
// Precaching can't determine that 'index.1234.html' and 'index.4321.html'
// represent the same URL, so the cache ould contain both at this point
// since they are technically different URLs
// It would be in the activate event that 'index.1234.html' would
// be removed from the cache and indexedDB.
expect(keysTwo.length).to.equal(cacheListTwo.length);
const urlsTwo = cacheListTwo.map((entry) => entry.url || entry);
await Promise.all(urlsTwo.map(async (url) => {
const cachedResponse = await cache.match(url);
expect(cachedResponse).to.exist;
}));
const entries = await precacheControllerTwo._precacheDetailsModel._getAllEntries();
expect(entries).to.deep.equal([
{
key: '/example.1234.css',
primaryKey: '/example.1234.css',
value: {
revision: '/example.1234.css',
url: '/example.1234.css',
},
},
{
key: '/index.4321.html',
primaryKey: '/index.4321.html',
value: {
revision: '/index.4321.html',
url: '/index.4321.html',
},
},
{
key: '/scripts/index.js',
primaryKey: '/scripts/index.js',
value: {
revision: '1234',
url: '/scripts/index.js',
},
},
{
key: '/scripts/stress.js?test=search&foo=bar',
primaryKey: '/scripts/stress.js?test=search&foo=bar',
value: {
revision: '4321',
url: '/scripts/stress.js?test=search&foo=bar',
},
},
]);
// Make sure we print some debug info.
if (process.env.NODE_ENV === 'production') {
expect(logger.log.callCount).to.equal(0);
} else {
expect(logger.log.callCount).to.be.gt(0);
}
});
it(`shouldn't open / create a cache when performing activate`, async function() {
const precacheController = new PrecacheController();
await precacheController.activate();
const hasCache = await caches.has(cacheNames.getPrecacheName());
expect(hasCache).to.equal(false);
});
prodOnly.it(`shouldn't log anything in production`, async function() {
const precacheControllerOne = new PrecacheController();
const cacheListOne = [
'/index.1234.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
{url: '/scripts/stress.js?test=search&foo=bar', revision: '1234'},
];
precacheControllerOne.addToCacheList(cacheListOne);
await precacheControllerOne.install();
await precacheControllerOne.activate();
const precacheControllerTwo = new PrecacheController();
const cacheListTwo = [
'/index.4321.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
{url: '/scripts/stress.js?test=search&foo=bar', revision: '4321'},
];
precacheControllerTwo.addToCacheList(cacheListTwo);
await precacheControllerTwo.install();
// Reset as addToCacheList and install will log.
logger.log.resetHistory();
await precacheControllerTwo.activate();
// Make sure we didn't print any debug info.
expect(logger.log.callCount).to.equal(0);
});
});
describe(`getCachedUrls()`, function() {
it(`should return the cached URLs`, function() {
const precacheController = new PrecacheController();
const cacheList = [
'/index.1234.html',
{url: '/example.1234.css'},
{url: '/scripts/index.js', revision: '1234'},
{url: '/scripts/stress.js?test=search&foo=bar', revision: '1234'},
];
precacheController.addToCacheList(cacheList);
const urls = precacheController.getCachedUrls();
expect(urls).to.deep.equal([
new URL('/index.1234.html', location).toString(),
new URL('/example.1234.css', location).toString(),
new URL('/scripts/index.js', location).toString(),
new URL('/scripts/stress.js?test=search&foo=bar', location).toString(),
]);
});
});
});