binding.js
4.83 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
describe('Rivets.Binding', function() {
var model, el, view, binding;
beforeEach(function() {
rivets.configure({
adapter: {
subscribe: function() {},
unsubscribe: function() {},
read: function() {},
publish: function() {}
}
});
el = document.createElement('div');
el.setAttribute('data-text', 'obj.name');
view = rivets.bind(el, {obj: {}});
binding = view.bindings[0];
model = binding.model;
});
it('gets assigned the routine function matching the identifier', function() {
expect(binding.routine).toBe(rivets.routines.text);
});
describe('bind()', function() {
it('subscribes to the model for changes via the adapter', function() {
spyOn(rivets.config.adapter, 'subscribe');
binding.bind();
expect(rivets.config.adapter.subscribe).toHaveBeenCalled();
});
describe('with preloadData set to true', function() {
beforeEach(function() {
rivets.config.preloadData = true;
});
it('sets the initial value via the adapter', function() {
spyOn(binding, 'set');
spyOn(rivets.config.adapter, 'read');
binding.bind();
expect(binding.set).toHaveBeenCalled();
expect(rivets.config.adapter.read).toHaveBeenCalled();
});
});
describe('with the bypass option set to true', function() {
beforeEach(function() {
binding.options.bypass = true;
});
it('sets the initial value from the model directly', function() {
spyOn(binding, 'set');
binding.model.name = 'espresso';
binding.bind();
expect(binding.set).toHaveBeenCalledWith('espresso');
});
});
});
describe('set()', function() {
it('performs the binding routine with the supplied value', function() {
spyOn(binding, 'routine');
binding.set('sweater');
expect(binding.routine).toHaveBeenCalledWith(el, 'sweater');
});
it('applies any formatters to the value before performing the routine', function() {
rivets.config.formatters = {
awesome: function(value) { return 'awesome ' + value }
};
binding.formatters.push('awesome');
spyOn(binding, 'routine');
binding.set('sweater');
expect(binding.routine).toHaveBeenCalledWith(el, 'awesome sweater');
});
describe('on an event binding', function() {
beforeEach(function() {
binding.options.special = 'event';
});
it('performs the binding routine with the supplied function and current listener', function() {
spyOn(binding, 'routine');
func = function() { return 1 + 2; }
binding.set(func);
expect(binding.routine).toHaveBeenCalledWith(el, func, undefined);
});
it('passes the previously set funcation as the current listener on subsequent calls', function() {
spyOn(binding, 'routine');
funca = function() { return 1 + 2; };
funcb = function() { return 2 + 5; };
binding.set(funca);
expect(binding.routine).toHaveBeenCalledWith(el, funca, undefined);
binding.set(funcb);
expect(binding.routine).toHaveBeenCalledWith(el, funcb, funca);
});
});
});
describe('publish()', function() {
it("should publish the value of a number input", function() {
numberInput = document.createElement('input');
numberInput.setAttribute('type', 'number');
numberInput.setAttribute('data-value', 'obj.num');
view = rivets.bind(numberInput, {obj: {num: 42}});
binding = view.bindings[0];
model = binding.model;
numberInput.value = 42;
spyOn(rivets.config.adapter, 'publish');
binding.publish({target: numberInput});
expect(rivets.config.adapter.publish).toHaveBeenCalledWith(model, 'num', '42');
});
});
describe('formattedValue()', function() {
it('applies the current formatters on the supplied value', function() {
rivets.config.formatters = {
awesome: function(value) { return 'awesome ' + value }
};
binding.formatters.push('awesome');
expect(binding.formattedValue('hat')).toBe('awesome hat');
});
it('uses formatters on the model', function() {
model.modelAwesome = function(value) { return 'model awesome ' + value };
binding.formatters.push('modelAwesome');
expect(binding.formattedValue('hat')).toBe('model awesome hat');
});
describe('with a multi-argument formatter string', function() {
beforeEach(function() {
rivets.config.formatters = {
awesome: function(value, prefix) {
return prefix + ' awesome ' + value;
}
};
binding.formatters.push('awesome super');
});
it('applies the formatter with arguments', function() {
expect(binding.formattedValue('jacket')).toBe('super awesome jacket');
});
});
});
});