Skip to content

Commit 540b78d

Browse files
committed
Adding a domEqual assertion to our testsuite for more sane DOM
comparisons. Comparing innerHTML is too dependent on random browser quirks like IE only sometimes rendering closing tags.
1 parent 6a5eb35 commit 540b78d

File tree

4 files changed

+42
-29
lines changed

4 files changed

+42
-29
lines changed

tests/unit/accordion/accordion_methods.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,9 @@
33
module( "accordion: methods", accordionSetupTeardown() );
44

55
test( "destroy", function() {
6-
var beforeHtml = $( "#list1" )
7-
.find( "div" )
8-
.css( "font-style", "normal" )
9-
.end()
10-
.parent()
11-
.html();
12-
var afterHtml = $( "#list1" )
13-
.accordion()
14-
.accordion( "destroy" )
15-
.parent()
16-
.html()
17-
// Opera 9 outputs role="" instead of removing the attribute like everyone else
18-
.replace( / role=""/g, "" );
19-
equal( afterHtml, beforeHtml );
6+
domEqual("#list1", function() {
7+
$("#list1").accordion().accordion("destroy");
8+
});
209
});
2110

2211
test( "enable/disable", function() {

tests/unit/autocomplete/autocomplete_methods.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ module("autocomplete: methods", {
1111
});
1212

1313
test("destroy", function() {
14-
var beforeHtml = $("#autocomplete").parent().html();
15-
var afterHtml = $("#autocomplete").autocomplete().autocomplete("destroy").parent().html();
16-
// Opera 9 outputs role="" instead of removing the attribute like everyone else
17-
if ($.browser.opera) {
18-
afterHtml = afterHtml.replace(/ role=""/g, "");
19-
}
20-
equal( afterHtml, beforeHtml, "before/after html should be the same" );
14+
domEqual("#autocomplete", function() {
15+
$("#autocomplete").autocomplete().autocomplete("destroy");
16+
});
2117
})
2218

2319
var data = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl"];

tests/unit/menu/menu_methods.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66
module("menu: methods");
77

88
test("destroy", function() {
9-
var beforeHtml = $("#menu1").find("div").css("font-style", "normal").end().parent().html();
10-
var afterHtml = $("#menu1").menu().menu("destroy").parent().html();
11-
// Opera 9 outputs role="" instead of removing the attribute like everyone else
12-
if ($.browser.opera) {
13-
afterHtml = afterHtml.replace(/ role=""/g, "");
14-
}
15-
equal( afterHtml, beforeHtml );
9+
domEqual("#menu1", function() {
10+
$("#menu1").menu().menu("destroy");
11+
});
1612
});
1713

1814

tests/unit/testsuite.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,38 @@ window.commonWidgetTests = function( widget, settings ) {
6565
test( "version", function() {
6666
ok( "version" in $.ui[ widget ], "version property exists" );
6767
});
68-
};
68+
}
69+
70+
/*
71+
* Experimental assertion for comparing DOM objects.
72+
*
73+
* Serializes an element and some attributes and it's children if any, otherwise the text.
74+
* Then compares the result using deepEqual.
75+
*/
76+
window.domEqual = function( selector, modifier, message ) {
77+
var attributes = ["class", "role", "id", "tabIndex", "aria-activedescendant"];
78+
79+
function extract(value) {
80+
var result = {};
81+
result.nodeName = value[0].nodeName;
82+
$.each(attributes, function(index, attr) {
83+
result[attr] = value.attr(attr);
84+
});
85+
result.children = [];
86+
var children = value.children();
87+
if (children.length) {
88+
children.each(function() {
89+
result.children.push(extract($(this)));
90+
});
91+
} else {
92+
result.text = value.text();
93+
}
94+
return result;
95+
}
96+
var expected = extract($(selector));
97+
modifier($(selector));
98+
99+
deepEqual( extract($(selector)), expected, message );
100+
}
69101

70102
}());

0 commit comments

Comments
 (0)