bind.spec.js 3.1 KB

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