forked from kpuputti/JSONCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.js
More file actions
executable file
·482 lines (400 loc) · 19.9 KB
/
Copy pathtests.js
File metadata and controls
executable file
·482 lines (400 loc) · 19.9 KB
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
/*jslint white: true, devel: true, onevar: false, undef: true, nomen: false,
regexp: true, plusplus: false, bitwise: true, newcap: true, maxerr: 50,
indent: 4 */
/*global jQuery: false, module: false, test: false, expect: false,
strictEqual: false, window: false, ok: false, QUnit: false,
asyncTest: false, start: false, stop: false, deepEqual: false */
(function ($) {
var JSONCache = window.JSONCache;
var _numTries = JSONCache.settings.numTries;
var _waitTime = JSONCache.settings.waitTime;
var _getJSONProxy = JSONCache._getJSONProxy;
var _getTime = JSONCache._getTime;
var _tryGetJSON = JSONCache._tryGetJSON;
var testData;
QUnit.reset = function () {
try {
window.localStorage.clear();
} catch (e) {
// Fail silently if localStorage is not supported.
// Support is checked in the first test case.
}
// Put back the original settings and functions after
// injecting the mocks.
JSONCache.settings.numTries = _numTries;
JSONCache.settings.waitTime = _waitTime;
JSONCache._getJSONProxy = _getJSONProxy;
JSONCache._getTime = _getTime;
JSONCache._tryGetJSON = _tryGetJSON;
testData = {
"success": true,
"data": [
"först item",
"secönd itém"
],
"Weird väl": 666
};
};
QUnit.reset();
module('JSONCache');
// Alias to the strict comparison function of QUnit (using ===).
var eq = strictEqual;
test('Requirements.', function () {
expect(9);
// Detect native JSON parser support.
var jsonSupported = function () {
return JSON &&
typeof JSON.parse === 'function' &&
typeof JSON.stringify === 'function';
};
// Detect localStorage support.
var localStorageSupported = function () {
try {
return 'localStorage' in window && window.localStorage !== null;
} catch (e) {
return false;
}
};
eq(typeof window.jQuery, 'function', 'jQuery is required');
eq(jsonSupported(), true, 'JSON is required');
eq(localStorageSupported(), true, 'localStorage is required');
eq(typeof JSONCache, 'object', 'JSONCache is required');
eq(typeof JSONCache.getCachedJSON, 'function', 'JSONCache is required');
eq(JSONCache.settings.browserOk, true, 'JSONCache.browserOk');
eq(JSONCache.settings.numTries, 5, 'numTries should be the one expected.');
eq(JSONCache.settings.waitTime, 200, 'waitTime should be the one expected.');
eq(JSONCache.settings.itemLifetime, 5 * 60 * 1000, 'itemLifetime should be the one expected.');
});
test('Basic localStorage functionality', function () {
expect(2);
// Initial conditions.
eq(window.localStorage.length, 0, 'localStorage should be empty.');
window.localStorage.test = 'åäö';
eq(window.localStorage.test, 'åäö', 'Value should be saved in the localStorage.');
});
asyncTest('Basic fetch with an empty localStorage.', function () {
expect(7);
var proxyMockCallCount = 0;
// Mock the json proxy to avoid networking.
JSONCache._getJSONProxy = function (url, options) {
proxyMockCallCount++;
if (url === 'data.json') {
options.success(testData);
}
};
var timeMockCallCount = 0;
// Mock the timestamp function for a static timestamp.
JSONCache._getTime = function () {
timeMockCallCount++;
return 2345678901234;
};
// Initial conditions.
eq(window.localStorage.length, 0, 'localStorage should be empty.');
JSONCache.getCachedJSON('data.json', {
success: function (data) {
deepEqual(data, {
success: true,
data: [
'först item',
'secönd itém'
],
'Weird väl': 666
}, 'Correct test data should be returned.');
deepEqual(data, JSON.parse(window.localStorage['JSONCache data data.json']),
'localStorage should be populated with the correct data.');
eq(window.localStorage['JSONCache time data.json'], '2345678901234',
'Timestamp should be the one returned by the static mock function.');
eq(proxyMockCallCount, 1, 'Mocked json proxy should be called once.');
eq(timeMockCallCount, 1, 'Mocked timestamp function should be called once.');
eq(window.localStorage.length, 2, 'There should be two items in the localStorage.');
start();
}
});
});
// JSONCache.clear
test('Cache item clearing with empty cache.', function () {
expect(2);
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
JSONCache.clear('http://example.org/key?param1=a%20b+c#hash-123');
eq(window.localStorage.length, 0, 'localStorage should be empty in the end.');
});
test('Whole cache clearing with empty cache.', function () {
expect(2);
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
JSONCache.clear();
eq(window.localStorage.length, 0, 'localStorage should be empty in the end.');
});
test('Cache item clearing with items in the cache.', function () {
expect(3);
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
var dataKey = 'JSONCache data http://example.org/key?param1=a%20b+c#hash-123';
var timeKey = 'JSONCache time http://example.org/key?param1=a%20b+c#hash-123';
window.localStorage[dataKey] = 'my dätä';
window.localStorage[timeKey] = '123';
window.localStorage['öther kéy'] = 'öther dätä';
// Remove a certain item from the cache.
JSONCache.clear('http://example.org/key?param1=a%20b+c#hash-123');
eq(window.localStorage.length, 1, 'There should be one item in the localStorage.');
eq(window.localStorage['öther kéy'], 'öther dätä', 'Correct data should be in the localStorage.');
});
test('Whole cache clearing with several items in the cache.', function () {
expect(3);
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
var dataKey = 'JSONCache data http://example.org/key?param1=a%20b+c#hash-123';
var timeKey = 'JSONCache time http://example.org/key?param1=a%20b+c#hash-123';
window.localStorage[dataKey] = 'my dätä';
window.localStorage[timeKey] = '123';
window.localStorage['JSONCache data http://example.org/data'] = 'my dätä 2';
window.localStorage['JSONCache time http://example.org/time'] = '1234';
window.localStorage['öther kéy'] = 'öther dätä';
// Clear the whole cache.
JSONCache.clear();
eq(window.localStorage.length, 1, 'There should be one item in the localStorage.');
eq(window.localStorage['öther kéy'], 'öther dätä', 'Correct data should be in the localStorage.');
});
test('Test oldest item removing with empty cache.', function () {
expect(2);
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
JSONCache.purgeOldest();
eq(window.localStorage.length, 0, 'localStorage should be empty in the end.');
});
test('Test oldest item removing with one item in the cache.', function () {
expect(2);
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
window.localStorage['JSONCache data http://example.org/data'] = 'söme dätä';
window.localStorage['JSONCache time http://example.org/data'] = '123';
JSONCache.purgeOldest();
eq(window.localStorage.length, 0, 'localStorage should be empty in the end.');
});
test('Test oldest item removing with several items in the cache.', function () {
expect(6);
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
window.localStorage['JSONCache data http://example.org/data1'] = 'söme dätä 1';
window.localStorage['JSONCache time http://example.org/data1'] = '1001';
window.localStorage['JSONCache data http://example.org/data2'] = 'söme dätä 2';
window.localStorage['JSONCache time http://example.org/data2'] = '1002';
window.localStorage['JSONCache data http://example.org/data3'] = 'söme dätä 3';
window.localStorage['JSONCache time http://example.org/data3'] = '1003';
JSONCache.purgeOldest();
eq(window.localStorage.length, 4, 'There should be four items in localStorage.');
eq(window.localStorage['JSONCache data http://example.org/data2'], 'söme dätä 2',
'Correct data should be in localStorage.');
eq(window.localStorage['JSONCache time http://example.org/data2'], '1002',
'Correct data should be in localStorage.');
eq(window.localStorage['JSONCache data http://example.org/data3'], 'söme dätä 3',
'Correct data should be in localStorage.');
eq(window.localStorage['JSONCache time http://example.org/data3'], '1003',
'Correct data should be in localStorage.');
});
test('Test oldest item removing with several items (including non JSONCache items).', function () {
expect(6);
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
window.localStorage['JSONCache data http://example.org/data1'] = 'söme dätä 1';
window.localStorage['JSONCache time http://example.org/data1'] = '1001';
window.localStorage['JSONCache data http://example.org/data2'] = 'söme dätä 2';
window.localStorage['JSONCache time http://example.org/data2'] = '1002';
window.localStorage['JSONCache data http://example.org/data3'] = 'söme dätä 3';
window.localStorage['JSONCache time http://example.org/data3'] = '1003';
window.localStorage['öther key 1'] = 'öther dätä 1';
window.localStorage['öther key 2'] = 'öther dätä 2';
// Clear two oldest items.
JSONCache.purgeOldest();
JSONCache.purgeOldest();
eq(window.localStorage.length, 4, 'There should be four items in localStorage.');
eq(window.localStorage['JSONCache data http://example.org/data3'], 'söme dätä 3',
'Correct data should be in localStorage.');
eq(window.localStorage['JSONCache time http://example.org/data3'], '1003',
'Correct data should be in localStorage.');
eq(window.localStorage['öther key 1'], 'öther dätä 1',
'Correct data should be in localStorage.');
eq(window.localStorage['öther key 2'], 'öther dätä 2',
'Correct data should be in localStorage.');
});
test('Default numTries in settings.', function () {
expect(1);
eq(JSONCache.settings.numTries, 5, 'Default tryCount value should be 5.');
});
asyncTest('Timeout for all fetching tries.', function () {
expect(2);
// Set shorter wait time for faster tests.
JSONCache.settings.waitTime = 10;
// Mock the proxy function to always time out.
var proxyCallCount = 0;
JSONCache._getJSONProxy = function (url, options) {
proxyCallCount++;
window.setTimeout(function () {
options.error(null, 'timeout', null);
}, 10);
};
JSONCache.getCachedJSON('data.json', {
ongiveup: function (status) {
eq(status, 'timeout', 'Error status should be correct.');
eq(proxyCallCount, 5, 'Proxy function should be called 5 times.');
start();
}
});
});
asyncTest('Timeout for all fetching tries with changed number of tries.', function () {
expect(3);
// Set shorter wait time for faster tests.
JSONCache.settings.waitTime = 10;
JSONCache.settings.numTries = 6;
// Mock the proxy function to always time out.
var proxyCallCount = 0;
JSONCache._getJSONProxy = function (url, options) {
proxyCallCount++;
window.setTimeout(function () {
options.error(null, 'timeout', null);
}, 10);
};
var retryArgs = [];
var ontry = function (tryNumber) {
retryArgs.push(tryNumber);
};
JSONCache.getCachedJSON('data.json', {
ontry: ontry,
ongiveup: function (status) {
eq(status, 'timeout', 'Error status should be correct.');
eq(proxyCallCount, 6, 'Proxy function should be called the correct amount of times.');
deepEqual(retryArgs, [1, 2, 3, 4, 5, 6],
'The retry hook function should be called before each try.');
start();
}
});
});
asyncTest('Error hook calling.', function () {
expect(4);
JSONCache.settings.waitTime = 10;
// Mock the proxy function to always time out.
var proxyCallCount = 0;
JSONCache._getJSONProxy = function (url, options) {
proxyCallCount++;
window.setTimeout(function () {
options.error(null, 'timeout', null);
}, 10);
};
var errorCallCount = 0;
var errorArgs = [];
var onerror = function (jqXHR, textStatus, errorThrown, tryNumber) {
errorCallCount++;
errorArgs.push(textStatus + ' ' + tryNumber);
};
JSONCache.getCachedJSON('data.json', {
onerror: onerror,
ongiveup: function (status) {
eq(status, 'timeout', 'Error status should be correct.');
eq(proxyCallCount, 5, 'Proxy function should be called the correct amount of times.');
eq(errorCallCount, 5, 'Error function should be called the correct amount of times.');
deepEqual(errorArgs, ['timeout 1', 'timeout 2', 'timeout 3', 'timeout 4', 'timeout 5'],
'The error hook function should be called on every error.');
start();
}
});
});
asyncTest('Trying to get an expired item from the cache.', function () {
expect(3);
eq(window.localStorage.length, 0, 'localStorage should be empty initially.');
var proxyCallCount = 0;
JSONCache._getJSONProxy = function (url, options) {
proxyCallCount++;
if (url === 'data.json') {
options.success(testData);
}
};
JSONCache._getTime = function () {
return 2345678901234;
};
// Expired one millisecond.
var lifetime = 5 * 60 * 1000;
var time = 2345678901234 - lifetime - 1;
window.localStorage['JSONCache data data.json'] = '{"data":"öld invalid dätä"}';
window.localStorage['JSONCache time data.json'] = time;
JSONCache.getCachedJSON('data.json', {
success: function (data) {
deepEqual(data, {
"success": true,
"data": [
"först item",
"secönd itém"
],
"Weird väl": 666
}, 'Returned data should be correct.');
eq(proxyCallCount, 1, 'Proxy function should be called once.');
start();
}
});
});
test('Cache clean with empty cache.', function () {
expect(2);
eq(window.localStorage.length, 0, 'localStorage should be empty initially.');
JSONCache.clean();
eq(window.localStorage.length, 0, 'localStorage should be empty in the end.');
});
test('Cache clean with only valid items.', function () {
expect(7);
eq(window.localStorage.length, 0, 'localStorage should be empty initially.');
JSONCache._getTime = function () {
return 2345678901234;
};
window.localStorage['JSONCache data data1'] = '{"data":"dätä 1"}';
window.localStorage['JSONCache time data1'] = '2345678901234';
window.localStorage['JSONCache data data2'] = '{"data":"dätä 2"}';
window.localStorage['JSONCache time data2'] = (2345678901234 - 5 * 60 * 1000).toString();
window.localStorage['söme öther key'] = 'söme öther dätä';
JSONCache.clean();
eq(window.localStorage.length, 5, 'All items should still be in the cache.');
eq(window.localStorage['JSONCache data data1'], '{"data":"dätä 1"}',
'Correct data should still be in the cache.');
eq(window.localStorage['JSONCache time data1'], '2345678901234',
'Correct data should still be in the cache.');
eq(window.localStorage['JSONCache data data2'], '{"data":"dätä 2"}',
'Correct data should still be in the cache.');
eq(window.localStorage['JSONCache time data2'], (2345678901234 - 5 * 60 * 1000).toString(),
'Correct data should still be in the cache.');
eq(window.localStorage['söme öther key'], 'söme öther dätä',
'Correct data should still be in the cache.');
});
test('Cache clean with only expired items.', function () {
expect(2);
eq(window.localStorage.length, 0, 'localStorage should be empty initially.');
JSONCache._getTime = function () {
return 2345678901234;
};
window.localStorage['JSONCache data data1'] = '{"data":"dätä 1"}';
window.localStorage['JSONCache time data1'] = '1234567890123';
window.localStorage['JSONCache data data2'] = '{"data":"dätä 2"}';
// Expired 1 millisecond.
window.localStorage['JSONCache time data2'] = (2345678901234 - 5 * 60 * 1000 - 1).toString();
JSONCache.clean();
eq(window.localStorage.length, 0, 'Cache should be empty.');
});
test('Cache clean with valid and expired items.', function () {
expect(7);
eq(window.localStorage.length, 0, 'localStorage should be empty initially.');
JSONCache._getTime = function () {
return 2345678901234;
};
window.localStorage['JSONCache data data1'] = '{"data":"dätä 1"}';
window.localStorage['JSONCache time data1'] = '1234567890123';
window.localStorage['JSONCache data data2'] = '{"data":"dätä 2"}';
// Expired 1 millisecond.
window.localStorage['JSONCache time data2'] = (2345678901234 - 5 * 60 * 1000 - 1).toString();
window.localStorage['JSONCache data data3'] = '{"data":"dätä 3"}';
window.localStorage['JSONCache time data3'] = '2345678901234';
window.localStorage['JSONCache data data4'] = '{"data":"dätä 4"}';
window.localStorage['JSONCache time data4'] = (2345678901234 - 5 * 60 * 1000).toString();
window.localStorage['söme öther key'] = 'söme öther dätä';
JSONCache.clean();
eq(window.localStorage.length, 5, 'Valid items should still be in the cache.');
eq(window.localStorage['JSONCache data data3'], '{"data":"dätä 3"}',
'Correct data should still be in the cache.');
eq(window.localStorage['JSONCache time data3'], '2345678901234',
'Correct data should still be in the cache.');
eq(window.localStorage['JSONCache data data4'], '{"data":"dätä 4"}',
'Correct data should still be in the cache.');
eq(window.localStorage['JSONCache time data4'], (2345678901234 - 5 * 60 * 1000).toString(),
'Correct data should still be in the cache.');
eq(window.localStorage['söme öther key'], 'söme öther dätä',
'Correct data should still be in the cache.');
});
}(jQuery));