-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
client-storage-tests.js
180 lines (152 loc) · 5.87 KB
/
client-storage-tests.js
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
import { Meteor } from 'meteor/meteor';
import { ClientStorage } from './client-storage.js';
const bound = Meteor.bindEnvironment((callback) => {callback();});
var isServer = function () {
return typeof process === 'object' && !process?.browser;
};
var storages = {
auto: new ClientStorage(),
cookies: new ClientStorage('cookies'),
localStorage: new ClientStorage('localStorage'),
js: new ClientStorage('js')
};
for (var storageName of Object.keys(storages)) {
var storage = storages[storageName];
Tinytest.add(`[${storageName}] storage - ensure correct driver`, function (test) {
if (isServer()) {
test.isTrue(storage.driverName === 'js');
} else {
if (storageName === 'auto') {
test.isTrue(storage.driverName === 'localStorage');
} else {
test.isTrue(storage.driverName === storageName);
}
}
});
Tinytest.add(`[${storageName}] storage - set() / get() / has() - Void (Should fail for localStorage)`, function (test) {
storage.empty();
var testVal = void 0;
var setRes = storage.set('Void', testVal);
test.isTrue(setRes);
test.isTrue(storage.has('Void'));
test.isUndefined(storage.get('Void'));
storage.empty();
});
Tinytest.add(`[${storageName}] storage - set() / get() - String`, function (test) {
storage.empty();
var testVal = 'this is test value';
var setRes = storage.set('teststorage', testVal);
test.isTrue(setRes);
test.equal(storage.get('teststorage'), testVal);
storage.empty();
});
Tinytest.add(`[${storageName}] storage - set() / get() / has() - Cyrillic`, function (test) {
storage.empty();
var testVal = 'Ключ и значение';
var setRes = storage.set('Кириллица', testVal);
test.isTrue(setRes);
test.isTrue(storage.has('Кириллица'));
test.isFalse(storage.has('ДругойКлюч'));
test.equal(storage.get('Кириллица'), testVal);
storage.empty();
});
Tinytest.add(`[${storageName}] storage - set() / get() / has() - Unicode`, function (test) {
storage.empty();
var testVal = '⦶';
var setRes = storage.set('⦁', testVal);
test.isTrue(setRes);
test.isTrue(storage.has('⦁'));
test.isFalse(storage.has('⦁⦁⦁'));
test.equal(storage.get('⦁'), testVal);
storage.empty();
});
Tinytest.add(`[${storageName}] storage - set() / get() - Object and Array`, function (test) {
storage.empty();
var one = [1, 'one'];
var two = {two: 2};
var three = [{three: ['one', 'two', {'three': 3}]}];
var setResOne = storage.set('teststorageOne', one);
var setResTwo = storage.set('teststorageTwo', two);
var setResThree = storage.set('teststorageThree', three);
test.isTrue(setResOne);
test.isTrue(setResTwo);
test.isTrue(setResThree);
test.equal(storage.get('teststorageOne'), one);
test.equal(storage.get('teststorageTwo'), two);
test.equal(storage.get('teststorageThree'), three);
storage.empty();
});
Tinytest.add(`[${storageName}] storage - set() / get() / has() - FALSE`, function (test) {
var testVal = false;
var setRes = storage.set('testFalse', testVal);
test.isTrue(setRes);
test.isTrue(storage.has('testFalse'));
test.equal(storage.get('testFalse'), testVal);
});
Tinytest.add(`[${storageName}] storage - set() / get() / has() - TRUE`, function (test) {
var testVal = true;
var setRes = storage.set('testTrue', testVal);
test.isTrue(setRes);
test.isTrue(storage.has('testTrue'));
test.equal(storage.get('testTrue'), testVal);
});
Tinytest.add(`[${storageName}] storage - set() / get() / has() - NULL`, function (test) {
var testVal = null;
var setRes = storage.set('testNull', testVal);
test.isTrue(setRes);
test.isTrue(storage.has('testNull'));
test.equal(storage.get('testNull'), testVal);
});
Tinytest.add(`[${storageName}] storage - get() - non existent value`, function (test) {
storage.empty();
test.isUndefined(storage.get('non-existent-key'));
storage.empty();
});
Tinytest.add(`[${storageName}] storage - remove() - non existent value`, function (test) {
storage.empty();
var removeRes = storage.remove('1234567890asdfghjk');
test.isFalse(removeRes);
storage.empty();
});
Tinytest.add(`[${storageName}] storage - empty() - ALL`, function (test) {
storage.empty();
storage.set('teststorageOne', 'One');
storage.set('teststorageTwo', 'Two');
var removeRes = storage.empty();
test.isTrue(removeRes);
test.equal(storage.keys(), []);
removeRes = storage.empty();
test.isFalse(removeRes);
});
Tinytest.add(`[${storageName}] storage - keys() / has() / remove() - String`, function (test) {
storage.empty();
storage.set('teststorageOne', 'One');
storage.set('teststorageTwo', 'Two');
test.isTrue(!!~storage.keys().indexOf('teststorageOne'));
test.isTrue(!!~storage.keys().indexOf('teststorageTwo'));
test.isTrue(storage.has('teststorageOne'));
test.isTrue(storage.has('teststorageTwo'));
var removeRes = storage.remove('teststorageOne');
test.isTrue(removeRes);
test.isFalse(storage.has('teststorageOne'));
test.isTrue(storage.has('teststorageTwo'));
storage.empty();
});
Tinytest.addAsync(`[${storageName}] storage - set() / get() - 2s TTL (Max-Age; ExpireAt;)`, function (test, next) {
bound(function () {
storage.empty();
var testVal = 'this is test value with 2s TTL';
var setRes = storage.set('teststorage', testVal, 2);
test.isTrue(setRes);
test.equal(storage.get('teststorage'), testVal);
Meteor.setTimeout(function () {
test.equal(storage.get('teststorage'), testVal, 'record exists after 1 seconds');
}, 1000);
Meteor.setTimeout(function () {
test.equal(storage.get('teststorage'), void 0, 'record is gone after 3 seconds');
storage.empty();
next();
}, 3000);
});
});
}