$watch.spec.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { haveText, html, test } from '../../utils'
  2. test('$watch',
  3. html`
  4. <div
  5. x-data="{ foo: 'bar', bob: 'lob' }"
  6. x-init="$watch('foo', value => { bob = value })"
  7. >
  8. <h1 x-text="foo"></h1>
  9. <h2 x-text="bob"></h2>
  10. <button x-on:click="foo = 'baz'"></button>
  11. </div>
  12. `,
  13. ({ get }) => {
  14. get('h1').should(haveText('bar'))
  15. get('h2').should(haveText('lob'))
  16. get('button').click()
  17. get('h1').should(haveText('baz'))
  18. get('h2').should(haveText('baz'))
  19. }
  20. )
  21. test('$watch receives old value',
  22. html`
  23. <div
  24. x-data="{ foo: 'bar', fresh: '', old: '' }"
  25. x-init="$watch('foo', (value, oldValue) => { fresh = value; old = oldValue; })"
  26. >
  27. <h1 x-text="fresh"></h1>
  28. <h2 x-text="old"></h2>
  29. <button x-on:click="foo = 'baz'"></button>
  30. </div>
  31. `,
  32. ({ get }) => {
  33. get('button').click()
  34. get('h1').should(haveText('baz'))
  35. get('h2').should(haveText('bar'))
  36. }
  37. )
  38. test('$watch nested properties',
  39. html`
  40. <div x-data="{ foo: { bar: 'baz', bob: 'lob' } }" x-init="
  41. $watch('foo.bar', value => { foo.bob = value });
  42. ">
  43. <h1 x-text="foo.bar"></h1>
  44. <h2 x-text="foo.bob"></h2>
  45. <button x-on:click="foo.bar = 'law'"></button>
  46. </div>
  47. `,
  48. ({ get }) => {
  49. get('h1').should(haveText('baz'))
  50. get('h2').should(haveText('lob'))
  51. get('button').click()
  52. get('h1').should(haveText('law'))
  53. get('h2').should(haveText('law'))
  54. }
  55. )
  56. test('$watch arrays',
  57. html`
  58. <div x-data="{ foo: ['one'], bob: 'lob' }"
  59. x-init="$watch('foo', value => { bob = value })">
  60. <h1 x-text="foo"></h1>
  61. <h2 x-text="bob"></h2>
  62. <button id="push" x-on:click="foo.push('two')"></button>
  63. <button id="pop" x-on:click="foo.pop()"></button>
  64. <button id="unshift" x-on:click="foo.unshift('zero')"></button>
  65. <button id="shift" x-on:click="foo.shift()"></button>
  66. <button id="assign" x-on:click="foo = [2,1,3]"></button>
  67. <button id="sort" x-on:click="foo.sort()"></button>
  68. <button id="reverse" x-on:click="foo.reverse()"></button>
  69. </div>
  70. `,
  71. ({ get }) => {
  72. get('h1').should(haveText('one'))
  73. get('h2').should(haveText('lob'))
  74. get('button#push').click()
  75. get('h1').should(haveText('one,two'))
  76. get('h2').should(haveText('one,two'))
  77. get('button#pop').click()
  78. get('h1').should(haveText('one'))
  79. get('h2').should(haveText('one'))
  80. get('button#unshift').click()
  81. get('h1').should(haveText('zero,one'))
  82. get('h2').should(haveText('zero,one'))
  83. get('button#shift').click()
  84. get('h1').should(haveText('one'))
  85. get('h2').should(haveText('one'))
  86. get('button#assign').click()
  87. get('h1').should(haveText('2,1,3'))
  88. get('h2').should(haveText('2,1,3'))
  89. get('button#sort').click()
  90. get('h1').should(haveText('1,2,3'))
  91. get('h2').should(haveText('1,2,3'))
  92. get('button#reverse').click()
  93. get('h1').should(haveText('3,2,1'))
  94. get('h2').should(haveText('3,2,1'))
  95. }
  96. )
  97. test('$watch nested arrays',
  98. html`
  99. <div x-data="{ foo: {baz: ['one']}, bob: 'lob' }" x-init="$watch('foo.baz', value => { bob = value })">
  100. <h1 x-text="foo.baz"></h1>
  101. <h2 x-text="bob"></h2>
  102. <button id="push" x-on:click="foo.baz.push('two')"></button>
  103. </div>
  104. `,
  105. ({ get }) => {
  106. get('h1').should(haveText('one'))
  107. get('h2').should(haveText('lob'))
  108. get('button').click()
  109. get('h1').should(haveText('one,two'))
  110. get('h2').should(haveText('one,two'))
  111. }
  112. )
  113. test('$watch ignores other dependencies',
  114. html`
  115. <div
  116. x-data="{ a: 0, b: 0, c: 0 }"
  117. x-init="$watch('a', () => { c = a + b })"
  118. >
  119. <button @click="a++" id="a">a</button>
  120. <button @click="b++" id="b">b</button>
  121. <span x-text="c"></span>
  122. </div>
  123. `,
  124. ({ get }) => {
  125. get('span').should(haveText('0'))
  126. get('#a').click()
  127. get('span').should(haveText('1'))
  128. get('#b').click()
  129. get('span').should(haveText('1'))
  130. }
  131. )