functional.js
6.72 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
describe('Functional', function() {
var data, bindData, el, input;
beforeEach(function() {
data = new Data({foo: 'bar', items: [{name: 'a'}, {name: 'b'}]});
bindData = {data: data};
el = document.createElement('div');
input = document.createElement('input');
input.setAttribute('type', 'text');
rivets.configure({
preloadData: true,
adapter: {
subscribe: function(obj, keypath, callback) {
obj.on(keypath, callback);
},
read: function(obj, keypath) {
return obj.get(keypath);
},
publish: function(obj, keypath, value) {
attributes = {};
attributes[keypath] = value;
obj.set(attributes);
}
}
});
});
describe('Adapter', function() {
it('should read the initial value', function() {
spyOn(data, 'get');
el.setAttribute('data-text', 'data.foo');
rivets.bind(el, bindData);
expect(data.get).toHaveBeenCalledWith('foo');
});
it('should read the initial value unless preloadData is false', function() {
rivets.configure({preloadData: false});
spyOn(data, 'get');
el.setAttribute('data-value', 'data.foo');
rivets.bind(el, bindData);
expect(data.get).not.toHaveBeenCalled();
});
it('should subscribe to updates', function() {
spyOn(data, 'on');
el.setAttribute('data-value', 'data.foo');
rivets.bind(el, bindData);
expect(data.on).toHaveBeenCalled();
});
});
describe('Binds', function() {
describe('Text', function() {
it('should set the text content of the element', function() {
el.setAttribute('data-text', 'data.foo');
rivets.bind(el, bindData);
expect(el.textContent || el.innerText).toBe(data.get('foo'));
});
it('should correctly handle HTML in the content', function() {
el.setAttribute('data-text', 'data.foo');
value = '<b>Fail</b>';
data.set({foo: value});
rivets.bind(el, bindData);
expect(el.textContent || el.innerText).toBe(value);
});
});
describe('HTML', function() {
it('should set the html content of the element', function() {
el.setAttribute('data-html', 'data.foo');
rivets.bind(el, bindData);
expect(el).toHaveTheTextContent(data.get('foo'));
});
it('should correctly handle HTML in the content', function() {
el.setAttribute('data-html', 'data.foo');
value = '<b>Fail</b>';
data.set({foo: value});
rivets.bind(el, bindData);
expect(el.innerHTML).toBe(value);
});
});
describe('Value', function() {
it('should set the value of the element', function() {
input.setAttribute('data-value', 'data.foo');
rivets.bind(input, bindData);
expect(input.value).toBe(data.get('foo'));
});
});
describe('Multiple', function() {
it('should bind a list of multiple elements', function() {
el.setAttribute('data-html', 'data.foo');
input.setAttribute('data-value', 'data.foo');
rivets.bind([el, input], bindData);
expect(el).toHaveTheTextContent(data.get('foo'));
expect(input.value).toBe(data.get('foo'));
});
});
describe('Iteration', function() {
beforeEach(function(){
list = document.createElement('ul');
el.appendChild(list);
listItem = document.createElement('li');
listItem.setAttribute('data-each-item', 'data.items');
list.appendChild(listItem);
});
it('should loop over a collection and create new instances of that element + children', function() {
expect(el.getElementsByTagName('li').length).toBe(1);
rivets.bind(el, bindData);
expect(el.getElementsByTagName('li').length).toBe(2);
});
it('should not fail if the collection being bound to is null', function() {
data.set({ items: null});
rivets.bind(el, bindData);
expect(el.getElementsByTagName('li').length).toBe(0);
});
it('should re-loop over the collection and create new instances when the array changes', function() {
rivets.bind(el, bindData);
expect(el.getElementsByTagName('li').length).toBe(2);
newItems = [{name: 'a'}, {name: 'b'}, {name: 'c'}];
data.set({items: newItems});
expect(el.getElementsByTagName('li').length).toBe(3);
});
it('should allow binding to the iterated item as well as any parent contexts', function() {
span1 = document.createElement('span');
span1.setAttribute('data-text', 'item:name')
span2 = document.createElement('span');
span2.setAttribute('data-text', 'data.foo')
listItem.appendChild(span1);
listItem.appendChild(span2);
rivets.bind(el, bindData);
expect(el.getElementsByTagName('span')[0]).toHaveTheTextContent('a');
expect(el.getElementsByTagName('span')[1]).toHaveTheTextContent('bar');
});
it('should allow binding to the iterated element directly', function() {
listItem.setAttribute('data-text', 'item:name');
listItem.setAttribute('data-class', 'data.foo');
rivets.bind(el, bindData);
expect(el.getElementsByTagName('li')[0]).toHaveTheTextContent('a');
expect(el.getElementsByTagName('li')[0].className).toBe('bar');
});
it('should insert items between any surrounding elements', function(){
firstItem = document.createElement('li');
lastItem = document.createElement('li');
firstItem.textContent = 'first';
lastItem.textContent = 'last';
list.appendChild(lastItem);
list.insertBefore(firstItem, listItem);
listItem.setAttribute('data-text', 'item:name');
rivets.bind(el, bindData);
expect(el.getElementsByTagName('li')[0]).toHaveTheTextContent('first');
expect(el.getElementsByTagName('li')[1]).toHaveTheTextContent('a');
expect(el.getElementsByTagName('li')[2]).toHaveTheTextContent('b');
expect(el.getElementsByTagName('li')[3]).toHaveTheTextContent('last');
})
});
});
describe('Updates', function() {
it('should change the value', function() {
el.setAttribute('data-text', 'data.foo');
rivets.bind(el, bindData);
data.set({foo: 'some new value'});
expect(el).toHaveTheTextContent(data.get('foo'));
});
});
describe('Input', function() {
it('should update the model value', function() {
input.setAttribute('data-value', 'data.foo');
rivets.bind(input, bindData);
input.value = 'some new value';
var event = document.createEvent('HTMLEvents')
event.initEvent('change', true, true);
input.dispatchEvent(event);
expect(input.value).toBe(data.get('foo'));
});
});
});