-
Notifications
You must be signed in to change notification settings - Fork 830
/
Copy pathtest-Plugin.mjs
222 lines (174 loc) · 7.44 KB
/
test-Plugin.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
/*
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 {Plugin} from 'workbox-expiration/Plugin.mjs';
import {CacheExpiration} from 'workbox-expiration/CacheExpiration.mjs';
import {cacheNames} from 'workbox-core/_private/cacheNames.mjs';
import {executeQuotaErrorCallbacks} from 'workbox-core/_private/quota.mjs';
describe(`Plugin`, function() {
const sandbox = sinon.createSandbox();
beforeEach(function() {
sandbox.restore();
});
after(function() {
sandbox.restore();
});
describe(`constructor`, function() {
it(`should throw for no config`, function() {
if (process.env.NODE_ENV === 'production') this.skip();
return expectError(() => {
new Plugin();
}, 'max-entries-or-age-required');
});
it(`should throw for non-number maxEntries`, function() {
if (process.env.NODE_ENV === 'production') this.skip();
return expectError(() => {
new Plugin({
maxEntries: 'Hi',
});
}, 'incorrect-type');
});
it(`should throw for non-number maxAgeSeconds`, function() {
if (process.env.NODE_ENV === 'production') this.skip();
return expectError(() => {
new Plugin({
maxAgeSeconds: 'Hi',
});
}, 'incorrect-type');
});
it(`should construct with just maxAgeSeconds`, function() {
const plugin = new Plugin({
maxAgeSeconds: 10,
});
expect(plugin._maxAgeSeconds).to.equal(10);
});
it(`should construct with just maxEntries`, function() {
const plugin = new Plugin({
maxEntries: 10,
});
expect(plugin._config.maxEntries).to.equal(10);
});
it(`should register a quota error callback when purgeOnQuotaError is true`, async function() {
const plugin = new Plugin({
maxEntries: 10,
purgeOnQuotaError: true,
});
plugin.deleteCacheAndMetadata = sandbox.stub();
await executeQuotaErrorCallbacks();
expect(plugin.deleteCacheAndMetadata.calledOnce).to.be.true;
});
it(`should not register a quota error callback when purgeOnQuotaError is false`, async function() {
const plugin = new Plugin({
maxEntries: 10,
purgeOnQuotaError: false,
});
plugin.deleteCacheAndMetadata = sandbox.stub();
await executeQuotaErrorCallbacks();
expect(plugin.deleteCacheAndMetadata.called).to.be.false;
});
});
describe(`cachedResponseWillBeUsed()`, function() {
it(`should expose a cachedResponseWillBeUsed() method`, function() {
const plugin = new Plugin({maxAgeSeconds: 1});
expect(plugin).to.respondTo('cachedResponseWillBeUsed');
});
it(`should return cachedResponse when cachedResponseWillBeUsed() is called and Responses Data header it valid`, function() {
// Just to ensure no timing flakiness in test.
sandbox.useFakeTimers({
toFake: ['Date'],
});
const dateString = new Date().toUTCString();
const cachedResponse = new Response('', {headers: {date: dateString}});
const plugin = new Plugin({maxAgeSeconds: 1});
const expirationManager = plugin._getCacheExpiration('test-cache');
sandbox.spy(expirationManager, 'expireEntries');
expect(plugin.cachedResponseWillBeUsed({request: new Request('/'), cacheName: 'test-cache', cachedResponse})).to.eql(cachedResponse);
expect(expirationManager.expireEntries.callCount).to.equal(1);
});
it(`should return null when cachedResponseWillBeUsed() is called and Responses Date header is too old`, function() {
const clock = sandbox.useFakeTimers({
toFake: ['Date'],
});
const dateString = new Date().toUTCString();
const cachedResponse = new Response('', {headers: {date: dateString}});
// Clock past the expiration of the Data header
clock.tick(1000 + 1);
const plugin = new Plugin({maxAgeSeconds: 1});
const expirationManager = plugin._getCacheExpiration('test-cache');
sandbox.spy(expirationManager, 'expireEntries');
expect(plugin.cachedResponseWillBeUsed({request: new Request('/'), cacheName: 'test-cache', cachedResponse})).to.eql(null);
expect(expirationManager.expireEntries.callCount).to.equal(1);
});
it(`should handle a null cachedResponse`, function() {
const plugin = new Plugin({maxAgeSeconds: 1});
const expirationManager = plugin._getCacheExpiration('test-cache');
sandbox.spy(expirationManager, 'expireEntries');
expect(plugin.cachedResponseWillBeUsed({cacheName: 'test-cache', cachedResponse: null})).to.eql(null);
});
it(`should update the timestamp for the request URL`, function() {
const plugin = new Plugin({maxEntries: 10});
const expirationManager = plugin._getCacheExpiration('test-cache');
sandbox.spy(expirationManager, 'updateTimestamp');
plugin.cachedResponseWillBeUsed({
request: new Request('/one'),
cacheName: 'test-cache',
cachedResponse: new Response(''),
});
expect(expirationManager.updateTimestamp.callCount).to.equal(1);
expect(expirationManager.updateTimestamp.args[0][0]).to.equal(`${location.origin}/one`);
});
});
describe(`_isResponseDateFresh()`, function() {
it(`should return true when maxAgeSeconds is not set`, function() {
const plugin = new Plugin({maxEntries: 1});
const isFresh = plugin._isResponseDateFresh(new Response('Hi'));
expect(isFresh).to.equal(true);
});
it(`should return true when there is no Date header`, function() {
const plugin = new Plugin({maxAgeSeconds: 1});
const isFresh = plugin._isResponseDateFresh(new Response('Hi', {
// TODO: Remove this when https://github.com/pinterest/service-workers/issues/72
// is fixed.
headers: {},
}));
expect(isFresh).to.equal(true);
});
it(`should return true when the Date header is invalid`, function() {
const plugin = new Plugin({maxAgeSeconds: 1});
const isFresh = plugin._isResponseDateFresh(new Response('Hi', {
headers: {date: 'invalid header'},
}));
expect(isFresh).to.equal(true);
});
});
describe(`cacheDidUpdate()`, function() {
it(`should expose a cacheDidUpdate() method`, function() {
const plugin = new Plugin({maxAgeSeconds: 1});
expect(plugin).to.respondTo('cacheDidUpdate');
});
it(`should update timestamps and expire entries`, async function() {
const cacheName = 'test-cache';
const url = new URL('/test', self.location).toString();
const request = new Request(url);
const plugin = new Plugin({maxAgeSeconds: 10});
sandbox.spy(CacheExpiration.prototype, 'updateTimestamp');
sandbox.spy(CacheExpiration.prototype, 'expireEntries');
await plugin.cacheDidUpdate({cacheName, request});
expect(CacheExpiration.prototype.updateTimestamp.callCount).to.equal(1);
expect(CacheExpiration.prototype.updateTimestamp.args[0][0]).to.equal(url);
expect(CacheExpiration.prototype.expireEntries.callCount).to.equal(1);
});
});
describe(`_getCacheExpiration()`, function() {
it(`should reject when called with the default runtime cache name`, async function() {
const plugin = new Plugin({maxAgeSeconds: 1});
await expectError(
() => plugin._getCacheExpiration(cacheNames.getRuntimeName()),
'expire-custom-caches-only'
);
});
});
});