listbox.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { beVisible, haveAttribute, haveText, html, notBeVisible, notHaveAttribute, test } from '../../../utils'
  2. test('it works with x-model',
  3. [html`
  4. <div
  5. x-data="{ active: null, people: [
  6. { id: 1, name: 'Wade Cooper' },
  7. { id: 2, name: 'Arlene Mccoy' },
  8. { id: 3, name: 'Devon Webb' },
  9. { id: 4, name: 'Tom Cook' },
  10. { id: 5, name: 'Tanya Fox', disabled: true },
  11. { id: 6, name: 'Hellen Schmidt' },
  12. { id: 7, name: 'Caroline Schultz' },
  13. { id: 8, name: 'Mason Heaney' },
  14. { id: 9, name: 'Claudie Smitham' },
  15. { id: 10, name: 'Emil Schaefer' },
  16. ]}"
  17. x-listbox
  18. x-model="active"
  19. >
  20. <label x-listbox:label>Assigned to</label>
  21. <button x-listbox:button x-text="active ? active.name : 'Select Person'"></button>
  22. <ul x-listbox:options>
  23. <template x-for="person in people" :key="person.id">
  24. <li
  25. :option="person.id"
  26. x-listbox:option
  27. :value="person"
  28. :disabled="person.disabled"
  29. >
  30. <span x-text="person.name"></span>
  31. <!-- <span x-show="$listboxOption.isSelected">
  32. selected
  33. </span> -->
  34. </li>
  35. </template>
  36. </ul>
  37. <article x-text="active?.name"></article>
  38. </div>
  39. `],
  40. ({ get }) => {
  41. get('ul').should(notBeVisible())
  42. get('button').click()
  43. get('ul').should(beVisible())
  44. get('button').click()
  45. get('ul').should(notBeVisible())
  46. get('button').click()
  47. get('[option="2"]').click()
  48. get('ul').should(notBeVisible())
  49. get('article').should(haveText('Arlene Mccoy'))
  50. },
  51. )
  52. test('it works with internal state/$listbox',
  53. [html`
  54. <div
  55. x-data="{ active: null, people: [
  56. { id: 1, name: 'Wade Cooper' },
  57. { id: 2, name: 'Arlene Mccoy' },
  58. { id: 3, name: 'Devon Webb' },
  59. { id: 4, name: 'Tom Cook' },
  60. { id: 5, name: 'Tanya Fox', disabled: true },
  61. { id: 6, name: 'Hellen Schmidt' },
  62. { id: 7, name: 'Caroline Schultz' },
  63. { id: 8, name: 'Mason Heaney' },
  64. { id: 9, name: 'Claudie Smitham' },
  65. { id: 10, name: 'Emil Schaefer' },
  66. ]}"
  67. x-listbox
  68. >
  69. <label x-listbox:label>Assigned to</label>
  70. <button x-listbox:button x-text="active ? active.name : 'Select Person'"></button>
  71. <ul x-listbox:options>
  72. <template x-for="person in people" :key="person.id">
  73. <li
  74. :option="person.id"
  75. x-listbox:option
  76. :value="person"
  77. :disabled="person.disabled"
  78. >
  79. <span x-text="person.name"></span>
  80. <!-- <span x-show="$listboxOption.isSelected">
  81. selected
  82. </span> -->
  83. </li>
  84. </template>
  85. </ul>
  86. <article x-text="$listbox.selected?.name"></article>
  87. </div>
  88. `],
  89. ({ get }) => {
  90. get('ul').should(notBeVisible())
  91. get('button').click()
  92. get('ul').should(beVisible())
  93. get('button').click()
  94. get('ul').should(notBeVisible())
  95. get('button').click()
  96. get('[option="2"]').click()
  97. get('ul').should(notBeVisible())
  98. get('article').should(haveText('Arlene Mccoy'))
  99. },
  100. )