-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelResetSpec.js
112 lines (94 loc) · 4.18 KB
/
ModelResetSpec.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
var Backbone = require("backbone");
require('../backbone-model-reset');
describe('Model Reset tests', function () {
describe('Test Backbone.Model.reset', function () {
var model;
beforeEach(function () {
model = new Backbone.Model();
});
it('reset method is present', function () {
expect(model).toBeDefined();
expect(typeof model.reset).toEqual('function');
});
function expectChangeHandlerToHaveBeenCalledWith(changeHandler, expectedModelValues) {
var changeHandlerModelArg = changeHandler.calls[0].args[0];
expect(changeHandlerModelArg.attributes).toEqual(expectedModelValues);
}
it('model.reset() sets unsets and where appropriate, and triggers change events properly', function () {
var originalAttributes = {
a: 1,
b: "2",
c: "three"
};
model.set(originalAttributes);
expect(model.toJSON()).toEqual(originalAttributes);
var aChange = jasmine.createSpy('aChange');
var bChange = jasmine.createSpy('bChange');
var cChange = jasmine.createSpy('cChange');
var dChange = jasmine.createSpy('dChange');
model.on('change:a', aChange);
model.on('change:b', bChange);
model.on('change:c', cChange);
model.on('change:d', dChange);
//set all but c, and don't change b, and add d
var newAttrs = {
a: 0,
b: "2",
d: "four"
};
model.reset(newAttrs);
//model.reset() fires appropriate change events
expect(aChange).toHaveBeenCalled();
expect(bChange).not.toHaveBeenCalled();
expect(cChange).toHaveBeenCalled();
expect(dChange).toHaveBeenCalled();
//When change handlers are called, expect them to have _all_ of the changes from `reset`
expectChangeHandlerToHaveBeenCalledWith(aChange, newAttrs);
expectChangeHandlerToHaveBeenCalledWith(cChange, newAttrs);
expectChangeHandlerToHaveBeenCalledWith(dChange, newAttrs);
//model.reset() set/unset all attributes appropriately
expect(model.get('c')).toBeUndefined();
expect(model.toJSON()).toEqual(newAttrs);
var changeHandler = jasmine.createSpy('changeHandler');
model.on('change', changeHandler);
model.reset();
expect(changeHandler).toHaveBeenCalled();
var changeHandlerModelArg = changeHandler.calls[0].args[0];
expect(changeHandlerModelArg.toJSON()).toEqual({});
expect(model.toJSON()).toEqual({});
});
it('model.reset() passes `options` into unset and set calls', function () {
var originalAttributes = {
a: 1,
b: "2",
c: "three"
};
model.set(originalAttributes);
expect(model.toJSON()).toEqual(originalAttributes);
var aChange = jasmine.createSpy('aChange');
var bChange = jasmine.createSpy('bChange');
var cChange = jasmine.createSpy('cChange');
var dChange = jasmine.createSpy('dChange');
model.on('change:a', aChange);
model.on('change:b', bChange);
model.on('change:c', cChange);
model.on('change:d', dChange);
//set all but c, and don't change b, and add d
var newAttrs = {
a: 0,
b: "2",
d: "four"
};
//`silent: true` should prevent callbacks from firing
model.reset(newAttrs, {silent: true});
//model.reset() fires appropriate change events
expect(aChange).not.toHaveBeenCalled();
expect(bChange).not.toHaveBeenCalled();
expect(cChange).not.toHaveBeenCalled();
expect(dChange).not.toHaveBeenCalled();
//model.reset() set/unset all attributes appropriately
expect(model.get('c')).toBeUndefined();
expect(model.toJSON()).toEqual(newAttrs);
})
});
});