on.spec.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import minimal from 'minimal'
  2. import { wait } from 'dom-testing-library'
  3. test('data modified in event listener updates effected attribute bindings', async () => {
  4. document.body.innerHTML = `
  5. <div x-data="{ foo: 'bar' }">
  6. <button x-on:click="foo = 'baz'"></button>
  7. <span x-bind:foo="foo"></span>
  8. </div>
  9. `
  10. minimal.start()
  11. expect(document.querySelector('span').getAttribute('foo')).toEqual('bar')
  12. document.querySelector('button').click()
  13. await wait(() => { expect(document.querySelector('span').getAttribute('foo')).toEqual('baz') })
  14. })
  15. test('data modified in event listener doesnt update uneffected attribute bindings', async () => {
  16. document.body.innerHTML = `
  17. <div x-data="{ foo: 'bar', count: 0 }">
  18. <button x-on:click="foo = 'baz'"></button>
  19. <button x-on:click="count++"></button>
  20. <span x-bind:output="foo"></span>
  21. <span x-bind:output="count++"></span>
  22. </div>
  23. `
  24. minimal.start()
  25. expect(document.querySelectorAll('span')[0].getAttribute('output')).toEqual('bar')
  26. expect(document.querySelectorAll('span')[1].getAttribute('output')).toEqual('0')
  27. document.querySelectorAll('button')[0].click()
  28. await wait(async () => {
  29. expect(document.querySelectorAll('span')[0].getAttribute('output')).toEqual('baz')
  30. expect(document.querySelectorAll('span')[1].getAttribute('output')).toEqual('0')
  31. document.querySelectorAll('button')[1].click()
  32. await wait(() => {
  33. expect(document.querySelectorAll('span')[0].getAttribute('output')).toEqual('baz')
  34. expect(document.querySelectorAll('span')[1].getAttribute('output')).toEqual('3')
  35. })
  36. })
  37. })
  38. test('click away', async () => {
  39. document.body.innerHTML = `
  40. <div id="outer">
  41. <div x-data="{ isOpen: true }">
  42. <button x-on:click="isOpen = true"></button>
  43. <ul x-bind:value="isOpen" x-on:click.away="isOpen = false">
  44. <li>...</li>
  45. </ul>
  46. </div>
  47. </div>
  48. `
  49. minimal.start()
  50. expect(document.querySelector('ul').getAttribute('value')).toEqual('true')
  51. document.querySelector('li').click()
  52. await wait(() => { expect(document.querySelector('ul').getAttribute('value')).toEqual('true') })
  53. document.querySelector('ul').click()
  54. await wait(() => { expect(document.querySelector('ul').getAttribute('value')).toEqual('true') })
  55. document.querySelector('#outer').click()
  56. await wait(() => { expect(document.querySelector('ul').getAttribute('value')).toEqual('false') })
  57. document.querySelector('button').click()
  58. await wait(() => { expect(document.querySelector('ul').getAttribute('value')).toEqual('true') })
  59. })