scope.spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { haveText, html, test } from "../utils";
  2. test(
  3. "properly merges the datastack",
  4. [
  5. html`
  6. <div x-data="{ foo: 'fizz' }">
  7. <div x-data="{ bar: 'buzz' }">
  8. <span x-text="foo + bar"></span>
  9. </div>
  10. </div>
  11. `,
  12. ],
  13. ({ get }) => {
  14. get("span").should(haveText("fizzbuzz"));
  15. }
  16. );
  17. test(
  18. "merges stack from bottom up",
  19. [
  20. html`
  21. <div x-data="{ foo: 'fizz' }">
  22. <div x-data="{ foo: 'buzz', get bar() { return this.foo } }">
  23. <span id="one" x-text="bar + foo"></span>
  24. </div>
  25. <span id="two" x-text="foo"></span>
  26. </div>
  27. `,
  28. ],
  29. ({ get }) => {
  30. get("span#one").should(haveText("buzzbuzz"));
  31. get("span#two").should(haveText("fizz"));
  32. }
  33. );
  34. test(
  35. "handles getter setter pairs",
  36. [
  37. html`
  38. <div x-data="{ foo: 'fizzbuzz' }">
  39. <div
  40. x-data="{ get bar() { return this.foo }, set bar(value) { this.foo = value } }"
  41. >
  42. <span id="one" x-text="bar" @click="bar = 'foobar'"></span>
  43. </div>
  44. <span id="two" x-text="foo"></span>
  45. </div>
  46. `,
  47. ],
  48. ({ get }) => {
  49. get("span#one").should(haveText("fizzbuzz"));
  50. get("span#two").should(haveText("fizzbuzz"));
  51. get("span#one").click();
  52. get("span#one").should(haveText("foobar"));
  53. get("span#two").should(haveText("foobar"));
  54. }
  55. );
  56. test(
  57. "allows accessing class methods",
  58. [
  59. html`
  60. <script>
  61. class Counter {
  62. value = 0;
  63. constructor() {}
  64. increment() {
  65. this.value++;
  66. }
  67. }
  68. document.addEventListener("alpine:init", () =>
  69. Alpine.data("counter", () => new Counter())
  70. );
  71. </script>
  72. <div x-data="counter">
  73. <button
  74. type="button"
  75. @click="increment"
  76. x-text="value"
  77. ></button>
  78. </div>
  79. `,
  80. ],
  81. ({ get }) => {
  82. get("button").should(haveText("0"));
  83. get("button").click();
  84. get("button").should(haveText("1"));
  85. }
  86. );
  87. test(
  88. "setting value doesn't register a dependency",
  89. [
  90. html`
  91. <div x-data="{ message: 'original' }">
  92. <button
  93. x-effect="message = 'effected'"
  94. @click="message = 'clicked'"
  95. x-text="message"
  96. ></button>
  97. </div>
  98. ;
  99. `,
  100. ],
  101. ({ get }) => {
  102. get("button").should(haveText("effected"));
  103. get("button").click();
  104. get("button").should(haveText("clicked"));
  105. }
  106. );