2011-02-23 22:03:38 +01:00
|
|
|
describe( 'Entwine', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
|
2011-02-23 22:03:38 +01:00
|
|
|
beforeEach(function() {
|
|
|
|
$('body').append('<div id="dom_test"></div>')
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
$('#dom_test').remove()
|
|
|
|
});
|
|
|
|
|
|
|
|
describe( 'Events', function() {
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
$.entwine.synchronous_mode();
|
|
|
|
$.entwine.clear_all_rules()
|
|
|
|
$('#dom_test').html('<div id="a" class="a b c"></div>')
|
2011-02-23 22:03:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls onfoo when foo triggered', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var a = 0;
|
|
|
|
$('#a').entwine({onfoo: function(){a = 1;} });
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual(0);
|
2010-04-13 07:45:29 +02:00
|
|
|
$('#a').trigger('foo');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'only calls most specific onfoo when foo triggered', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var a = 0, b = 0;
|
|
|
|
$('#a.a').entwine({onfoo: function(){a = 1;} });
|
|
|
|
$('#a').entwine({onfoo: function(){b = 1;} });
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 0);
|
|
|
|
expect(b).toEqual( 0);
|
2010-04-13 07:45:29 +02:00
|
|
|
$('#a').trigger('foo');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 1);
|
|
|
|
expect(b).toEqual( 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls namespaced onfoo when foo triggered', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var a = 0;
|
|
|
|
$('#a').entwine('bar', function($){return{onfoo: function(){a = 1;} }});
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 0);
|
2010-04-13 07:45:29 +02:00
|
|
|
$('#a').trigger('foo');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls most specific namespaced onfoo and most specific non-namespaced onfoo when foo triggered', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var a = 0, b = 0, c = 0, d = 0;
|
|
|
|
$('#a.a').entwine({onfoo: function(){a = 1;} });
|
|
|
|
$('#a').entwine({onfoo: function(){b = 1;} });
|
|
|
|
$('#a.a').entwine('bar', function($){return{onfoo: function(){c = 1;} }});
|
|
|
|
$('#a').entwine('bar', function($){return{onfoo: function(){d = 1;} }});
|
2011-02-23 22:03:38 +01:00
|
|
|
expect([a, b, c, d]).toEqual( [0, 0, 0, 0] );
|
2010-04-13 07:45:29 +02:00
|
|
|
|
|
|
|
$('#a').trigger('foo');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect([a, b, c, d]).toEqual( [1, 0, 1, 0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls up correctly on _super', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var a = 0, b = 0;
|
|
|
|
$('#a').entwine({onfoo: function(){a += 1;} });
|
|
|
|
$('#a.a').entwine({onfoo: function(){this._super(); b += 1; this._super();} });
|
|
|
|
|
2011-02-23 22:03:38 +01:00
|
|
|
expect([a, b]).toEqual( [0, 0]);
|
2010-04-13 07:45:29 +02:00
|
|
|
$('#a').trigger('foo')
|
2011-02-23 22:03:38 +01:00
|
|
|
expect([a, b]).toEqual( [2, 1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'passes event object', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var event;
|
|
|
|
$('#a').entwine({onfoo: function(e){event = e;} });
|
|
|
|
$('#a').trigger('foo');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(event.type).toBeDefined();
|
|
|
|
expect(event.type).toEqual('foo');
|
|
|
|
expect(event.target).toHaveAttr( 'id', 'a');
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'delegates submit events to forms', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var a = 0;
|
|
|
|
$('<form class="foo" action="javascript:undefined">').appendTo('#dom_test');
|
|
|
|
|
|
|
|
$('.foo').entwine({onsubmit: function(e, d){a = 1;} });
|
|
|
|
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 0);
|
2010-04-13 07:45:29 +02:00
|
|
|
$('.foo').trigger('submit');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe( 'can pass event data', function() {
|
|
|
|
it( 'on custom events', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var data;
|
|
|
|
$('#a').entwine({onfoo: function(e, d){data = d;} });
|
|
|
|
$('#a').trigger('foo', {cheese: 'burger'});
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(data.cheese).toEqual( 'burger');
|
|
|
|
});
|
2010-04-13 07:45:29 +02:00
|
|
|
|
2011-02-23 22:03:38 +01:00
|
|
|
it( 'on normal events', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var data;
|
|
|
|
$('#a').entwine({onclick: function(e, d){data = d;} });
|
|
|
|
$('#a').trigger('click', {finger: 'left'});
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(data.finger).toEqual( 'left');
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'on submit', function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
var data;
|
|
|
|
|
|
|
|
$('<form class="foo" action="javascript:undefined">').appendTo('#dom_test');
|
|
|
|
$('.foo').entwine({onsubmit: function(e, d){data = d; return false;} })
|
|
|
|
|
|
|
|
$('.foo').trigger('submit', {cheese: 'burger'});
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(data.cheese).toEqual( 'burger');
|
|
|
|
});
|
|
|
|
});
|
2011-05-12 08:11:41 +02:00
|
|
|
|
|
|
|
describe( 'calls onchange on checkboxes properly', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
$('#dom_test').html('<input id="i" type="checkbox" name="test_input_i" value="i" />');
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls onchange', function() {
|
|
|
|
var a = 0;
|
|
|
|
|
|
|
|
$('#i').entwine({onchange: function(){ a += 1; }});
|
|
|
|
|
|
|
|
// Can't just "click()" - it's not the same as an actual click event
|
|
|
|
$('#i').trigger('focusin'); $('#i')[0].click();
|
|
|
|
expect(a).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls onchange only once per change', function() {
|
|
|
|
var a = 0;
|
|
|
|
|
|
|
|
$('#i').entwine({onchange: function(){ a += 1; }});
|
|
|
|
|
|
|
|
$('#i').trigger('focusin'); $('#i')[0].click();
|
|
|
|
expect(a).toEqual(1);
|
|
|
|
|
|
|
|
$('#i').trigger('focusout'); $('#i').trigger('focusin'); $('#i').trigger('focusout');
|
|
|
|
expect(a).toEqual(1);
|
|
|
|
|
|
|
|
$('#i')[0].click();
|
|
|
|
expect(a).toEqual(2);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls onchange even if checked attribute altered in mean time', function() {
|
|
|
|
var a = 0;
|
|
|
|
|
|
|
|
$('#i').entwine({onchange: function(){ a += 1; }});
|
|
|
|
|
|
|
|
$('#i').trigger('focusin'); $('#i')[0].click();
|
|
|
|
expect(a).toEqual(1);
|
|
|
|
|
|
|
|
$('#i').removeAttr('checked');
|
|
|
|
|
|
|
|
$('#i').trigger('focusin'); $('#i')[0].click();
|
|
|
|
expect(a).toEqual(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-02-23 22:03:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|