bind.spec.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import minimal from 'minimal'
  2. test('attribute bindings are set on initialize', async () => {
  3. document.body.innerHTML = `
  4. <div x-data="{ foo: 'bar' }">
  5. <span x-bind:foo="foo"></span>
  6. </div>
  7. `
  8. minimal.start()
  9. expect(document.querySelector('span').getAttribute('foo')).toEqual('bar')
  10. })
  11. test('class attribute bindings are removed by object syntax', async () => {
  12. document.body.innerHTML = `
  13. <div x-data="{ isOn: false }">
  14. <span class="foo" x-bind:class="{ 'foo': isOn }"></span>
  15. </div>
  16. `
  17. minimal.start()
  18. expect(document.querySelector('span').classList.contains('foo')).toBeFalsy()
  19. })
  20. test('class attribute bindings are added by object syntax', async () => {
  21. document.body.innerHTML = `
  22. <div x-data="{ isOn: true }">
  23. <span x-bind:class="{ 'foo': isOn }"></span>
  24. </div>
  25. `
  26. minimal.start()
  27. expect(document.querySelector('span').classList.contains('foo')).toBeTruthy()
  28. })
  29. test('class attribute bindings are removed by array syntax', async () => {
  30. document.body.innerHTML = `
  31. <div x-data="{}">
  32. <span class="foo" x-bind:class="[]"></span>
  33. </div>
  34. `
  35. minimal.start()
  36. expect(document.querySelector('span').classList.contains('foo')).toBeFalsy()
  37. })
  38. test('class attribute bindings are added by array syntax', async () => {
  39. document.body.innerHTML = `
  40. <div x-data="{}">
  41. <span class="" x-bind:class="['foo']"></span>
  42. </div>
  43. `
  44. minimal.start()
  45. expect(document.querySelector('span').classList.contains('foo')).toBeTruthy
  46. })
  47. test('boolean attributes set to false are removed from element', async () => {
  48. document.body.innerHTML = `
  49. <div x-data="{ isSet: false }">
  50. <input x-bind:disabled="isSet"></input>
  51. <input x-bind:checked="isSet"></input>
  52. <input x-bind:required="isSet"></input>
  53. <input x-bind:readonly="isSet"></input>
  54. </div>
  55. `
  56. minimal.start()
  57. expect(document.querySelectorAll('input')[0].disabled).toBeFalsy()
  58. expect(document.querySelectorAll('input')[1].checked).toBeFalsy()
  59. expect(document.querySelectorAll('input')[2].required).toBeFalsy()
  60. expect(document.querySelectorAll('input')[3].readOnly).toBeFalsy()
  61. })
  62. test('boolean attributes set to true are added to element', async () => {
  63. document.body.innerHTML = `
  64. <div x-data="{ isSet: true }">
  65. <input x-bind:disabled="isSet"></input>
  66. <input x-bind:checked="isSet"></input>
  67. <input x-bind:required="isSet"></input>
  68. <input x-bind:readonly="isSet"></input>
  69. </div>
  70. `
  71. minimal.start()
  72. expect(document.querySelectorAll('input')[0].disabled).toBeTruthy()
  73. expect(document.querySelectorAll('input')[1].checked).toBeTruthy()
  74. expect(document.querySelectorAll('input')[2].required).toBeTruthy()
  75. expect(document.querySelectorAll('input')[3].readOnly).toBeTruthy()
  76. })