-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
288 lines (255 loc) · 10.1 KB
/
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
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
/* globals angular, beforeEach, describe, module, inject, it, sinon */
'use strict';
describe('$store', function() {
describe('configure', function() {
describe('default settings', function() {
beforeEach(module('local-storage.service'));
it('should set cookieFallback to true',
inject(function ($store) {
$store.getConfiguration().cookieFallback.should.be.true; // jshint ignore:line
})
);
it('should set useSessionStorage to false',
inject(function ($store) {
$store.getConfiguration().useSessionStorage.should.be.false; // jshint ignore:line
})
);
});
describe('overridden settings', function() {
beforeEach(
module('local-storage.service', function ($storeProvider) {
$storeProvider.configure({
cookieFallback: false,
useSessionStorage: true
});
})
);
it('should properly set cookieFallback to false',
inject(function ($store) {
$store.getConfiguration().cookieFallback.should.be.false; // jshint ignore:line
})
);
it('should properly set useSessionStorage to true',
inject(function ($store) {
$store.getConfiguration().useSessionStorage.should.be.true; // jshint ignore:line
})
);
});
});
describe('$get', function() {
beforeEach(module('local-storage.service'));
it('should have a list of functions',
inject(function ($store) {
var functions = ['set', 'get', 'remove', 'bind', 'unbind', 'has', 'clear', 'getConfiguration'];
for (var i in functions) {
$store[functions[i]].should.be.a.Function; // jshint ignore:line
}
})
);
describe('set', function() {
it('should remove a key if set is called with an empty value',
inject(function ($store) {
sinon.spy($store, 'remove');
var result = $store.set('foo');
$store.remove.calledOnce.should.be.true; // jshint ignore:line
(result === undefined).should.be.true; // jshint ignore:line
})
);
it('should transform the value into json and set the key',
inject(function ($store) {
sinon.spy($store.getStorage(), 'setItem');
sinon.spy(angular, 'toJson');
var result = $store.set('foo', {bar: 'a'});
$store.getStorage().setItem.calledOnce.should.be.true; // jshint ignore:line
angular.toJson.calledOnce.should.be.true; // jshint ignore:line
result.should.eql({bar: 'a'});
})
);
it('should store info in the cookie store if the environment is not supported', function() {
module('ngCookies');
inject(function ($store, $cookieStore) {
sinon.spy($cookieStore, 'put');
$store.setSupported(false);
$store.set('foo', 'bar').should.equal('bar');
$cookieStore.put.calledOnce.should.be.true; // jshint ignore:line
});
});
it('should store info in the memStorage if the environment is not supported and cookie fallback is disabled', function() {
module('local-storage.service', function ($storeProvider) {
$storeProvider.configure({cookieFallback: false});
});
inject(function ($store) {
$store.setSupported(false);
var result = $store.set('foo', 'bar');
$store.getMemStore().foo.should.equal('bar');
result.should.equal('bar');
});
});
});
describe('get', function() {
beforeEach(module('local-storage.service'));
it('should return the value from the store',
inject(function ($store) {
$store.set('foo', 'bar');
$store.get('foo').should.equal('bar');
})
);
it('should return the value from the cookie store if the environment is not supported', function() {
module('ngCookies');
inject(function ($store, $cookieStore) {
sinon.spy($cookieStore, 'put');
sinon.spy($cookieStore, 'get');
$store.setSupported(false);
$store.set('foo', 'bar');
$store.get('foo').should.equal('bar');
$cookieStore.put.calledOnce.should.be.true; // jshint ignore:line
$cookieStore.get.calledOnce.should.be.true; // jshint ignore:line
});
});
it('should return the value from the memStorage if the environment is not supported and cookie fallback is disabled', function() {
module('local-storage.service', function ($storeProvider) {
$storeProvider.configure({cookieFallback: false});
});
inject(function ($store) {
$store.setSupported(false);
$store.set('foo', 'bar');
$store.getMemStore().foo.should.equal('bar');
$store.get('foo').should.equal('bar');
});
});
});
describe('remove', function() {
beforeEach(module('local-storage.service'));
it('should remove the value from the store',
inject(function ($store) {
sinon.spy($store.getStorage(), 'removeItem');
$store.remove('foo');
$store.getStorage().removeItem.calledOnce.should.be.true; // jshint ignore:line
})
);
it('should remove the value from the cookie store if the environment is not supported', function() {
module('ngCookies');
inject(function ($store, $cookieStore) {
$store.setSupported(false);
sinon.spy($cookieStore, 'remove');
$store.remove('foo');
$cookieStore.remove.calledOnce.should.be.true; // jshint ignore:line
});
});
it('should remove the value from the memStorage if the environment is not supported and cookie fallback is disabled', function() {
module('local-storage.service', function ($storeProvider) {
$storeProvider.configure({cookieFallback: false});
});
inject(function ($store) {
$store.setSupported(false);
$store.set('foo', 'bar');
$store.getMemStore().foo.should.equal('bar');
$store.remove('foo');
($store.getMemStore().foo === undefined).should.be.true; // jshint ignore:line
});
});
});
describe('bind', function() {
beforeEach(module('local-storage.service'));
it('should add a watch to a scope',
inject(function ($store, $rootScope) {
var scope = $rootScope.$new();
sinon.spy(scope, '$watch');
$store.bind(scope, 'foo');
scope.$watch.calledOnce.should.be.true; // jshint ignore:line
})
);
it('should set the value to fallback if a fallback is given and key does not already exist',
inject(function ($store, $rootScope) {
var scope = $rootScope.$new();
$store.remove('foo');
$store.bind(scope, 'foo', 'bar');
$store.get('foo').should.equal('bar');
})
);
});
describe('unbind', function() {
beforeEach(module('local-storage.service'));
it('should empty the watcher',
inject(function ($store, $rootScope) {
var scope = $rootScope.$new();
sinon.spy(scope, '$watch');
$store.bind(scope, 'foo');
$store.unbind(scope, 'foo');
// We wanted the first call because the zeroth is the bind call
var watchCall = scope.$watch.getCall(1);
watchCall.args[1].toString().should.eql(function(){}.toString());
})
);
it('should remove the key',
inject(function ($store, $rootScope) {
var scope = $rootScope.$new();
$store.bind(scope, 'foo', 'bar');
$store.get('foo').should.equal('bar');
$store.unbind(scope, 'foo');
($store.get('foo') === null).should.be.true; // jshint ignore:line
})
);
});
describe('has', function() {
beforeEach(module('local-storage.service'));
it('should return false if the key is not in the store',
inject(function ($store) {
$store.remove('foo');
$store.has('foo').should.be.false; // jshint ignore:line
})
);
it('should return true if the key is in the store',
inject(function ($store) {
$store.set('foo', 'bar');
$store.has('foo').should.be.true; // jshint ignore:line
})
);
});
describe('clear', function() {
beforeEach(module('local-storage.service'));
it('should remove everything if the environment is supported',
inject(function ($store) {
$store.set('foo', 1);
$store.set('bar', 2);
$store.set('baz', 3);
$store.clear();
$store.has('foo').should.be.false; // jshint ignore:line
$store.has('bar').should.be.false; // jshint ignore:line
$store.has('baz').should.be.false; // jshint ignore:line
})
);
it('should not remove anything if cookie fallback is enabled', function() {
module('ngCookies');
inject(function ($store) {
$store.setSupported(false);
$store.set('foo', 1);
$store.set('bar', 2);
$store.set('baz', 3);
$store.clear();
$store.has('foo').should.be.true; // jshint ignore:line
$store.has('bar').should.be.true; // jshint ignore:line
$store.has('baz').should.be.true; // jshint ignore:line
});
});
it('should create a new memStorage object if environment is not supported and cookie fallback is disabled', function() {
module('local-storage.service', function ($storeProvider) {
$storeProvider.configure({cookieFallback: false});
});
inject(function ($store) {
var memStorage = $store.getMemStore();
$store.setSupported(false);
$store.set('foo', 1);
$store.set('bar', 2);
$store.set('baz', 3);
$store.clear();
$store.has('foo').should.be.false; // jshint ignore:line
$store.has('bar').should.be.false; // jshint ignore:line
$store.has('baz').should.be.false; // jshint ignore:line
$store.getMemStore().should.not.equal(memStorage);
$store.getMemStore().should.be.empty; // jshint ignore:line
});
});
});
});
});