forked from alekseykulikov/backbone-offline
/
storage_spec.js
289 lines (287 loc) · 10.7 KB
/
storage_spec.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
289
(function() {
describe('Offline.Storage', function() {
beforeEach(function() {
localStorage.setItem('dreams', '');
this.dreams = new Dreams();
return this.storage = this.dreams.storage;
});
afterEach(function() {
return localStorage.clear();
});
describe('constructor', function() {
beforeEach(function() {
localStorage.setItem('items', '2,3');
localStorage.setItem('items-destroy', '1,4');
return this.itemsStore = new Offline.Storage('items', [], {
autoSync: false,
keys: {
parent_id: this.dreams
}
});
});
it('sets @allIds', function() {
return expect(this.itemsStore.allIds.values).toEqual(['2', '3']);
});
it('sets @destroyIds', function() {
return expect(this.itemsStore.destroyIds.values).toEqual(['1', '4']);
});
return it('sets @keys by options.keys', function() {
return expect(this.itemsStore.keys).toEqual({
parent_id: this.dreams
});
});
});
describe('create', function() {
beforeEach(function() {
return this.dream = new Dream({
name: 'Diving with scuba'
});
});
it("should return model's attributes", function() {
return expect(this.storage.create(this.dream).get('name')).toEqual('Diving with scuba');
});
it('should generate local id', function() {
spyOn(this.storage, 'guid').andReturn('1');
this.storage.create(this.dream);
return expect(this.storage.guid).toHaveBeenCalled();
});
it('should call "save"', function() {
spyOn(this.storage, 'save');
this.storage.create(this.dream);
return expect(this.storage.save).toHaveBeenCalledWith(jasmine.any(Object), {
regenerateId: true
});
});
it('sets updated_at and dirty attributes', function() {
var createdModel;
createdModel = this.storage.create(this.dream);
expect(createdModel.get('dirty')).toBeTruthy();
return expect(createdModel.get('updated_at')).toBeDefined();
});
it('does not set updated_at and dirty when options = {local: true}', function() {
var createdModel;
createdModel = this.storage.create(this.dream, {
local: true
});
expect(createdModel.dirty).toBeUndefined();
return expect(createdModel.updated_at).toBeUndefined();
});
it('should save server id to "sid" attribute', function() {
return expect(this.storage.create(new Dream({
id: 1
})).get('sid')).toEqual(1);
});
it('should set "sid" attribute to "new" when model was create locally', function() {
return expect(this.storage.create(this.dream).get('sid')).toEqual('new');
});
return it("should set model's \"sid\" attribute when model has it", function() {
return expect(this.storage.create(new Dream({
sid: 'abcd'
})).get('sid')).toEqual('abcd');
});
});
describe('update', function() {
beforeEach(function() {
return this.dream = this.dreams.create();
});
it('sets updated_at and dirty', function() {
var updatedModel;
updatedModel = this.storage.update(this.dream);
expect(updatedModel.get('dirty')).toBeTruthy();
return expect(updatedModel.get('updated_at')).toBeDefined();
});
it('does not set updated_at and dirty when options = {local: true}', function() {
var updatedModel;
updatedModel = this.storage.update(this.dream, {
local: true
});
expect(updatedModel.dirty).toBeUndefined();
return expect(updatedModel.updated_at).toBeUndefined();
});
return it('should call "save"', function() {
spyOn(this.storage, 'save');
this.storage.update(this.dream);
return expect(this.storage.save).toHaveBeenCalledWith(this.dream, {});
});
});
describe('destroy', function() {
beforeEach(function() {
return this.dream = this.dreams.create();
});
it('should call "remove"', function() {
spyOn(this.storage, 'remove');
this.storage.destroy(this.dream);
return expect(this.storage.remove).toHaveBeenCalledWith(this.dream);
});
return it('should change @destroyIds', function() {
this.dream.set('sid', '1');
this.storage.destroy(this.dream);
return expect(this.storage.destroyIds.values).toEqual(['1']);
});
});
describe('find', function() {
return it('should return specified item', function() {
var dream;
dream = this.dreams.create();
return expect(this.storage.find(dream).id).toEqual(dream.id);
});
});
describe('findAll', function() {
beforeEach(function() {
spyOn(this.storage.sync, 'incremental');
return spyOn(this.storage.sync, 'full');
});
it("should return all collection's items", function() {
this.dreams.create();
return expect(this.storage.findAll().length).toEqual(1);
});
it('should call "incremental" sync', function() {
this.storage.findAll();
return expect(this.storage.sync.incremental).toHaveBeenCalled();
});
it('should call "full" sync when storage is empty', function() {
localStorage.clear();
this.storage.findAll();
return expect(this.storage.sync.full).toHaveBeenCalled();
});
return it('should not call "full" or "incremental" if added option {local: true}', function() {
this.dreams.fetch({
local: true
});
expect(this.storage.sync.full.callCount).toBe(0);
return expect(this.storage.sync.incremental.callCount).toBe(0);
});
});
describe('save', function() {
beforeEach(function() {
return this.dream = new Dream({
id: 'abcd',
name: 'New dream'
});
});
it('should save item to localStorage', function() {
this.storage.save(this.dream);
return expect(localStorage.getItem('dreams-abcd')).toEqual(JSON.stringify(this.dream));
});
it('should add item.id to @allIds', function() {
this.storage.save(this.dream);
return expect(_.include(this.storage.allIds.values, 'abcd')).toBeTruthy();
});
it('should call "replaceKeyFields"', function() {
spyOn(this.storage, 'replaceKeyFields');
this.storage.save(this.dream);
return expect(this.storage.replaceKeyFields).toHaveBeenCalledWith(this.dream, 'local');
});
it("should push item if options.autoPush", function() {
this.storage.autoPush = true;
spyOn(this.storage.sync, 'pushItem');
this.storage.create(this.dream);
return expect(this.storage.sync.pushItem).toHaveBeenCalledWith(this.dream);
});
return it("should does not push item if local=true", function() {
this.storage.autoPush = true;
spyOn(this.storage.sync, 'pushItem');
this.storage.create(this.dream, {
local: true
});
return expect(this.storage.sync.pushItem.callCount).toBe(0);
});
});
describe('remove', function() {
beforeEach(function() {
return this.dream = this.dreams.create({
id: 'dcba'
}, {
regenerateId: true
});
});
it('should remove item from localStorage', function() {
this.storage.remove(this.dream);
return expect(localStorage.getItem('dreams-dcba')).toBeNull();
});
it('should remove item.id from @allIds', function() {
this.storage.remove(this.dream);
return expect(_.include(this.storage.values, 'dcba')).toBeFalsy();
});
return it("should flush item if options.autoPush", function() {
this.storage.autoPush = true;
spyOn(this.storage.sync, 'flushItem');
this.storage.remove(this.dream);
return expect(this.storage.sync.flushItem).toHaveBeenCalledWith('dcba');
});
});
describe('isEmpty', function() {
it("should return true when localStorage's key is null", function() {
localStorage.removeItem('dreams');
return expect(this.storage.isEmpty()).toBeTruthy();
});
return it('should return false when localStorage has necessary key', function() {
localStorage.setItem('dreams', '1,2,3');
return expect(this.storage.isEmpty()).toBeFalsy();
});
});
describe('clear', function() {
beforeEach(function() {
localStorage.setItem('dreams', '1234');
localStorage.setItem('dreams-1234', 'id:0002');
return localStorage.setItem('other', '');
});
it("should clear localStorage's items including collection-key", function() {
this.storage.clear();
return expect(localStorage.getItem('dreams-1234')).toBeNull();
});
it('sets collection-key to ""', function() {
this.storage.clear();
return expect(localStorage.getItem('dreams')).toEqual('');
});
it('does not clear other collections', function() {
this.storage.clear();
return expect(localStorage.getItem('other')).toEqual('');
});
return it('should reset @allIds and @destroyIds', function() {
spyOn(this.storage.allIds, 'reset');
spyOn(this.storage.destroyIds, 'reset');
this.storage.clear();
expect(this.storage.allIds.reset).toHaveBeenCalled();
return expect(this.storage.destroyIds.reset).toHaveBeenCalled();
});
});
return describe('replaceKeyFields', function() {
beforeEach(function() {
localStorage.setItem('child-dreams', '');
this.childDreams = new Dreams();
this.storage2 = new Offline.Storage('child-dreams', this.childDreams, {
autoSync: false,
keys: {
parent_id: this.dreams
}
});
this.childDreams.storage = this.storage2;
return this.dream = this.dreams.create({
name: 'Visit Norway',
sid: '1'
});
});
describe('when method is "local"', function() {
return it('should replace server ids to local', function() {
var item;
item = this.storage2.replaceKeyFields({
name: 'Live in Norvay',
parent_id: '1'
}, 'local');
return expect(item.parent_id).toEqual(this.dream.id);
});
});
return describe('when method is "server"', function() {
return it('should replace local ids to server', function() {
var item;
item = this.storage2.replaceKeyFields({
name: 'Live in Norvay',
parent_id: this.dream.id
}, 'server');
return expect(item.parent_id).toEqual(this.dream.get('sid'));
});
});
});
});
}).call(this);