diff options
author | Scott González <scott.gonzalez@gmail.com> | 2015-04-03 15:21:16 -0400 |
---|---|---|
committer | Scott González <scott.gonzalez@gmail.com> | 2015-04-09 09:21:06 -0400 |
commit | 7c896ddb8563e1f4fc655904614cf72d010e2ecb (patch) | |
tree | 160c3d2c1c4aa7e89f38f8371558f3fccd8e8d8a /tests/lib/qunit-assert-domequal.js | |
parent | d0ea32e3ad613eaaa523d0c88c776dab2b26b25f (diff) | |
download | jquery-ui-7c896ddb8563e1f4fc655904614cf72d010e2ecb.tar.gz jquery-ui-7c896ddb8563e1f4fc655904614cf72d010e2ecb.zip |
Tests: Change test infrastructure to use AMD and reduce boilerplate
Ref #10119
Ref gh-1528
* Adds RequireJS and relies on AMD for loading dependencies.
* Updates to grunt-contrib-qunit 0.6.0.
* Convert `domEqual()` to a proper QUnit assertion.
* Introduces two bootstrap files (JS and CSS) which use `data-` attributes to
reduce the amount of boilerplate needed in each test
Diffstat (limited to 'tests/lib/qunit-assert-domequal.js')
-rw-r--r-- | tests/lib/qunit-assert-domequal.js | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/tests/lib/qunit-assert-domequal.js b/tests/lib/qunit-assert-domequal.js new file mode 100644 index 000000000..49c8f6714 --- /dev/null +++ b/tests/lib/qunit-assert-domequal.js @@ -0,0 +1,122 @@ +/* + * Experimental assertion for comparing DOM objects. + * + * Serializes an element and some properties and attributes and its children if any, + * otherwise the text. Then compares the result using deepEqual(). + */ +define( [ + "qunit", + "jquery" +], function( QUnit, $ ) { + +var domEqual = QUnit.assert.domEqual = function( selector, modifier, message ) { + + var assert = this; + + // Get current state prior to modifier + var expected = extract( $( selector ) ); + + function done() { + var actual = extract( $( selector ) ); + assert.push( QUnit.equiv( actual, expected ), actual, expected, message ); + } + + // Run modifier (async or sync), then compare state via done() + if ( modifier.length ) { + modifier( done ); + } else { + modifier(); + done(); + } +}; + +domEqual.properties = [ + "disabled", + "readOnly" +]; + +domEqual.attributes = [ + "autocomplete", + "aria-activedescendant", + "aria-controls", + "aria-describedby", + "aria-disabled", + "aria-expanded", + "aria-haspopup", + "aria-hidden", + "aria-labelledby", + "aria-pressed", + "aria-selected", + "aria-valuemax", + "aria-valuemin", + "aria-valuenow", + "class", + "href", + "id", + "nodeName", + "role", + "tabIndex", + "title" +]; + +function getElementStyles( elem ) { + var styles = {}; + var style = elem.ownerDocument.defaultView ? + elem.ownerDocument.defaultView.getComputedStyle( elem, null ) : + elem.currentStyle; + var key, len; + + if ( style && style.length && style[ 0 ] && style[ style[ 0 ] ] ) { + len = style.length; + while ( len-- ) { + key = style[ len ]; + if ( typeof style[ key ] === "string" ) { + styles[ $.camelCase( key ) ] = style[ key ]; + } + } + + // Support: Opera, IE <9 + } else { + for ( key in style ) { + if ( typeof style[ key ] === "string" ) { + styles[ key ] = style[ key ]; + } + } + } + + return styles; +} + +function extract( elem ) { + if ( !elem || !elem.length ) { + QUnit.push( false, actual, expected, + "domEqual failed, can't extract " + selector + ", message was: " + message ); + return; + } + + var result = {}; + var children; + $.each( domEqual.properties, function( index, attr ) { + var value = elem.prop( attr ); + result[ attr ] = value != null ? value : ""; + }); + $.each( domEqual.attributes, function( index, attr ) { + var value = elem.attr( attr ); + result[ attr ] = value != null ? value : ""; + }); + result.style = getElementStyles( elem[ 0 ] ); + result.events = $._data( elem[ 0 ], "events" ); + result.data = $.extend( {}, elem.data() ); + delete result.data[ $.expando ]; + children = elem.children(); + if ( children.length ) { + result.children = elem.children().map(function() { + return extract( $( this ) ); + }).get(); + } else { + result.text = elem.text(); + } + return result; +} + +} ); |