entangle.spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { haveValue, html, test } from '../utils'
  2. test.skip('can entangle to getter/setter pairs',
  3. [html`
  4. <div x-data="{ outer: 'foo' }">
  5. <input x-model="outer" outer>
  6. <div x-data="{ inner: 'bar' }" x-init="() => {}; Alpine.entangle(
  7. {
  8. get() { return outer },
  9. set(value) { outer = value },
  10. },
  11. {
  12. get() { return inner },
  13. set(value) { inner = value },
  14. }
  15. )">
  16. <input x-model="inner" inner>
  17. </div>
  18. </div>
  19. `],
  20. ({ get }) => {
  21. get('input[outer]').should(haveValue('foo'))
  22. get('input[inner]').should(haveValue('foo'))
  23. get('input[inner]').type('bar')
  24. get('input[inner]').should(haveValue('foobar'))
  25. get('input[outer]').should(haveValue('foobar'))
  26. get('input[outer]').type('baz')
  27. get('input[outer]').should(haveValue('foobarbaz'))
  28. get('input[inner]').should(haveValue('foobarbaz'))
  29. }
  30. )
  31. test.skip('can release entanglement',
  32. [html`
  33. <div x-data="{ outer: 'foo' }">
  34. <input x-model="outer" outer>
  35. <div x-data="{ inner: 'bar', release: () => {} }" x-init="() => {}; release = Alpine.entangle(
  36. {
  37. get() { return outer },
  38. set(value) { outer = value },
  39. },
  40. {
  41. get() { return inner },
  42. set(value) { inner = value },
  43. }
  44. )">
  45. <input x-model="inner" inner>
  46. <button @click="release()">release</button>
  47. </div>
  48. </div>
  49. `],
  50. ({ get }) => {
  51. get('input[outer]').should(haveValue('foo'))
  52. get('input[inner]').should(haveValue('foo'))
  53. get('input[inner]').type('bar')
  54. get('input[inner]').should(haveValue('foobar'))
  55. get('input[outer]').should(haveValue('foobar'))
  56. get('button').click()
  57. get('input[inner]').type('baz')
  58. get('input[inner]').should(haveValue('foobarbaz'))
  59. get('input[outer]').should(haveValue('foobar'))
  60. }
  61. )
  62. test(
  63. "can handle undefined",
  64. [
  65. html`
  66. <div x-data="{ outer: undefined }">
  67. <input x-model="outer" outer />
  68. <div
  69. x-data="{ inner: 'bar' }"
  70. x-init="() => {}; Alpine.entangle(
  71. {
  72. get() { return outer },
  73. set(value) { outer = value },
  74. },
  75. {
  76. get() { return inner },
  77. set(value) { inner = value },
  78. }
  79. )"
  80. >
  81. <input x-model="inner" inner />
  82. </div>
  83. </div>
  84. `,
  85. ],
  86. ({ get }) => {
  87. get("input[outer]").should(haveValue(''));
  88. get("input[inner]").should(haveValue(''));
  89. get("input[inner]").type("bar");
  90. get("input[inner]").should(haveValue("bar"));
  91. get("input[outer]").should(haveValue("bar"));
  92. }
  93. );