This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
/
backbone_extensions_spec.js
108 lines (103 loc) · 3.96 KB
/
backbone_extensions_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
// Generated by CoffeeScript 1.9.3
(function() {
var Backbone, localStorage;
Backbone = window.Backbone, localStorage = window.localStorage;
describe('monkey patching', function() {
return it('aliases Backbone.sync to backboneSync', function() {
expect(window.backboneSync).toBeDefined();
return expect(window.backboneSync.identity).toEqual('sync');
});
});
describe('offline localStorage sync', function() {
var collection, model;
collection = {}.collection;
model = {}.model;
model = Backbone.Model.extend({
idAttribute: '_id'
});
beforeEach(function() {
var Collection;
localStorage.clear();
localStorage.setItem('cats', '1,2,3,a,deadbeef-c03d-f00d-aced-dec0ded4b1ff');
localStorage.setItem('cats_dirty', '2,a,deadbeef-c03d-f00d-aced-dec0ded4b1ff');
localStorage.setItem('cats_destroyed', '3');
localStorage.setItem('cats1', '{"_id": "1", "color": "translucent"}');
localStorage.setItem('cats2', '{"_id": "2", "color": "auburn"}');
localStorage.setItem('cats3', '{"_id": "3", "color": "burgundy"}');
localStorage.setItem('catsa', '{"_id": "a", "color": "scarlet"}');
localStorage.setItem('catsnew', '{"_id": "deadbeef-c03d-f00d-aced-dec0ded4b1ff", "color": "pearl"}');
Collection = Backbone.Collection.extend({
model: model,
url: 'cats'
});
return collection = new Collection([
{
_id: 1,
color: 'translucent'
}, {
_id: 2,
color: 'auburn'
}, {
_id: 3,
color: 'burgundy'
}, {
_id: 'a',
color: 'scarlet'
}, {
_id: 'deadbeef-c03d-f00d-aced-dec0ded4b1ff',
color: 'pearl'
}
]);
});
describe('syncDirtyAndDestroyed', function() {
return it('calls syncDirty and syncDestroyed', function() {
var syncDestroyed, syncDirty;
syncDirty = spyOn(Backbone.Collection.prototype, 'syncDirty');
syncDestroyed = spyOn(Backbone.Collection.prototype, 'syncDestroyed');
collection.syncDirtyAndDestroyed();
expect(syncDirty).toHaveBeenCalled();
return expect(syncDestroyed).toHaveBeenCalled();
});
});
describe('syncDirty', function() {
it('finds and saves all dirty models', function() {
var saveInteger, saveString;
saveInteger = spyOn(collection.get(2), 'save').andCallThrough();
saveString = spyOn(collection.get('a'), 'save').andCallThrough();
collection.syncDirty();
expect(saveInteger).toHaveBeenCalled();
expect(saveString).toHaveBeenCalled();
return expect(localStorage.getItem('cats_dirty')).toBeFalsy();
});
return it('works when there are no dirty records', function() {
localStorage.removeItem('cats_dirty');
return collection.syncDirty();
});
});
describe('syncDestroyed', function() {
it('finds all models marked as destroyed and destroys them', function() {
var destroy;
destroy = spyOn(collection.get(3), 'destroy');
collection.syncDestroyed();
return expect(localStorage.getItem('cats_destroyed')).toBeFalsy();
});
return it('works when there are no destroyed records', function() {
localStorage.setItem('cats_destroyed', '');
return collection.syncDestroyed();
});
});
describe('dirtyModels', function() {
return it('returns the model instances that are dirty', function() {
return expect(collection.dirtyModels().map(function(model) {
return model.id;
})).toEqual([2, 'a', 'deadbeef-c03d-f00d-aced-dec0ded4b1ff']);
});
});
return describe('destoyedModelsIds', function() {
return it('returns the ids of models that have been destroyed locally but not synced', function() {
return expect(collection.destroyedModelIds()).toEqual(['3']);
});
});
});
}).call(this);
//# sourceMappingURL=backbone_extensions_spec.js.map