legacy.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var DomUtils = require("../..");
  2. var fixture = require("../fixture");
  3. var assert = require("assert");
  4. // Set up expected structures
  5. var expected = {
  6. idAsdf: fixture[1],
  7. tag2: [],
  8. typeScript: []
  9. };
  10. for (var idx = 0; idx < 20; ++idx) {
  11. expected.tag2.push(fixture[idx*2 + 1].children[5]);
  12. expected.typeScript.push(fixture[idx*2 + 1].children[1]);
  13. }
  14. describe("legacy", function() {
  15. describe("getElements", function() {
  16. var getElements = DomUtils.getElements;
  17. it("returns the node with the specified ID", function() {
  18. assert.deepEqual(
  19. getElements({ id: "asdf" }, fixture, true, 1),
  20. [expected.idAsdf]
  21. );
  22. });
  23. it("returns empty array for unknown IDs", function() {
  24. assert.deepEqual(getElements({ id: "asdfs" }, fixture, true), []);
  25. });
  26. it("returns the nodes with the specified tag name", function() {
  27. assert.deepEqual(
  28. getElements({ tag_name:"tag2" }, fixture, true),
  29. expected.tag2
  30. );
  31. });
  32. it("returns empty array for unknown tag names", function() {
  33. assert.deepEqual(
  34. getElements({ tag_name : "asdfs" }, fixture, true),
  35. []
  36. );
  37. });
  38. it("returns the nodes with the specified tag type", function() {
  39. assert.deepEqual(
  40. getElements({ tag_type: "script" }, fixture, true),
  41. expected.typeScript
  42. );
  43. });
  44. it("returns empty array for unknown tag types", function() {
  45. assert.deepEqual(
  46. getElements({ tag_type: "video" }, fixture, true),
  47. []
  48. );
  49. });
  50. });
  51. describe("getElementById", function() {
  52. var getElementById = DomUtils.getElementById;
  53. it("returns the specified node", function() {
  54. assert.equal(
  55. expected.idAsdf,
  56. getElementById("asdf", fixture, true)
  57. );
  58. });
  59. it("returns `null` for unknown IDs", function() {
  60. assert.equal(null, getElementById("asdfs", fixture, true));
  61. });
  62. });
  63. describe("getElementsByTagName", function() {
  64. var getElementsByTagName = DomUtils.getElementsByTagName;
  65. it("returns the specified nodes", function() {
  66. assert.deepEqual(
  67. getElementsByTagName("tag2", fixture, true),
  68. expected.tag2
  69. );
  70. });
  71. it("returns empty array for unknown tag names", function() {
  72. assert.deepEqual(
  73. getElementsByTagName("tag23", fixture, true),
  74. []
  75. );
  76. });
  77. });
  78. describe("getElementsByTagType", function() {
  79. var getElementsByTagType = DomUtils.getElementsByTagType;
  80. it("returns the specified nodes", function() {
  81. assert.deepEqual(
  82. getElementsByTagType("script", fixture, true),
  83. expected.typeScript
  84. );
  85. });
  86. it("returns empty array for unknown tag types", function() {
  87. assert.deepEqual(
  88. getElementsByTagType("video", fixture, true),
  89. []
  90. );
  91. });
  92. });
  93. describe("getOuterHTML", function() {
  94. var getOuterHTML = DomUtils.getOuterHTML;
  95. it("Correctly renders the outer HTML", function() {
  96. assert.equal(
  97. getOuterHTML(fixture[1]),
  98. "<tag1 id=\"asdf\"> <script>text</script> <!-- comment --> <tag2> text </tag2></tag1>"
  99. );
  100. });
  101. });
  102. describe("getInnerHTML", function() {
  103. var getInnerHTML = DomUtils.getInnerHTML;
  104. it("Correctly renders the inner HTML", function() {
  105. assert.equal(
  106. getInnerHTML(fixture[1]),
  107. " <script>text</script> <!-- comment --> <tag2> text </tag2>"
  108. );
  109. });
  110. });
  111. });