scope.spec.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. );