forked from pamelafox/lscache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
176 lines (155 loc) · 5.36 KB
/
test.html
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
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
<script src="qunit.js"></script>
<script src="lscache.js"></script>
<script>
module('lscache', {
setup: function() {
// Reset localStorage before each test
try {
localStorage.clear();
} catch(e) {}
},
teardown: function() {
// Reset localStorage after each test
try {
localStorage.clear();
} catch(e) {}
}
});
test('Testing set() and get() with string', function() {
var key = 'thekey';
var value = 'thevalue'
lscache.set(key, value, 1);
if (lscache.supported()) {
equals(lscache.get(key), value, 'We expect value to be ' + value);
} else {
equals(lscache.get(key), null, 'We expect null value');
}
});
if (lscache.supported()) {
test('Testing set() with non-string values', function() {
var key, value;
key = 'numberkey';
value = 2;
lscache.set(key, value, 3);
equals(lscache.get(key)+1, value+1, 'We expect incremented value to be ' + (value+1));
key = 'arraykey';
value = ['a', 'b', 'c'];
lscache.set(key, value, 3);
equals(lscache.get(key).length, value.length, 'We expect array to have length ' + value.length);
key = 'objectkey';
value = {'name': 'Pamela', 'age': 26};
lscache.set(key, value, 3);
equals(lscache.get(key).name, value.name, 'We expect name to be ' + value.name);
});
test('Testing remove()', function() {
var key = 'thekey';
lscache.set(key, 'bla', 2);
lscache.remove(key);
equals(lscache.get(key), null, 'We expect value to be null');
});
test('Testing flush()', function() {
localStorage.setItem('outside-cache', 'not part of lscache');
var key = 'thekey';
lscache.set(key, 'bla', 100);
lscache.flush();
equals(lscache.get(key), null, 'We expect flushed value to be null');
equals(localStorage.getItem('outside-cache'), 'not part of lscache', 'We expect localStorage value to still persist');
});
test('Testing setBucket()', function() {
var key = 'thekey';
var value1 = 'awesome';
var value2 = 'awesomer';
var bucketName = 'BUCKETONE';
lscache.set(key, value1, 1);
lscache.setBucket(bucketName);
lscache.set(key, value2, 1);
equals(lscache.get(key), value2, 'We expect "' + value2 + '" to be returned for the current bucket: ' + bucketName);
lscache.flush();
equals(lscache.get(key), null, 'We expect "' + value2 + '" to be flushed for the current bucket');
lscache.resetBucket();
equals(lscache.get(key), value1, 'We expect "' + value1 + '", the non-bucket value, to persist');
});
test('Testing quota exceeding', function() {
var key = 'thekey';
// Figure out this browser's localStorage limit -
// Chrome is around 2.6 mil, for example
var stringLength = 10000;
var longString = (new Array(stringLength+1)).join('s');
var num = 0;
while(num < 10000) {
try {
localStorage.setItem(key + num, longString);
num++;
} catch (e) {
break;
}
}
localStorage.clear();
// Now add enough to go over the limit
var approxLimit = num * stringLength;
var numKeys = Math.ceil(approxLimit/(stringLength+8)) + 1;
for (var i = 0; i <= numKeys; i++) {
var currentKey = key + i;
lscache.set(currentKey, longString, i+1);
}
// Test that last-to-expire is still there
equals(lscache.get(currentKey), longString, 'We expect newest value to still be there');
// Test that the first-to-expire is kicked out
equals(lscache.get(key + '0'), null, 'We expect oldest value to be kicked out (null)');
// Test trying to add something thats bigger than previous items,
// check that it is successfully added (requires removal of multiple keys)
var veryLongString = longString + longString;
lscache.set(key + 'long', veryLongString, i+1);
equals(lscache.get(key + 'long'), veryLongString, 'We expect long string to get stored');
// Try the same with no expiry times
localStorage.clear();
for (var i = 0; i <= numKeys; i++) {
var currentKey = key + i;
lscache.set(currentKey, longString);
}
// Test that latest added is still there
equals(lscache.get(currentKey), longString, 'We expect value to be set');
});
// We do this test last since it must wait 1 minute
asyncTest('Testing set() and get() with string and expiration', 1, function() {
var key = 'thekey';
var value = 'thevalue';
var minutes = 1;
lscache.set(key, value, minutes);
setTimeout(function() {
equals(lscache.get(key), null, 'We expect value to be null');
start();
}, 1000*60*minutes);
});
asyncTest('Testing set() and get() with string and expiration in a different bucket', 2, function() {
var key = 'thekey';
var value1 = 'thevalue1';
var value2 = 'thevalue2';
var minutes = 1;
var bucket = 'newbucket';
lscache.set(key, value1, minutes * 2);
lscache.setBucket(bucket);
lscache.set(key, value2, minutes);
setTimeout(function() {
equals(lscache.get(key), null, 'We expect value to be null for the bucket: ' + bucket);
lscache.resetBucket();
equals(lscache.get(key), value1, 'We expect value to be ' + value1 + ' for the base bucket.');
start();
}, 1000*60*minutes);
});
}
</script>
</head>
<body>
<h1 id="qunit-header">QUnit lscache tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>