1 var makeDom = require("../utils").makeDom;
2 var helpers = require("../..");
3 var assert = require("assert");
5 describe("helpers", function() {
6 describe("removeSubsets", function() {
7 var removeSubsets = helpers.removeSubsets;
8 var dom = makeDom("<div><p><span></span></p><p></p></div>")[0];
10 it("removes identical trees", function() {
11 var matches = removeSubsets([dom, dom]);
12 assert.equal(matches.length, 1);
15 it("Removes subsets found first", function() {
16 var matches = removeSubsets([dom, dom.children[0].children[0]]);
17 assert.equal(matches.length, 1);
20 it("Removes subsets found last", function() {
21 var matches = removeSubsets([dom.children[0], dom]);
22 assert.equal(matches.length, 1);
25 it("Does not remove unique trees", function() {
26 var matches = removeSubsets([dom.children[0], dom.children[1]]);
27 assert.equal(matches.length, 2);
31 describe("compareDocumentPosition", function() {
32 var compareDocumentPosition = helpers.compareDocumentPosition;
33 var markup = "<div><p><span></span></p><a></a></div>";
34 var dom = makeDom(markup)[0];
35 var p = dom.children[0];
36 var span = p.children[0];
37 var a = dom.children[1];
39 it("reports when the first node occurs before the second indirectly", function() {
40 assert.equal(compareDocumentPosition(span, a), 2);
43 it("reports when the first node contains the second", function() {
44 assert.equal(compareDocumentPosition(p, span), 10);
47 it("reports when the first node occurs after the second indirectly", function() {
48 assert.equal(compareDocumentPosition(a, span), 4);
51 it("reports when the first node is contained by the second", function() {
52 assert.equal(compareDocumentPosition(span, p), 20);
55 it("reports when the nodes belong to separate documents", function() {
56 var other = makeDom(markup)[0].children[0].children[0];
58 assert.equal(compareDocumentPosition(span, other), 1);
61 it("reports when the nodes are identical", function() {
62 assert.equal(compareDocumentPosition(span, span), 0);
66 describe("uniqueSort", function() {
67 var uniqueSort = helpers.uniqueSort;
70 beforeEach(function() {
71 dom = makeDom("<div><p><span></span></p><a></a></div>")[0];
77 it("leaves unique elements untouched", function() {
78 assert.deepEqual(uniqueSort([p, a]), [p, a]);
81 it("removes duplicate elements", function() {
82 assert.deepEqual(uniqueSort([p, a, p]), [p, a]);
85 it("sorts nodes in document order", function() {
86 assert.deepEqual(uniqueSort([a, dom, span, p]), [dom, p, span, a]);