You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6 KiB

4 years ago
  1. var makeDom = require("../utils").makeDom;
  2. var helpers = require("../..");
  3. var assert = require("assert");
  4. describe("helpers", function() {
  5. describe("removeSubsets", function() {
  6. var removeSubsets = helpers.removeSubsets;
  7. var dom = makeDom("<div><p><span></span></p><p></p></div>")[0];
  8. it("removes identical trees", function() {
  9. var matches = removeSubsets([dom, dom]);
  10. assert.equal(matches.length, 1);
  11. });
  12. it("Removes subsets found first", function() {
  13. var matches = removeSubsets([dom, dom.children[0].children[0]]);
  14. assert.equal(matches.length, 1);
  15. });
  16. it("Removes subsets found last", function() {
  17. var matches = removeSubsets([dom.children[0], dom]);
  18. assert.equal(matches.length, 1);
  19. });
  20. it("Does not remove unique trees", function() {
  21. var matches = removeSubsets([dom.children[0], dom.children[1]]);
  22. assert.equal(matches.length, 2);
  23. });
  24. });
  25. describe("compareDocumentPosition", function() {
  26. var compareDocumentPosition = helpers.compareDocumentPosition;
  27. var markup = "<div><p><span></span></p><a></a></div>";
  28. var dom = makeDom(markup)[0];
  29. var p = dom.children[0];
  30. var span = p.children[0];
  31. var a = dom.children[1];
  32. it("reports when the first node occurs before the second indirectly", function() {
  33. assert.equal(compareDocumentPosition(span, a), 2);
  34. });
  35. it("reports when the first node contains the second", function() {
  36. assert.equal(compareDocumentPosition(p, span), 10);
  37. });
  38. it("reports when the first node occurs after the second indirectly", function() {
  39. assert.equal(compareDocumentPosition(a, span), 4);
  40. });
  41. it("reports when the first node is contained by the second", function() {
  42. assert.equal(compareDocumentPosition(span, p), 20);
  43. });
  44. it("reports when the nodes belong to separate documents", function() {
  45. var other = makeDom(markup)[0].children[0].children[0];
  46. assert.equal(compareDocumentPosition(span, other), 1);
  47. });
  48. it("reports when the nodes are identical", function() {
  49. assert.equal(compareDocumentPosition(span, span), 0);
  50. });
  51. });
  52. describe("uniqueSort", function() {
  53. var uniqueSort = helpers.uniqueSort;
  54. var dom, p, span, a;
  55. beforeEach(function() {
  56. dom = makeDom("<div><p><span></span></p><a></a></div>")[0];
  57. p = dom.children[0];
  58. span = p.children[0];
  59. a = dom.children[1];
  60. });
  61. it("leaves unique elements untouched", function() {
  62. assert.deepEqual(uniqueSort([p, a]), [p, a]);
  63. });
  64. it("removes duplicate elements", function() {
  65. assert.deepEqual(uniqueSort([p, a, p]), [p, a]);
  66. });
  67. it("sorts nodes in document order", function() {
  68. assert.deepEqual(uniqueSort([a, dom, span, p]), [dom, p, span, a]);
  69. });
  70. });
  71. });