2011-02-23 22:03:38 +01:00
|
|
|
describe( 'Entwine', function() {
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
$('body').append('<div id="dom_test"></div>')
|
|
|
|
});
|
2009-11-21 03:33:06 +01:00
|
|
|
|
2011-02-23 22:03:38 +01:00
|
|
|
afterEach(function() {
|
|
|
|
$('#dom_test').remove()
|
|
|
|
});
|
2009-11-21 03:33:06 +01:00
|
|
|
|
2011-02-23 22:03:38 +01:00
|
|
|
describe( 'Ctors', function() {
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2010-04-13 07:45:29 +02:00
|
|
|
$.entwine.synchronous_mode();
|
|
|
|
$.entwine.clear_all_rules()
|
2009-11-21 03:33:06 +01:00
|
|
|
$('#dom_test').html('<div id="a" class="a b c"></div>')
|
2011-02-23 22:03:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls onmatch when new element created', function() {
|
2009-11-21 03:33:06 +01:00
|
|
|
var a = false;
|
2010-04-13 07:45:29 +02:00
|
|
|
$('#b').entwine({onmatch: function(){a = true;} });
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toBeFalsy();
|
2009-11-21 03:33:06 +01:00
|
|
|
$('#a').after('<div id="b"></div>');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls onunmatch when new element deleted', function() {
|
2009-11-21 03:33:06 +01:00
|
|
|
var a = 0;
|
2010-04-13 07:45:29 +02:00
|
|
|
$('#b').entwine({onmatch: function(){a = 1;}, onunmatch: function(){a = 2;} });
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 0);
|
2009-11-21 03:33:06 +01:00
|
|
|
$('#a').after('<div id="b"></div>');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 1);
|
2009-11-21 03:33:06 +01:00
|
|
|
$('#b').remove();
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls onmatch when ruleset matches after class added', function() {
|
2009-11-21 03:33:06 +01:00
|
|
|
var a = 0;
|
2010-04-13 07:45:29 +02:00
|
|
|
$('#a.foo').entwine({onmatch: function(){a = 1;} });
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual(0);
|
2009-11-21 03:33:06 +01:00
|
|
|
$('#a').addClass('foo');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect(a).toEqual( 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls onmatch in both direct and namespaced onmatch, does not call less specific onmatch', function() {
|
2009-11-21 03:33:06 +01:00
|
|
|
var a = 0, b=0, c=0, d=0;
|
2010-04-13 07:45:29 +02:00
|
|
|
$('.foo').entwine({onmatch: function(){a = 1;}})
|
2011-02-23 22:03:38 +01:00
|
|
|
$('.foo').entwine('bar', function($){return{onmatch: function(){b = 1;}}})
|
2010-04-13 07:45:29 +02:00
|
|
|
$('#a.foo').entwine({onmatch: function(){c = 1;}})
|
2011-02-23 22:03:38 +01:00
|
|
|
$('#a.foo').entwine('bar', function($){return{onmatch: function(){d = 1}}})
|
|
|
|
expect([a, b, c, d]).toEqual( [0, 0, 0, 0]);
|
2009-11-21 03:33:06 +01:00
|
|
|
$('#a').addClass('foo');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect([a, b, c, d]).toEqual( [0, 0, 1, 1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it( 'calls onmatch in both direct and namespaced onmatch, super works as expected', function() {
|
2009-11-21 03:33:06 +01:00
|
|
|
var a = 0, b=0, c=0, d=0;
|
2010-04-13 07:45:29 +02:00
|
|
|
$('.foo').entwine({onmatch: function(){a += 1;}})
|
2011-02-23 22:03:38 +01:00
|
|
|
$('.foo').entwine('bar', function($){return{onmatch: function(){b += 1;}}})
|
2010-04-13 07:45:29 +02:00
|
|
|
$('#a.foo').entwine({onmatch: function(){this._super(); c = 1; this._super();}})
|
2011-02-23 22:03:38 +01:00
|
|
|
$('#a.foo').entwine('bar', function($){return{onmatch: function(){this._super(); d = 1; this._super();}}})
|
|
|
|
expect([a, b, c, d]).toEqual( [0, 0, 0, 0]);
|
2009-11-21 03:33:06 +01:00
|
|
|
$('#a').addClass('foo');
|
2011-02-23 22:03:38 +01:00
|
|
|
expect([a, b, c, d]).toEqual( [2, 2, 1, 1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|