Last active
December 26, 2015 08:39
-
-
Save zkat/7124197 to your computer and use it in GitHub Desktop.
Example dom comparison utility useful for testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function domEqual(tree, expected, opts) { | |
| function htmlNormalize(str) { | |
| return str.replace(/\s+/gm, " ") | |
| .replace(/>\s/gm, ">") | |
| .replace(/\s</gm, "<"); | |
| } | |
| tree = $(tree); | |
| expected = $(expected); | |
| opts = opts || {}; | |
| let treeHtml = tree[0].outerHTML, | |
| expectedHtml = expected[0].outerHTML; | |
| if (!opts.strict) { | |
| treeHtml = htmlNormalize(treeHtml); | |
| expectedHtml = htmlNormalize(expectedHtml); | |
| } | |
| // Replace this with appropriate comparators to get the best | |
| // results from your testing library on failures. | |
| assert.equal(treeHtml, expectedHtml); | |
| assert.deepEqual(tree.data(), expected.data()); | |
| } | |
| // pass | |
| domEqual("<div>", "<div>"); | |
| domEqual("<div class=foo> hey there\n\n\t </div>", "<div class='foo'>hey there</div>"); | |
| domEqual($("<div>").data("x", 5), $("<div>").data("x", 5)); | |
| domEqual($("<div>").data("x", {foo:1}), $("<div>").data("x", {foo:1})); | |
| // fail | |
| domEqual("<span>", "<div>"); | |
| domEqual("<div class=foo> hey there\n\n\t </div>", "<div class='foo'>hey there</div>", {strict: true}); | |
| domEqual($("<div>").data("x", 5), $("<div>").data("x", 4)); | |
| domEqual($("<div>").data("x", 5), $("<div>")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment