Background:
I am using QUnit to validate the creation of DOM elements in the following manner (which has been working fine so far):
$actual = Mylibrary.createSomething();
$expected = $('<div........');
ok($actual[0].isEqualNode($expected[0]),'Node properly created');
More info on isEqualNode: MDN, W3
Issue:
I am having an issue comparing nodes when I use the jQuery's .hide() method because:
- it produces
style="display: none;"(no extra space after;) withFirefox - it produces
style="display: none; "(extra space after;) withChrome isEqualNodeconsiders thatstyle="display: none;"is different thanstyle="display: none; "(you can have attributes in different order andisEqualNodestill considers nodes to be equal, but that extra space not).- when building
$expected, I can only cater for 1 of the 2 situations.
Accordingly my tests end up always failing with Chrome or Firefox depending on which situation I choose to create $expected.
Here is a jsFiddle so that you can see what I mean
I could check if element is hidden with .is(':hidden') but that isn’t ideal to me because:
- I can no longer compare whole set of DOM elements with
.isEqualNodelike I currently do and would have to write many individual tests to get an equivalent which will make my tests very extensive and hard to maintain. - I feel that by using a
DOMmethod (i.e.isEqualNode) my tests are more robusts than if I used ajQuerymethod (e.g..is(':hidden')) because my elements are generated withjQueryin the first place and having ajQuery-independent verification method will more likely spot problems.
Do you see the discrepancies with the way the jQuery‘s .hide() method generates the stype="display attribute as a bug/problem? (i.e. should I raise it on http://bugs.jquery.com/)?
Do you see a solution that would allow me comparing my nodes without using jQuery methods?
From QUnit perspective perform the following check in order to see whether the element is hidden:
This eliminates the need for you to compare string differences. If you are not using jQuery then perform:
If you would like to create some utility extensions to modularize your unit/interaction testing, then isEqualNode is not your best approach here. Better provide yourself with methods like: