backbone-model-overlay.spec.js
7.18 KB
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
define(function(require) {
'use strict';
var $ = require('jquery');
window.jQuery = $;
var _ = require('underscore');
var Backbone = require('backbone');
var BackboneModelOverlay = require('backbone-model-overlay');
describe('BackboneModelOverlay', function() {
var staticParentData = {inParent: 'Parent'};
var staticChildData = {inChild: 'Child'};
it('exists', function() {
expect(BackboneModelOverlay).toBeDefined();
});
var model, parent;
var attributes;
var perAttributeChange;
var previous;
function doBeforeEach(parentData, childData) {
var options;
if (parentData) {
parent = new Backbone.Model(parentData);
options = {parent: parent};
} else {
parent = null;
}
model = new BackboneModelOverlay(childData, options);
attributes = [];
perAttributeChange = {};
previous = [];
model.on('change', function() {
attributes.push(model.toJSON());
previous.push(model.previousAttributes());
_.each(model.changedAttributes(), function(value, key) {
if (perAttributeChange[key] === undefined) {
perAttributeChange[key] = [value];
} else {
perAttributeChange[key].push(value);
}
});
});
}
describe('no-parent', function() {
describe('empty', function() {
beforeEach(function() {
doBeforeEach();
});
it('get', function() {
expect(perAttributeChange).toEqual({});
expect(previous).toEqual([]);
expect(attributes).toEqual([]);
expect(model.get('missing')).toBeUndefined();
expect(model.toJSON()).toEqual({});
});
});
describe('baseline', function() {
beforeEach(function() {
doBeforeEach(null, staticChildData);
model.set('newChild', 'OldChild', {overlay: true});
model.set('newChild', 'NewChild', {overlay: true});
});
it('get', function() {
expect(perAttributeChange).toEqual({
newChild: ['OldChild', 'NewChild'],
});
expect(previous).toEqual([
{inChild: 'Child'},
{inChild: 'Child', newChild: 'OldChild'},
]);
expect(attributes).toEqual([
{inChild: 'Child', newChild: 'OldChild'},
{inChild: 'Child', newChild: 'NewChild'},
]);
expect(model.get('missing')).toBeUndefined();
expect(model.toJSON()).toEqual({inChild: 'Child', newChild: 'NewChild'});
});
});
});
describe('with-parent', function() {
beforeEach(function() {
doBeforeEach(staticParentData, staticChildData);
});
describe('initial', function() {
it('get', function() {
expect(parent.get('missing')).toBeUndefined();
expect(model.get('missing')).toBeUndefined();
expect(parent.get('inParent')).toEqual('Parent');
expect(model.get('inParent')).toEqual('Parent');
expect(parent.get('inChild')).toBeUndefined();
expect(model.get('inChild')).toEqual('Child');
expect(attributes).toEqual([]);
expect(model.toJSON()).toEqual({inParent: 'Parent', inChild: 'Child'});
});
});
describe('add', function() {
beforeEach(function() {
parent.set('newParent', 'OldParent');
model.set('newChild', 'OldChild', {overlay: true});
parent.set('newParent', 'NewParent');
model.set('newChild', 'NewChild', {overlay: true});
model.set('passThruParent', 'PassThruParent');
});
it('get:perAttributeChange', function() {
expect(perAttributeChange).toEqual({
newParent: ['OldParent', 'OldParent', 'NewParent', 'NewParent'],
newChild: ['OldChild', 'NewChild'],
passThruParent: ['PassThruParent'],
});
});
it('get:previous', function() {
_.each(previous, function(v) {
//console.log('111', JSON.stringify(v));
});
expect(previous).toEqual([
{inParent: 'Parent'},
{inParent: 'Parent', inChild: 'Child'},
{inParent: 'Parent', newParent: 'OldParent', inChild: 'Child'},
{inParent: 'Parent', newParent: 'OldParent', inChild: 'Child', newChild: 'OldChild'},
{inParent: 'Parent', newParent: 'NewParent', inChild: 'Child', newChild: 'OldChild'},
]);
});
it('get:attributes', function() {
expect(attributes).toEqual([
{inParent: 'Parent', newParent: 'OldParent', inChild: 'Child'},
{inParent: 'Parent', newParent: 'OldParent', inChild: 'Child', newChild: 'OldChild'},
{inParent: 'Parent', newParent: 'NewParent', inChild: 'Child', newChild: 'OldChild'},
{inParent: 'Parent', newParent: 'NewParent', inChild: 'Child', newChild: 'NewChild'},
{inParent: 'Parent', newParent: 'NewParent', inChild: 'Child', newChild: 'NewChild', passThruParent: 'PassThruParent'},
]);
});
it('get:parent:separate-checks', function() {
expect(parent.get('missing')).toBeUndefined();
expect(model.get('missing')).toBeUndefined();
expect(parent.get('inParent')).toEqual('Parent');
expect(parent.get('newParent')).toEqual('NewParent');
expect(model.get('inParent')).toEqual('Parent');
expect(model.get('newParent')).toEqual('NewParent');
});
it('get:child:separate-checks:1', function() {
expect(parent.get('inChild')).toBeUndefined();
expect(parent.get('newChild')).toBeUndefined();
expect(model.get('inChild')).toEqual('Child');
});
it('get:child:separate-checks:2', function() {
expect(model.get('newChild')).toEqual('NewChild');
expect(model.toJSON()).toEqual({inParent: 'Parent', newParent: 'NewParent', inChild: 'Child', newChild: 'NewChild', passThruParent: 'PassThruParent'});
});
});
});
});
});