(function(undefined){ var config, ss = shoestring; var $fixture = shoestring( '#qunit-fixture' ); module( 'dom', config = { setup: function() { $fixture = shoestring( '#qunit-fixture' ); }, teardown: function() { $fixture.unbind("foo"); $(document).unbind("foo"); } }); test( '`.add()` adds selected elements to the set', function(){ var length, count; length = $fixture.length; count = shoestring( '.add' ).length; $fixture = $fixture.add( '.add' ); ok( count > 0 ); equal( $fixture.length, length + count ); }); test( '`.addClass()` adds a classes when they doesnt exit', function(){ var $element = $fixture.find( '.add-class' ); $element.addClass( 'foo bar baz' ); equal( $element.attr( "class" ), "add-class foo bar baz" ); }); test( '`.addClass()` doesnt duplicate classes', function(){ var $element = $fixture.find( '.add-class' ); $element.addClass( 'add-class' ); equal( $element.attr( "class" ), "add-class" ); }); test( '`.after()` inserts a sibling after the current obj element', function(){ expect( 3 ); var $element = $fixture.find( '.after' ); equal( $fixture.find( '.foo-after' ).length, 0 ); $element.after( "
"); equal( $fixture.find( '.foo-after' ).length, 1 ); // sibling to .foo-after $fixture.children().each(function(i) { if( shoestring( this ).is( '.after' ) ){ equal( $fixture.children()[i+1].className, "foo-after" ); } }); }); test( '`.after()` inserts siblings after the current obj element in the correct order', function(){ expect( 6 ); var $element = $fixture.find( '.after' ); equal( $fixture.find( '.foo-after' ).length, 0 ); equal( $fixture.find( '.foo-after2' ).length, 0 ); $element.after( " "); equal( $fixture.find( '.foo-after' ).length, 1 ); equal( $fixture.find( '.foo-after2' ).length, 1 ); // sibling to .foo-after $fixture.children().each(function(i) { if( shoestring( this ).is( '.after' ) ){ equal( $fixture.children()[i+1].className, "foo-after" ); equal( $fixture.children()[i+2].className, "foo-after2" ); } }); }); test( '`.insertAfter()` inserts after the selector', function(){ expect( 3 ); equal( $fixture.find( '.foo-after' ).length, 0 ); shoestring( " ").insertAfter( '.after' ); equal( $fixture.find( '.foo-after' ).length, 1 ); // sibling to .foo-after $fixture.children().each(function(i) { if( shoestring( this ).is( '.after' ) ){ equal( $fixture.children()[i+1].className, "foo-after" ); } }); }); test( '`.append()` inserts a child in the current obj element', function(){ var $element = $fixture.find( '.append' ); equal( $element.find( '.foo-append' ).length, 0 ); $element.append( " "); equal( $element.find( '.foo-append' ).length, 1 ); }); test( '`.insertAfter()` inserts after the selector', function(){ var $element = $fixture.find( '.append' ); equal( $element.find( '.foo-append' ).length, 0 ); shoestring( " ").appendTo( $element ); equal( $element.find( '.foo-append' ).length, 1 ); }); test( '`.attr()` returns undefined on empty set', function(){ var $element = $( '#this_will_not_match' ); equal( $element.attr( 'class' ), undefined ); }); test( '`.attr()` gets the attribute', function(){ var $element = $fixture.find( '.attr' ); equal( $element.attr( 'class' ), "attr" ); }); test( '`.attr()` sets the attribute', function(){ var $element = $fixture.find( '.attr' ); equal( $element.attr( 'class', "foo" ).attr( 'class' ), "foo" ); }); test( '`.before()` inserts a sibling before the current obj element', function(){ expect( 3 ); var $element = $fixture.find( '.before' ); equal( $fixture.find( '.foo-before' ).length, 0 ); $element.before( " "); equal( $fixture.find( '.foo-before' ).length, 1 ); // sibling to .foo-before $fixture.children().each(function(i) { if( shoestring( this ).is( '.before' ) ){ equal( $fixture.children()[i-1].className, "foo-before" ); } }); }); test( '`.data` and falsy values', function() { var $fixture = shoestring( '#qunit-fixture' ), $el; $fixture.html( '' ); $el = $( "#el" ); $el.data( "val-false", false ); strictEqual( $( '#el' ).data( "val-false" ), false ); $el.data( "val-zero", 0 ); strictEqual( $( '#el' ).data( "val-zero" ), 0 ); $el.data( "val-undefined", undefined ); strictEqual( $( '#el' ).data( "val-undefined" ), undefined ); }); test( '`.data` works on empty nodelists', function() { var $fixture = shoestring( '#qunit-fixture' ), $el; $fixture.html( '' ); $el = $( "#el" ); strictEqual( $( '#thiswontmatch' ).data(), undefined, 'should be undefined on an empty result set.' ); strictEqual( $( '#thiswontmatch' ).data( "somekey" ), undefined, 'should be undefined on an empty result set with a key passed in.' ); deepEqual( $( '#el' ).data(), {}, 'should be an empty object on an nonempty result set.' ); strictEqual( $( '#el' ).data( "somekey" ), undefined, 'should be undefined on an nonempty result set with a key passed in.' ); }); test( '`.data` does not alias to `data-` attributes', function() { expect( 3 ); var $fixture = shoestring( '#qunit-fixture' ), $el; $fixture.html( '' ); $el = $( "#el" ); strictEqual( $( '#el' ).data( "attr0" ), undefined, 'attribute does not exist, should not throw an error.' ); throws(function() { $( '#el' ).data( "attr1" ); }, 'attribute exists but has no value, should have thrown a dev error.' ); throws(function() { $( '#el' ).data( "attr2" ); }, 'attribute exists and has a value, should have thrown a dev error.' ); }); test( '`.insertBefore()` inserts before the selector', function(){ expect( 3 ); equal( $fixture.find( '.foo-before' ).length, 0 ); shoestring( " ").insertBefore( '.before' ); equal( $fixture.find( '.foo-before' ).length, 1 ); // sibling to .foo-before $fixture.children().each(function(i) { if( shoestring( this ).is( '.before' ) ){ equal( $fixture.children()[i-1].className, "foo-before" ); } }); }); test( '`.clone()` prevents alteration of original', function() { var $clone, $element; $element = $fixture.find( ".clone" ); $clone = $element.clone(); equal( $element.attr( "class" ), "clone" ); equal( $clone.attr( "class" ), "clone" ); $clone.attr( "class", "foo" ); equal( $element.attr( "class" ), "clone" ); equal( $clone.attr( "class" ), "foo" ); }); test( '`.closest()`', function() { var $fixture = shoestring( '#qunit-fixture' ); var $child = $fixture.find( '.closest .child' ); equal( $child[0], $child.closest( '.child' )[0], 'Closest returns current element on match' ); equal( $child.closest( '.parent' ).length, 1, 'Closest returns only one element when original nodelist has one element.' ); var $children = $fixture.find( '.closest .second-child' ).add( $child ); equal( $children.closest( '.parent' ).length, 2, 'Closest returns only two elements when original nodelist has two element.' ); ok( $child.closest( '.parent' ).is( '.first' ), 'Closest returns from the bottom up.' ); ok( $child.closest( '.parent.second' ).is( '.second' ), 'Closest will traverse at least two parents correctly.' ); }); test('`.css()`', function() { var $css = $fixture.find( ".css" ), $otherCss = $fixture.find( ".othercss" ); $css.css({ foo: "bar", baz: "bak", "float": "left", "margin-right": "1px", "transform": "rotateX(0)", "WebkitTransform": "rotateX(0)", "box-sizing": "border-box", "WebkitBoxSizing": "border-box" }); $css.css( 'margin-left', "2px" ); // computed style should ignore spurious styles equal( ss._getStyle($css[0], 'baz'), undefined ); // width is defined in the page equal( ss._getStyle($css[0], 'width'), "200px", "width should show value set from