attachto.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { assert } from "chai";
  2. import { init, RemoveHook, attachTo, h } from "../../src/index";
  3. const patch = init([]);
  4. describe("attachTo", function () {
  5. let elm: any, vnode0: any;
  6. beforeEach(function () {
  7. elm = document.createElement("div");
  8. vnode0 = elm;
  9. });
  10. it("adds element to target", function () {
  11. const vnode1 = h("div", [
  12. h("div#wrapper", [
  13. h("div", "Some element"),
  14. attachTo(elm, h("div#attached", "Test")),
  15. ]),
  16. ]);
  17. elm = patch(vnode0, vnode1).elm;
  18. assert.strictEqual(elm.children.length, 2);
  19. });
  20. it("updates element at target", function () {
  21. const vnode1 = h("div", [
  22. h("div#wrapper", [
  23. h("div", "Some element"),
  24. attachTo(elm, h("div#attached", "First text")),
  25. ]),
  26. ]);
  27. const vnode2 = h("div", [
  28. h("div#wrapper", [
  29. h("div", "Some element"),
  30. attachTo(elm, h("div#attached", "New text")),
  31. ]),
  32. ]);
  33. elm = patch(vnode0, vnode1).elm;
  34. assert.strictEqual(elm.children[0].innerHTML, "First text");
  35. elm = patch(vnode1, vnode2).elm;
  36. assert.strictEqual(elm.children[0].innerHTML, "New text");
  37. });
  38. it("element can be inserted before modal", function () {
  39. const vnode1 = h("div", [
  40. h("div#wrapper", [
  41. h("div", "Some element"),
  42. attachTo(elm, h("div#attached", "Text")),
  43. ]),
  44. ]);
  45. const vnode2 = h("div", [
  46. h("div#wrapper", [
  47. h("div", "Some element"),
  48. h("div", "A new element"),
  49. attachTo(elm, h("div#attached", "Text")),
  50. ]),
  51. ]);
  52. elm = patch(vnode0, vnode1).elm;
  53. assert.strictEqual(elm.children[0].innerHTML, "Text");
  54. elm = patch(vnode1, vnode2).elm;
  55. assert.strictEqual(elm.children[0].innerHTML, "Text");
  56. });
  57. it("removes element at target", function () {
  58. const vnode1 = h("div", [
  59. h("div#wrapper", [
  60. h("div", "Some element"),
  61. attachTo(elm, h("div#attached", "First text")),
  62. ]),
  63. ]);
  64. const vnode2 = h("div", [h("div#wrapper", [h("div", "Some element")])]);
  65. elm = patch(vnode0, vnode1).elm;
  66. assert.strictEqual(elm.children[0].innerHTML, "First text");
  67. elm = patch(vnode1, vnode2).elm;
  68. assert.strictEqual(elm.children.length, 1);
  69. });
  70. it("remove hook receives real element", function () {
  71. const rm: RemoveHook = (vnode, cb) => {
  72. const elm = vnode.elm as HTMLDivElement;
  73. assert.strictEqual(elm.tagName, "DIV");
  74. assert.strictEqual(elm.innerHTML, "First text");
  75. cb();
  76. };
  77. const vnode1 = h("div", [
  78. h("div#wrapper", [
  79. h("div", "Some element"),
  80. attachTo(
  81. elm,
  82. h("div#attached", { hook: { remove: rm } }, "First text")
  83. ),
  84. ]),
  85. ]);
  86. const vnode2 = h("div", [h("div#wrapper", [h("div", "Some element")])]);
  87. elm = patch(vnode0, vnode1).elm;
  88. elm = patch(vnode1, vnode2).elm;
  89. });
  90. });