rivets-backbone-adapter-brainfood.spec.js
7.36 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
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
define(function(require) {
'use strict';
var $ = require('jquery');
window.jQuery = $;
var RivetsBackboneAdapterBrainfood = require('rivets-backbone-adapter-brainfood');
var _ = require('underscore');
var Backbone = require('backbone');
var rivets = require('rivets');
require('backbone-validation');
_.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
//rivets.config.rootInterface = ':';
/* global console:false */
describe('RivetsBackboneAdapterBrainfood', function() {
it('returns undefined', function() {
expect(RivetsBackboneAdapterBrainfood).toBeDefined();
});
var ContactMech, ContactMechs;
beforeEach(function() {
ContactMech = Backbone.Model.extend({
defaults: {
contactMechId: '',
contactMechPurposeTypeId: '',
infoString: '',
address1: '',
},
});
ContactMechs = Backbone.Collection.extend({
modelId: function(attrs) {
return attrs.contactMechPurposeTypeId;
},
});
});
var contactMechs, model;
beforeEach(function() {
contactMechs = new ContactMechs([
new ContactMech({
contactMechPurposeTypeId: 'PRIMARY_EMAIL',
infoString: 'user@example.com',
}),
new ContactMech({
contactMechPurposeTypeId: 'PRIMARY_ADDRESS',
address1: '1234 Main St.',
}),
]);
model = new Backbone.Model({contactMechs: contactMechs, notBackbone: {}});
});
function buildTest(path, options) {
it(path, function() {
var inputElement = document.createElement('input');
var binderName = options.bb ? 'bb-model' : 'value';
inputElement.setAttribute('rv-' + binderName, path);
var pojoObjects = [];
var view = rivets.bind(inputElement, model, {
binders: {
'bb-model': {
bind: function(el) {
console.log('bb-model:bind', el);
},
routine: function(el, value) {
var pojoObject = options.parse(value);
console.log('bb-model:routine', el, JSON.stringify(pojoObject));
pojoObjects.push(pojoObject);
},
unbind: function(el) {
console.log('bb-model:unbind', el);
},
},
},
formatters: {
toJSON: function(value) {
return value.toJSON();
},
jsonStringify: {
read: function(value) {
return JSON.stringify(value);
},
publish: function(value) {
return JSON.parse(value);
},
},
},
});
function getInputElementValue() {
var rawValue = inputElement.value;
if (options.parse) {
rawValue = options.parse(rawValue);
}
return rawValue;
}
function convertValue(value) {
if (typeof value === 'function') {
return value();
} else {
return value;
}
}
var value = convertValue(options.value);
if (options.bb) {
expect(pojoObjects[0]).toEqual(value);
} else {
expect(getInputElementValue()).toEqual(value);
}
if (options.get) {
var newValue = convertValue(options.newValue);
inputElement.value = newValue;
$(inputElement).trigger('input').trigger('change');
expect(options.get()).toEqual(newValue);
if (options.set) {
options.set(value);
expect(getInputElementValue()).toEqual(value);
}
} else if (options.set) {
}
});
}
buildTest(':contactMechs:PRIMARY_EMAIL:infoString', {
value: 'user@example.com',
newValue: '(user@example.com)',
get: function() {
return contactMechs.get('PRIMARY_EMAIL').get('infoString');
},
});
buildTest(':contactMechs:PRIMARY_ADDRESS:address1', {
value: '1234 Main St.',
newValue: '(1234 Main St.)',
get: function() {
return contactMechs.get('PRIMARY_ADDRESS').get('address1');
},
set: function(value) {
contactMechs.get('PRIMARY_ADDRESS').set('address1', value);
},
});
buildTest(':contactMechs:PRIMARY_EMAIL:*.infoString', {
value: 'user@example.com',
});
buildTest(':notBackbone:notValid', {
value: '',
});
//buildTest(':contactMechs:*.length', '2');
buildTest(':contactMechs:PRIMARY_EMAIL', {
bb: true,
value: function() {
return contactMechs.get('PRIMARY_EMAIL').toJSON();
},
parse: function(rawValue) {
return rawValue.toJSON();
},
});
buildTest(':contactMechs:PRIMARY_EMAIL:*', {
bb: true,
value: function() {
return contactMechs.get('PRIMARY_EMAIL').toJSON();
},
parse: function(rawValue) {
return rawValue;
},
});
buildTest(':contactMechs', {
bb: true,
value: function() {
return contactMechs.toJSON();
},
parse: function(rawValue) {
return rawValue.toJSON();
},
});
buildTest(':contactMechs:*', {
bb: true,
value: function() {
return contactMechs.models;
},
parse: function(rawValue) {
return rawValue;
},
});
/*
buildTest(':contactMechs:PRIMARY_EMAIL:* | jsonStringify', {
value: function() {
var primaryEmail = contactMechs.get('PRIMARY_EMAIL');
return primaryEmail.toJSON();
},
newValue: JSON.stringify({
infoString: 'someone@example.com',
}),
parse: function(rawValue) {
return JSON.parse(rawValue);
},
get: function() {
var primaryEmail = contactMechs.get('PRIMARY_EMAIL');
console.log('primaryEmail', JSON.stringify(primaryEmail.toJSON()));
return primaryEmail.toJSON();
},
});
*/
});
});