9b3d6099 by Michael Richards

Add initial set of tests for Rivets.Binding.

1 parent 5d8926e0
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
13 13
14 <script type="text/javascript" src="matchers.js"></script> 14 <script type="text/javascript" src="matchers.js"></script>
15 <script type="text/javascript" src="mock.data.js"></script> 15 <script type="text/javascript" src="mock.data.js"></script>
16 <script type="text/javascript" src="rivets/binding.js"></script>
16 <script type="text/javascript" src="rivets.js"></script> 17 <script type="text/javascript" src="rivets.js"></script>
17 18
18 <script type="text/javascript"> 19 <script type="text/javascript">
......
1 describe('Rivets.Binding', function() {
2 var model, el, view, binding;
3
4 beforeEach(function() {
5 rivets.configure({
6 adapter: {
7 subscribe: function() {},
8 unsubscribe: function() {},
9 read: function() {},
10 publish: function() {}
11 }
12 });
13
14 el = document.createElement('div');
15 el.setAttribute('data-text', 'obj.name');
16 view = rivets.bind(el, {obj: {}});
17 binding = view.bindings[0];
18 });
19
20 it('gets assigned the routine function matching the identifier', function() {
21 expect(binding.routine).toBe(rivets.routines.text);
22 });
23
24 describe('set()', function() {
25 it('performs the binding routine with the supplied value', function() {
26 spyOn(binding, 'routine');
27 binding.set('sweater');
28 expect(binding.routine).toHaveBeenCalledWith(el, 'sweater');
29 });
30
31 it('applies any formatters to the value before performing the routine', function() {
32 rivets.config.formatters = {
33 awesome: function(value) { return 'awesome ' + value }
34 };
35 binding.formatters.push('awesome');
36 spyOn(binding, 'routine');
37 binding.set('sweater');
38 expect(binding.routine).toHaveBeenCalledWith(el, 'awesome sweater');
39 });
40
41 describe('on an event binding', function() {
42 beforeEach(function() {
43 binding.bindType = 'event';
44 });
45
46 it('performs the binding routine with the supplied function and current listener', function() {
47 spyOn(binding, 'routine');
48 func = function() { return 1 + 2; }
49 binding.set(func);
50 expect(binding.routine).toHaveBeenCalledWith(el, func, undefined);
51 });
52
53 it('passes the previously set funcation as the current listener on subsequent calls', function() {
54 spyOn(binding, 'routine');
55 funca = function() { return 1 + 2; };
56 funcb = function() { return 2 + 5; };
57
58 binding.set(funca);
59 expect(binding.routine).toHaveBeenCalledWith(el, funca, undefined);
60
61 binding.set(funcb);
62 expect(binding.routine).toHaveBeenCalledWith(el, funcb, funca);
63 });
64 });
65 });
66 });