d0b185b9 by Michael Richards

Merge pull request #80 from mikeric/advanced-custom-binders-api

Advanced custom binders API
2 parents 4093da62 9b103172
......@@ -18,8 +18,8 @@ describe('Rivets.Binding', function() {
model = binding.model;
});
it('gets assigned the routine function matching the identifier', function() {
expect(binding.routine).toBe(rivets.routines.text);
it('gets assigned the proper binder routine matching the identifier', function() {
expect(binding.binder.routine).toBe(rivets.binders.text);
});
describe('bind()', function() {
......@@ -72,50 +72,24 @@ describe('Rivets.Binding', function() {
describe('set()', function() {
it('performs the binding routine with the supplied value', function() {
spyOn(binding, 'routine');
spyOn(binding.binder, 'routine');
binding.set('sweater');
expect(binding.routine).toHaveBeenCalledWith(el, 'sweater');
expect(binding.binder.routine).toHaveBeenCalledWith(el, 'sweater');
});
it('applies any formatters to the value before performing the routine', function() {
rivets.formatters.awesome = function(value) { return 'awesome ' + value };
binding.formatters.push('awesome');
spyOn(binding, 'routine');
spyOn(binding.binder, 'routine');
binding.set('sweater');
expect(binding.routine).toHaveBeenCalledWith(el, 'awesome sweater');
expect(binding.binder.routine).toHaveBeenCalledWith(el, 'awesome sweater');
});
it('calls methods with the object as context', function() {
binding.model = {foo: 'bar'};
spyOn(binding, 'routine');
spyOn(binding.binder, 'routine');
binding.set(function() { return this.foo; });
expect(binding.routine).toHaveBeenCalledWith(el, binding.model.foo);
});
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, binding.model, func, undefined);
});
});
describe('on an iteration binding', function(){
beforeEach(function(){
binding.options.special = 'iteration';
});
it('performs the binding routine with the supplied collection and binding', function() {
spyOn(binding, 'routine');
array = [{name: 'a'}, {name: 'b'}];
binding.set(array);
expect(binding.routine).toHaveBeenCalledWith(el, array, binding);
});
expect(binding.binder.routine).toHaveBeenCalledWith(el, binding.model.foo);
});
});
......
......@@ -18,13 +18,13 @@ describe('Routines', function() {
describe('text', function() {
it("sets the element's text content", function() {
rivets.routines.text(el, '<em>gluten-free</em>');
rivets.binders.text(el, '<em>gluten-free</em>');
expect(el.textContent || el.innerText).toBe('<em>gluten-free</em>');
expect(el.innerHTML).toBe('&lt;em&gt;gluten-free&lt;/em&gt;');
});
it("sets the element's text content to zero when a numeric zero is passed", function() {
rivets.routines.text(el, 0);
rivets.binders.text(el, 0);
expect(el.textContent || el.innerText).toBe('0');
expect(el.innerHTML).toBe('0');
});
......@@ -32,13 +32,13 @@ describe('Routines', function() {
describe('html', function() {
it("sets the element's HTML content", function() {
rivets.routines.html(el, '<strong>fixie</strong>');
rivets.binders.html(el, '<strong>fixie</strong>');
expect(el.textContent || el.innerText).toBe('fixie');
expect(el.innerHTML).toBe('<strong>fixie</strong>');
});
it("sets the element's HTML content to zero when a zero value is passed", function() {
rivets.routines.html(el, 0);
rivets.binders.html(el, 0);
expect(el.textContent || el.innerText).toBe('0');
expect(el.innerHTML).toBe('0');
});
......@@ -46,17 +46,17 @@ describe('Routines', function() {
describe('value', function() {
it("sets the element's value", function() {
rivets.routines.value(input, 'pitchfork');
rivets.binders.value.routine(input, 'pitchfork');
expect(input.value).toBe('pitchfork');
});
it("applies a default value to the element when the model doesn't contain it", function() {
rivets.routines.value(input, undefined);
rivets.binders.value.routine(input, undefined);
expect(input.value).toBe('');
});
it("sets the element's value to zero when a zero value is passed", function() {
rivets.routines.value(input, 0);
rivets.binders.value.routine(input, 0);
expect(input.value).toBe('0');
});
});
......@@ -64,14 +64,14 @@ describe('Routines', function() {
describe('show', function() {
describe('with a truthy value', function() {
it('shows the element', function() {
rivets.routines.show(el, true);
rivets.binders.show(el, true);
expect(el.style.display).toBe('');
});
});
describe('with a falsey value', function() {
it('hides the element', function() {
rivets.routines.show(el, false);
rivets.binders.show(el, false);
expect(el.style.display).toBe('none');
});
});
......@@ -80,14 +80,14 @@ describe('Routines', function() {
describe('hide', function() {
describe('with a truthy value', function() {
it('hides the element', function() {
rivets.routines.hide(el, true);
rivets.binders.hide(el, true);
expect(el.style.display).toBe('none');
});
});
describe('with a falsey value', function() {
it('shows the element', function() {
rivets.routines.hide(el, false);
rivets.binders.hide(el, false);
expect(el.style.display).toBe('');
});
});
......@@ -96,14 +96,14 @@ describe('Routines', function() {
describe('enabled', function() {
describe('with a truthy value', function() {
it('enables the element', function() {
rivets.routines.enabled(el, true);
rivets.binders.enabled(el, true);
expect(el.disabled).toBe(false);
});
});
describe('with a falsey value', function() {
it('disables the element', function() {
rivets.routines.enabled(el, false);
rivets.binders.enabled(el, false);
expect(el.disabled).toBe(true);
});
});
......@@ -112,14 +112,14 @@ describe('Routines', function() {
describe('disabled', function() {
describe('with a truthy value', function() {
it('disables the element', function() {
rivets.routines.disabled(el, true);
rivets.binders.disabled(el, true);
expect(el.disabled).toBe(true);
});
});
describe('with a falsey value', function() {
it('enables the element', function() {
rivets.routines.disabled(el, false);
rivets.binders.disabled(el, false);
expect(el.disabled).toBe(false);
});
});
......@@ -128,14 +128,14 @@ describe('Routines', function() {
describe('checked', function() {
describe('with a truthy value', function() {
it('checks the element', function() {
rivets.routines.checked(el, true);
rivets.binders.checked.routine(el, true);
expect(el.checked).toBe(true);
});
});
describe('with a falsey value', function() {
it('unchecks the element', function() {
rivets.routines.checked(el, false);
rivets.binders.checked.routine(el, false);
expect(el.checked).toBe(false);
});
});
......@@ -144,14 +144,14 @@ describe('Routines', function() {
describe('unchecked', function() {
describe('with a truthy value', function() {
it('unchecks the element', function() {
rivets.routines.unchecked(el, true);
rivets.binders.unchecked.routine(el, true);
expect(el.checked).toBe(false);
});
});
describe('with a falsey value', function() {
it('checks the element', function() {
rivets.routines.unchecked(el, false);
rivets.binders.unchecked.routine(el, false);
expect(el.checked).toBe(true);
});
});
......