test.spec.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import minimal from 'minimal'
  2. import { wait } from 'dom-testing-library'
  3. test('attribute bindings are set on initialize', async () => {
  4. document.body.innerHTML = `
  5. <div x-data="{ foo: 'bar' }">
  6. <span x-bind:foo="foo"></span>
  7. </div>
  8. `
  9. minimal.start()
  10. expect(document.querySelector('span').getAttribute('foo')).toEqual('bar')
  11. })
  12. test('class attribute bindings are removed by object syntax', async () => {
  13. document.body.innerHTML = `
  14. <div x-data="{ isOn: false }">
  15. <span class="foo" x-bind:class="{ 'foo': isOn }"></span>
  16. </div>
  17. `
  18. minimal.start()
  19. expect(document.querySelector('span').classList.contains('foo')).toBeFalsy
  20. })
  21. test('class attribute bindings are added by object syntax', async () => {
  22. document.body.innerHTML = `
  23. <div x-data="{ isOn: true }">
  24. <span x-bind:class="{ 'foo': isOn }"></span>
  25. </div>
  26. `
  27. minimal.start()
  28. expect(document.querySelector('span').classList.contains('foo')).toBeTruthy
  29. })
  30. test('boolean attributes set to false are removed from element', async () => {
  31. document.body.innerHTML = `
  32. <div x-data="{ isSet: false }">
  33. <input x-bind:disabled="isSet"></input>
  34. <input x-bind:checked="isSet"></input>
  35. <input x-bind:required="isSet"></input>
  36. <input x-bind:readonly="isSet"></input>
  37. </div>
  38. `
  39. minimal.start()
  40. expect(document.querySelectorAll('input')[0].disabled).toBeFalsy()
  41. expect(document.querySelectorAll('input')[1].checked).toBeFalsy()
  42. expect(document.querySelectorAll('input')[2].required).toBeFalsy()
  43. expect(document.querySelectorAll('input')[3].readOnly).toBeFalsy()
  44. })
  45. test('boolean attributes set to true are added to element', async () => {
  46. document.body.innerHTML = `
  47. <div x-data="{ isSet: true }">
  48. <input x-bind:disabled="isSet"></input>
  49. <input x-bind:checked="isSet"></input>
  50. <input x-bind:required="isSet"></input>
  51. <input x-bind:readonly="isSet"></input>
  52. </div>
  53. `
  54. minimal.start()
  55. expect(document.querySelectorAll('input')[0].disabled).toBeTruthy()
  56. expect(document.querySelectorAll('input')[1].checked).toBeTruthy()
  57. expect(document.querySelectorAll('input')[2].required).toBeTruthy()
  58. expect(document.querySelectorAll('input')[3].readOnly).toBeTruthy()
  59. })
  60. test('data modified in event listener updates effected attribute bindings', async () => {
  61. document.body.innerHTML = `
  62. <div x-data="{ foo: 'bar' }">
  63. <button x-on:click="foo = 'baz'"></button>
  64. <span x-bind:foo="foo"></span>
  65. </div>
  66. `
  67. minimal.start()
  68. expect(document.querySelector('span').getAttribute('foo')).toEqual('bar')
  69. document.querySelector('button').click()
  70. await wait(() => { expect(document.querySelector('span').getAttribute('foo')).toEqual('baz') })
  71. })
  72. test('data modified in event listener doesnt update uneffected attribute bindings', async () => {
  73. document.body.innerHTML = `
  74. <div x-data="{ foo: 'bar', count: 0 }">
  75. <button x-on:click="foo = 'baz'"></button>
  76. <button x-on:click="count++"></button>
  77. <span x-bind:value="foo"></span>
  78. <span x-bind:value="count++"></span>
  79. </div>
  80. `
  81. minimal.start()
  82. expect(document.querySelectorAll('span')[0].getAttribute('value')).toEqual('bar')
  83. expect(document.querySelectorAll('span')[1].getAttribute('value')).toEqual('0')
  84. document.querySelectorAll('button')[0].click()
  85. await wait(async () => {
  86. expect(document.querySelectorAll('span')[0].getAttribute('value')).toEqual('baz')
  87. expect(document.querySelectorAll('span')[1].getAttribute('value')).toEqual('0')
  88. document.querySelectorAll('button')[1].click()
  89. await wait(() => {
  90. expect(document.querySelectorAll('span')[0].getAttribute('value')).toEqual('baz')
  91. expect(document.querySelectorAll('span')[1].getAttribute('value')).toEqual('3')
  92. })
  93. })
  94. })
  95. test('click away', async () => {
  96. document.body.innerHTML = `
  97. <div id="outer">
  98. <div x-data="{ isOpen: true }">
  99. <button x-on:click="isOpen = true"></button>
  100. <ul x-bind:value="isOpen" x-on:click.away="isOpen = false">
  101. <li>...</li>
  102. </ul>
  103. </div>
  104. </div>
  105. `
  106. minimal.start()
  107. expect(document.querySelector('ul').getAttribute('value')).toEqual('true')
  108. document.querySelector('li').click()
  109. await wait(() => { expect(document.querySelector('ul').getAttribute('value')).toEqual('true') })
  110. document.querySelector('ul').click()
  111. await wait(() => { expect(document.querySelector('ul').getAttribute('value')).toEqual('true') })
  112. document.querySelector('#outer').click()
  113. await wait(() => { expect(document.querySelector('ul').getAttribute('value')).toEqual('false') })
  114. document.querySelector('button').click()
  115. await wait(() => { expect(document.querySelector('ul').getAttribute('value')).toEqual('true') })
  116. })