combobox.spec.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. import { beVisible, beHidden, haveAttribute, haveClasses, notHaveClasses, haveText, contain, notContain, html, notBeVisible, notHaveAttribute, notExist, haveFocus, test, haveValue, haveLength} from '../../../utils'
  2. test('it works with x-model',
  3. [html`
  4. <div
  5. x-data="{
  6. query: '',
  7. selected: null,
  8. people: [
  9. { id: 1, name: 'Wade Cooper' },
  10. { id: 2, name: 'Arlene Mccoy' },
  11. { id: 3, name: 'Devon Webb' },
  12. { id: 4, name: 'Tom Cook' },
  13. { id: 5, name: 'Tanya Fox', disabled: true },
  14. { id: 6, name: 'Hellen Schmidt' },
  15. { id: 7, name: 'Caroline Schultz' },
  16. { id: 8, name: 'Mason Heaney' },
  17. { id: 9, name: 'Claudie Smitham' },
  18. { id: 10, name: 'Emil Schaefer' },
  19. ],
  20. get filteredPeople() {
  21. return this.query === ''
  22. ? this.people
  23. : this.people.filter((person) => {
  24. return person.name.toLowerCase().includes(this.query.toLowerCase())
  25. })
  26. },
  27. }"
  28. >
  29. <div x-combobox x-model="selected">
  30. <label x-combobox:label>Select person</label>
  31. <div>
  32. <div>
  33. <input
  34. x-combobox:input
  35. :display-value="person => person.name"
  36. @change="query = $event.target.value"
  37. placeholder="Search..."
  38. />
  39. <button x-combobox:button>Toggle</button>
  40. </div>
  41. <div x-combobox:options>
  42. <ul>
  43. <template
  44. x-for="person in filteredPeople"
  45. :key="person.id"
  46. hidden
  47. >
  48. <li
  49. x-combobox:option
  50. :option="person.id"
  51. :value="person"
  52. :disabled="person.disabled"
  53. x-text="person.name"
  54. >
  55. </li>
  56. </template>
  57. </ul>
  58. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  59. </div>
  60. </div>
  61. <article x-text="selected?.name"></article>
  62. </div>
  63. </div>
  64. `],
  65. ({ get }) => {
  66. get('ul').should(notBeVisible())
  67. get('button').click()
  68. get('ul').should(beVisible())
  69. get('button').click()
  70. get('ul').should(notBeVisible())
  71. get('button').click()
  72. get('[option="2"]').click()
  73. get('ul').should(notBeVisible())
  74. get('input').should(haveValue('Arlene Mccoy'))
  75. get('article').should(haveText('Arlene Mccoy'))
  76. get('button').click()
  77. get('ul').should(contain('Wade Cooper'))
  78. .should(contain('Arlene Mccoy'))
  79. .should(contain('Devon Webb'))
  80. get('[option="3"]').click()
  81. get('ul').should(notBeVisible())
  82. get('input').should(haveValue('Devon Webb'))
  83. get('article').should(haveText('Devon Webb'))
  84. get('button').click()
  85. get('ul').should(contain('Wade Cooper'))
  86. .should(contain('Arlene Mccoy'))
  87. .should(contain('Devon Webb'))
  88. get('[option="1"]').click()
  89. get('ul').should(notBeVisible())
  90. get('input').should(haveValue('Wade Cooper'))
  91. get('article').should(haveText('Wade Cooper'))
  92. },
  93. )
  94. test('it works with internal state',
  95. [html`
  96. <div
  97. x-data="{ people: [
  98. { id: 1, name: 'Wade Cooper' },
  99. { id: 2, name: 'Arlene Mccoy' },
  100. { id: 3, name: 'Devon Webb' },
  101. { id: 4, name: 'Tom Cook' },
  102. { id: 5, name: 'Tanya Fox', disabled: true },
  103. { id: 6, name: 'Hellen Schmidt' },
  104. { id: 7, name: 'Caroline Schultz' },
  105. { id: 8, name: 'Mason Heaney' },
  106. { id: 9, name: 'Claudie Smitham' },
  107. { id: 10, name: 'Emil Schaefer' },
  108. ]}"
  109. x-combobox
  110. >
  111. <label x-combobox:label>Assigned to</label>
  112. <input x-combobox:input :display-value="(person) => person.name" type="text">
  113. <button x-combobox:button x-text="$combobox.value ? $combobox.value.name : 'Select Person'"></button>
  114. <ul x-combobox:options>
  115. <template x-for="person in people" :key="person.id">
  116. <li
  117. :option="person.id"
  118. x-combobox:option
  119. :value="person"
  120. :disabled="person.disabled"
  121. >
  122. <span x-text="person.name"></span>
  123. </li>
  124. </template>
  125. </ul>
  126. </div>
  127. `],
  128. ({ get }) => {
  129. get('ul').should(notBeVisible())
  130. get('button')
  131. .should(haveText('Select Person'))
  132. .click()
  133. get('ul').should(beVisible())
  134. get('button').click()
  135. get('ul').should(notBeVisible())
  136. get('button').click()
  137. get('[option="2"]').click()
  138. get('ul').should(notBeVisible())
  139. get('button').should(haveText('Arlene Mccoy'))
  140. get('input').should(haveValue('Arlene Mccoy'))
  141. },
  142. )
  143. test('$combobox/$comboboxOption',
  144. [html`
  145. <div
  146. x-data="{ people: [
  147. { id: 1, name: 'Wade Cooper' },
  148. { id: 2, name: 'Arlene Mccoy' },
  149. { id: 3, name: 'Devon Webb' },
  150. { id: 4, name: 'Tom Cook' },
  151. { id: 5, name: 'Tanya Fox', disabled: true },
  152. { id: 6, name: 'Hellen Schmidt' },
  153. { id: 7, name: 'Caroline Schultz' },
  154. { id: 8, name: 'Mason Heaney' },
  155. { id: 9, name: 'Claudie Smitham' },
  156. { id: 10, name: 'Emil Schaefer' },
  157. ]}"
  158. x-combobox
  159. >
  160. <label x-combobox:label>Assigned to</label>
  161. <input x-combobox:input :display-value="(person) => person.name" type="text">
  162. <button x-combobox:button x-text="$combobox.value ? $combobox.value.name : 'Select Person'"></button>
  163. <p x-text="$combobox.activeIndex"></p>
  164. <article x-text="$combobox.activeOption?.name"></article>
  165. <ul x-combobox:options>
  166. <template x-for="person in people" :key="person.id">
  167. <li
  168. :option="person.id"
  169. x-combobox:option
  170. :value="person"
  171. :disabled="person.disabled"
  172. :class="{
  173. 'selected': $comboboxOption.isSelected,
  174. 'active': $comboboxOption.isActive,
  175. 'disabled': $comboboxOption.isDisabled,
  176. }"
  177. >
  178. <span x-text="person.name"></span>
  179. </li>
  180. </template>
  181. </ul>
  182. </div>
  183. `],
  184. ({ get }) => {
  185. get('article').should(haveText(''))
  186. get('[option="5"]').should(haveClasses(['disabled']))
  187. get('button')
  188. .should(haveText('Select Person'))
  189. .click()
  190. get('[option="1"]').should(haveClasses(['active']))
  191. get('input').type('{downarrow}')
  192. get('article').should(haveText('Arlene Mccoy'))
  193. get('p').should(haveText('1'))
  194. get('[option="2"]').should(haveClasses(['active']))
  195. get('button').should(haveText('Select Person'))
  196. get('[option="2"]').click()
  197. get('button').should(haveText('Arlene Mccoy'))
  198. get('[option="2"]').should(haveClasses(['selected']))
  199. },
  200. )
  201. test('"name" prop',
  202. [html`
  203. <div
  204. x-data="{ people: [
  205. { id: 1, name: 'Wade Cooper' },
  206. { id: 2, name: 'Arlene Mccoy' },
  207. { id: 3, name: 'Devon Webb' },
  208. { id: 4, name: 'Tom Cook' },
  209. { id: 5, name: 'Tanya Fox', disabled: true },
  210. { id: 6, name: 'Hellen Schmidt' },
  211. { id: 7, name: 'Caroline Schultz' },
  212. { id: 8, name: 'Mason Heaney' },
  213. { id: 9, name: 'Claudie Smitham' },
  214. { id: 10, name: 'Emil Schaefer' },
  215. ]}"
  216. x-combobox
  217. name="person"
  218. >
  219. <label x-combobox:label>Assigned to</label>
  220. <input x-combobox:input :display-value="(person) => person.name" type="text">
  221. <button x-combobox:button x-text="$combobox.value ? $combobox.value : 'Select Person'"></button>
  222. <ul x-combobox:options>
  223. <template x-for="person in people" :key="person.id">
  224. <li
  225. :option="person.id"
  226. x-combobox:option
  227. :value="person.id"
  228. :disabled="person.disabled"
  229. :class="{
  230. 'selected': $comboboxOption.isSelected,
  231. 'active': $comboboxOption.isActive,
  232. }"
  233. >
  234. <span x-text="person.name"></span>
  235. </li>
  236. </template>
  237. </ul>
  238. </div>
  239. `],
  240. ({ get }) => {
  241. get('input').should(haveAttribute('value', 'null'))
  242. get('button').click()
  243. get('input').should(haveAttribute('value', 'null'))
  244. get('[option="2"]').click()
  245. get('input').should(beHidden())
  246. .should(haveAttribute('name', 'person'))
  247. .should(haveAttribute('value', '2'))
  248. .should(haveAttribute('type', 'hidden'))
  249. get('button').click()
  250. get('[option="4"]').click()
  251. get('input').should(beHidden())
  252. .should(haveAttribute('name', 'person'))
  253. .should(haveAttribute('value', '4'))
  254. .should(haveAttribute('type', 'hidden'))
  255. },
  256. );
  257. test('"name" prop with object value',
  258. [html`
  259. <div
  260. x-data="{ people: [
  261. { id: 1, name: 'Wade Cooper' },
  262. { id: 2, name: 'Arlene Mccoy' },
  263. { id: 3, name: 'Devon Webb' },
  264. { id: 4, name: 'Tom Cook' },
  265. { id: 5, name: 'Tanya Fox', disabled: true },
  266. { id: 6, name: 'Hellen Schmidt' },
  267. { id: 7, name: 'Caroline Schultz' },
  268. { id: 8, name: 'Mason Heaney' },
  269. { id: 9, name: 'Claudie Smitham' },
  270. { id: 10, name: 'Emil Schaefer' },
  271. ]}"
  272. x-combobox
  273. name="person"
  274. >
  275. <label x-combobox:label>Assigned to</label>
  276. <input x-combobox:input :display-value="(person) => person.name" type="text">
  277. <button x-combobox:button x-text="$combobox.value ? $combobox.value.name : 'Select Person'"></button>
  278. <ul x-combobox:options>
  279. <template x-for="person in people" :key="person.id">
  280. <li
  281. :option="person.id"
  282. x-combobox:option
  283. :value="person"
  284. :disabled="person.disabled"
  285. :class="{
  286. 'selected': $comboboxOption.isSelected,
  287. 'active': $comboboxOption.isActive,
  288. }"
  289. >
  290. <span x-text="person.name"></span>
  291. </li>
  292. </template>
  293. </ul>
  294. </div>
  295. `],
  296. ({ get }) => {
  297. get('input[name="person"]').should(haveAttribute('value', 'null'))
  298. get('button').click()
  299. get('[name="person[id]"]').should(notExist())
  300. get('[option="2"]').click()
  301. get('input[name="person"]').should(notExist())
  302. get('[name="person[id]"]').should(beHidden())
  303. .should(haveAttribute('value', '2'))
  304. .should(haveAttribute('type', 'hidden'))
  305. get('[name="person[name]"]').should(beHidden())
  306. .should(haveAttribute('value', 'Arlene Mccoy'))
  307. .should(haveAttribute('type', 'hidden'))
  308. get('button').click()
  309. get('[option="4"]').click()
  310. get('[name="person[id]"]').should(beHidden())
  311. .should(haveAttribute('value', '4'))
  312. .should(haveAttribute('type', 'hidden'))
  313. get('[name="person[name]"]').should(beHidden())
  314. .should(haveAttribute('value', 'Tom Cook'))
  315. .should(haveAttribute('type', 'hidden'))
  316. },
  317. );
  318. test('"default-value" prop',
  319. [html`
  320. <div
  321. x-data="{ people: [
  322. { id: 1, name: 'Wade Cooper' },
  323. { id: 2, name: 'Arlene Mccoy' },
  324. { id: 3, name: 'Devon Webb' },
  325. { id: 4, name: 'Tom Cook' },
  326. { id: 5, name: 'Tanya Fox', disabled: true },
  327. { id: 6, name: 'Hellen Schmidt' },
  328. { id: 7, name: 'Caroline Schultz' },
  329. { id: 8, name: 'Mason Heaney' },
  330. { id: 9, name: 'Claudie Smitham' },
  331. { id: 10, name: 'Emil Schaefer' },
  332. ]}"
  333. x-combobox
  334. name="person"
  335. default-value="2"
  336. >
  337. <label x-combobox:label>Assigned to</label>
  338. <input x-combobox:input :display-value="(person) => person.name" type="text">
  339. <button x-combobox:button x-text="$combobox.value ? $combobox.value : 'Select Person'"></button>
  340. <ul x-combobox:options>
  341. <template x-for="person in people" :key="person.id">
  342. <li
  343. :option="person.id"
  344. x-combobox:option
  345. :value="person.id"
  346. :disabled="person.disabled"
  347. :class="{
  348. 'selected': $comboboxOption.isSelected,
  349. 'active': $comboboxOption.isActive,
  350. }"
  351. >
  352. <span x-text="person.name"></span>
  353. </li>
  354. </template>
  355. </ul>
  356. </div>
  357. `],
  358. ({ get }) => {
  359. get('input[name="person"]').should(beHidden())
  360. .should(haveAttribute('value', '2'))
  361. .should(haveAttribute('type', 'hidden'))
  362. },
  363. );
  364. test('"multiple" prop',
  365. [html`
  366. <div
  367. x-data="{
  368. people: [
  369. { id: 1, name: 'Wade Cooper' },
  370. { id: 2, name: 'Arlene Mccoy' },
  371. { id: 3, name: 'Devon Webb' },
  372. { id: 4, name: 'Tom Cook' },
  373. { id: 5, name: 'Tanya Fox', disabled: true },
  374. { id: 6, name: 'Hellen Schmidt' },
  375. { id: 7, name: 'Caroline Schultz' },
  376. { id: 8, name: 'Mason Heaney' },
  377. { id: 9, name: 'Claudie Smitham' },
  378. { id: 10, name: 'Emil Schaefer' },
  379. ]
  380. }"
  381. x-combobox
  382. multiple
  383. >
  384. <label x-combobox:label>Assigned to</label>
  385. <input x-combobox:input :display-value="(person) => person.name" type="text">
  386. <button x-combobox:button x-text="$combobox.value ? $combobox.value.join(',') : 'Select People'"></button>
  387. <ul x-combobox:options>
  388. <template x-for="person in people" :key="person.id">
  389. <li
  390. :option="person.id"
  391. x-combobox:option
  392. :value="person.id"
  393. :disabled="person.disabled"
  394. :class="{
  395. 'selected': $comboboxOption.isSelected,
  396. 'active': $comboboxOption.isActive,
  397. }"
  398. >
  399. <span x-text="person.name"></span>
  400. </li>
  401. </template>
  402. </ul>
  403. </div>
  404. `],
  405. ({ get }) => {
  406. get('button').click()
  407. get('[option="2"]').click()
  408. get('ul').should(beVisible())
  409. get('button').should(haveText('2'))
  410. get('[option="4"]').click()
  411. get('button').should(haveText('2,4'))
  412. get('ul').should(beVisible())
  413. get('[option="4"]').click()
  414. get('button').should(haveText('2'))
  415. get('ul').should(beVisible())
  416. get('input').type('Tom')
  417. get('input').type('{enter}')
  418. get('button').should(haveText('2,4'))
  419. // input field doesn't reset when a new option is selected
  420. get('input').should(haveValue('Tom'))
  421. },
  422. );
  423. test('"multiple" and "name" props together',
  424. [html`
  425. <div
  426. x-data="{
  427. people: [
  428. { id: 1, name: 'Wade Cooper' },
  429. { id: 2, name: 'Arlene Mccoy' },
  430. { id: 3, name: 'Devon Webb' },
  431. { id: 4, name: 'Tom Cook' },
  432. { id: 5, name: 'Tanya Fox', disabled: true },
  433. { id: 6, name: 'Hellen Schmidt' },
  434. { id: 7, name: 'Caroline Schultz' },
  435. { id: 8, name: 'Mason Heaney' },
  436. { id: 9, name: 'Claudie Smitham' },
  437. { id: 10, name: 'Emil Schaefer' },
  438. ]
  439. }"
  440. x-combobox
  441. multiple
  442. name="people"
  443. >
  444. <label x-combobox:label>Assigned to</label>
  445. <input x-combobox:input :display-value="(person) => person.name" type="text">
  446. <button x-combobox:button x-text="$combobox.value ? $combobox.value.map(p => p.id).join(',') : 'Select People'"></button>
  447. <ul x-combobox:options>
  448. <template x-for="person in people" :key="person.id">
  449. <li
  450. :option="person.id"
  451. x-combobox:option
  452. :value="person"
  453. :disabled="person.disabled"
  454. :class="{
  455. 'selected': $comboboxOption.isSelected,
  456. 'active': $comboboxOption.isActive,
  457. }"
  458. >
  459. <span x-text="person.name"></span>
  460. </li>
  461. </template>
  462. </ul>
  463. </div>
  464. `],
  465. ({ get }) => {
  466. // get('input[name="people"]').should(haveAttribute('value', 'null'))
  467. get('button').click()
  468. get('[name="people[0][id]"]').should(notExist())
  469. get('[option="2"]').click()
  470. get('ul').should(beVisible())
  471. get('button').should(haveText('2'))
  472. get('input[name="people"]').should(notExist())
  473. get('[name="people[0][id]"]').should(beHidden())
  474. .should(haveAttribute('value', '2'))
  475. .should(haveAttribute('type', 'hidden'))
  476. get('[name="people[0][name]"]').should(beHidden())
  477. .should(haveAttribute('value', 'Arlene Mccoy'))
  478. .should(haveAttribute('type', 'hidden'))
  479. get('[option="4"]').click()
  480. get('[name="people[0][id]"]').should(beHidden())
  481. .should(haveAttribute('value', '2'))
  482. .should(haveAttribute('type', 'hidden'))
  483. get('[name="people[0][name]"]').should(beHidden())
  484. .should(haveAttribute('value', 'Arlene Mccoy'))
  485. .should(haveAttribute('type', 'hidden'))
  486. get('[name="people[1][id]"]').should(beHidden())
  487. .should(haveAttribute('value', '4'))
  488. .should(haveAttribute('type', 'hidden'))
  489. get('[name="people[1][name]"]').should(beHidden())
  490. .should(haveAttribute('value', 'Tom Cook'))
  491. .should(haveAttribute('type', 'hidden'))
  492. get('button').should(haveText('2,4'))
  493. get('ul').should(beVisible())
  494. get('[option="4"]').click()
  495. get('[name="people[0][id]"]').should(beHidden())
  496. .should(haveAttribute('value', '2'))
  497. .should(haveAttribute('type', 'hidden'))
  498. get('[name="people[0][name]"]').should(beHidden())
  499. .should(haveAttribute('value', 'Arlene Mccoy'))
  500. .should(haveAttribute('type', 'hidden'))
  501. get('[name="people[1][id]"]').should(notExist())
  502. get('[name="people[1][name]"]').should(notExist())
  503. get('button').should(haveText('2'))
  504. get('ul').should(beVisible())
  505. },
  506. );
  507. test('keyboard controls',
  508. [html`
  509. <div
  510. x-data="{ active: null, people: [
  511. { id: 1, name: 'Wade Cooper' },
  512. { id: 2, name: 'Arlene Mccoy' },
  513. { id: 3, name: 'Devon Webb', disabled: true },
  514. { id: 4, name: 'Tom Cook' },
  515. { id: 5, name: 'Tanya Fox', disabled: true },
  516. { id: 6, name: 'Hellen Schmidt' },
  517. { id: 7, name: 'Caroline Schultz' },
  518. { id: 8, name: 'Mason Heaney' },
  519. { id: 9, name: 'Claudie Smitham' },
  520. { id: 10, name: 'Emil Schaefer' },
  521. ]}"
  522. x-combobox
  523. x-model="active"
  524. >
  525. <label x-combobox:label>Assigned to</label>
  526. <input x-combobox:input :display-value="(person) => person.name" type="text">
  527. <button x-combobox:button x-text="active ? active.name : 'Select Person'"></button>
  528. <ul x-combobox:options options>
  529. <template x-for="person in people" :key="person.id">
  530. <li
  531. :option="person.id"
  532. x-combobox:option
  533. :value="person"
  534. :disabled="person.disabled"
  535. :class="{
  536. 'selected': $comboboxOption.isSelected,
  537. 'active': $comboboxOption.isActive,
  538. }"
  539. >
  540. <span x-text="person.name"></span>
  541. </li>
  542. </template>
  543. </ul>
  544. </div>
  545. `],
  546. ({ get }) => {
  547. get('.active').should(notExist())
  548. get('button').click()
  549. get('[options]')
  550. .should(beVisible())
  551. get('input').should(haveFocus())
  552. get('[option="1"]')
  553. .should(haveClasses(['active']))
  554. get('input')
  555. .type('{downarrow}')
  556. get('[option="2"]')
  557. .should(haveClasses(['active']))
  558. get('input')
  559. .type('{downarrow}')
  560. get('[option="4"]')
  561. .should(haveClasses(['active']))
  562. get('input')
  563. .type('{uparrow}')
  564. get('[option="2"]')
  565. .should(haveClasses(['active']))
  566. get('input')
  567. .type('{home}')
  568. get('[option="1"]')
  569. .should(haveClasses(['active']))
  570. get('input')
  571. .type('{end}')
  572. get('[option="10"]')
  573. .should(haveClasses(['active']))
  574. get('input')
  575. .type('{pageUp}')
  576. get('[option="1"]')
  577. .should(haveClasses(['active']))
  578. get('input')
  579. .type('{pageDown}')
  580. get('[option="10"]')
  581. .should(haveClasses(['active']))
  582. get('input')
  583. .tab()
  584. .should(haveFocus())
  585. get('[options]')
  586. .should(beVisible())
  587. get('input')
  588. .type('{esc}')
  589. get('[options]')
  590. .should(notBeVisible())
  591. },
  592. )
  593. test('changing value manually changes internal state',
  594. [html`
  595. <div
  596. x-data="{ active: null, people: [
  597. { id: 1, name: 'Wade Cooper' },
  598. { id: 2, name: 'Arlene Mccoy' },
  599. { id: 3, name: 'Devon Webb', disabled: true },
  600. { id: 4, name: 'Tom Cook' },
  601. { id: 5, name: 'Tanya Fox', disabled: true },
  602. { id: 6, name: 'Hellen Schmidt' },
  603. { id: 7, name: 'Caroline Schultz' },
  604. { id: 8, name: 'Mason Heaney' },
  605. { id: 9, name: 'Claudie Smitham' },
  606. { id: 10, name: 'Emil Schaefer' },
  607. ]}"
  608. x-combobox
  609. x-model="active"
  610. >
  611. <label x-combobox:label>Assigned to</label>
  612. <input x-combobox:input :display-value="(person) => person.name" type="text">
  613. <button toggle x-combobox:button x-text="$combobox.value ? $combobox.value : 'Select Person'"></button>
  614. <button select-tim @click="active = 4">Select Tim</button>
  615. <ul x-combobox:options options>
  616. <template x-for="person in people" :key="person.id">
  617. <li
  618. :option="person.id"
  619. x-combobox:option
  620. :value="person.id"
  621. :disabled="person.disabled"
  622. :class="{
  623. 'selected': $comboboxOption.isSelected,
  624. 'active': $comboboxOption.isActive,
  625. }"
  626. >
  627. <span x-text="person.name"></span>
  628. </li>
  629. </template>
  630. </ul>
  631. </div>
  632. `],
  633. ({ get }) => {
  634. get('[select-tim]').click()
  635. get('[option="4"]').should(haveClasses(['selected']))
  636. get('[option="1"]').should(notHaveClasses(['selected']))
  637. get('[toggle]').should(haveText('4'))
  638. },
  639. )
  640. test('has accessibility attributes',
  641. [html`
  642. <div
  643. x-data="{ active: null, people: [
  644. { id: 1, name: 'Wade Cooper' },
  645. { id: 2, name: 'Arlene Mccoy' },
  646. { id: 3, name: 'Devon Webb', disabled: true },
  647. { id: 4, name: 'Tom Cook' },
  648. { id: 5, name: 'Tanya Fox', disabled: true },
  649. { id: 6, name: 'Hellen Schmidt' },
  650. { id: 7, name: 'Caroline Schultz' },
  651. { id: 8, name: 'Mason Heaney' },
  652. { id: 9, name: 'Claudie Smitham' },
  653. { id: 10, name: 'Emil Schaefer' },
  654. ]}"
  655. x-combobox
  656. x-model="active"
  657. >
  658. <label x-combobox:label>Assigned to</label>
  659. <input x-combobox:input :display-value="(person) => person.name" type="text">
  660. <button x-combobox:button x-text="active ? active.name : 'Select Person'"></button>
  661. <ul x-combobox:options options>
  662. <template x-for="person in people" :key="person.id">
  663. <li
  664. :option="person.id"
  665. x-combobox:option
  666. :value="person"
  667. :disabled="person.disabled"
  668. :class="{
  669. 'selected': $comboboxOption.isSelected,
  670. 'active': $comboboxOption.isActive,
  671. }"
  672. >
  673. <span x-text="person.name"></span>
  674. </li>
  675. </template>
  676. </ul>
  677. </div>
  678. `],
  679. ({ get }) => {
  680. get('input')
  681. .should(haveAttribute('aria-expanded', 'false'))
  682. get('button')
  683. .should(haveAttribute('aria-haspopup', 'true'))
  684. .should(haveAttribute('aria-labelledby', 'alpine-combobox-label-1 alpine-combobox-button-1'))
  685. .should(haveAttribute('aria-expanded', 'false'))
  686. .should(notHaveAttribute('aria-controls'))
  687. .should(haveAttribute('id', 'alpine-combobox-button-1'))
  688. .should(haveAttribute('tabindex', '-1'))
  689. .click()
  690. .should(haveAttribute('aria-expanded', 'true'))
  691. .should(haveAttribute('aria-controls', 'alpine-combobox-options-1'))
  692. get('[options]')
  693. .should(haveAttribute('role', 'combobox'))
  694. .should(haveAttribute('id', 'alpine-combobox-options-1'))
  695. .should(haveAttribute('aria-labelledby', 'alpine-combobox-label-1'))
  696. get('[option="1"]')
  697. .should(haveAttribute('role', 'option'))
  698. .should(haveAttribute('id', 'alpine-combobox-option-1'))
  699. .should(haveAttribute('tabindex', '-1'))
  700. .should(haveAttribute('aria-selected', 'true'))
  701. get('[option="2"]')
  702. .should(haveAttribute('role', 'option'))
  703. .should(haveAttribute('id', 'alpine-combobox-option-2'))
  704. .should(haveAttribute('tabindex', '-1'))
  705. .should(haveAttribute('aria-selected', 'false'))
  706. get('input')
  707. .should(haveAttribute('role', 'combobox'))
  708. .should(haveAttribute('aria-autocomplete', 'list'))
  709. .should(haveAttribute('tabindex', '0'))
  710. .should(haveAttribute('aria-expanded', 'true'))
  711. .should(haveAttribute('aria-labelledby', 'alpine-combobox-label-1'))
  712. .should(haveAttribute('aria-controls', 'alpine-combobox-options-1'))
  713. .should(haveAttribute('aria-activedescendant', 'alpine-combobox-option-1'))
  714. .type('{downarrow}')
  715. .should(haveAttribute('aria-activedescendant', 'alpine-combobox-option-2'))
  716. get('[option="2"]')
  717. .should(haveAttribute('aria-selected', 'true'))
  718. },
  719. )
  720. test('"static" prop',
  721. [html`
  722. <div
  723. x-data="{ active: null, show: false, people: [
  724. { id: 1, name: 'Wade Cooper' },
  725. { id: 2, name: 'Arlene Mccoy' },
  726. { id: 3, name: 'Devon Webb' },
  727. { id: 4, name: 'Tom Cook' },
  728. { id: 5, name: 'Tanya Fox', disabled: true },
  729. { id: 6, name: 'Hellen Schmidt' },
  730. { id: 7, name: 'Caroline Schultz' },
  731. { id: 8, name: 'Mason Heaney' },
  732. { id: 9, name: 'Claudie Smitham' },
  733. { id: 10, name: 'Emil Schaefer' },
  734. ]}"
  735. x-combobox
  736. x-model="active"
  737. >
  738. <label x-combobox:label>Assigned to</label>
  739. <input x-combobox:input :display-value="(person) => person.name" type="text">
  740. <button normal-toggle x-combobox:button x-text="active ? active.name : 'Select Person'"></button>
  741. <button real-toggle @click="show = ! show">Toggle</button>
  742. <ul x-combobox:options x-show="show" static>
  743. <template x-for="person in people" :key="person.id">
  744. <li
  745. :option="person.id"
  746. x-combobox:option
  747. :value="person"
  748. :disabled="person.disabled"
  749. >
  750. <span x-text="person.name"></span>
  751. </li>
  752. </template>
  753. </ul>
  754. </div>
  755. `],
  756. ({ get }) => {
  757. get('ul').should(notBeVisible())
  758. get('[normal-toggle]')
  759. .should(haveText('Select Person'))
  760. .click()
  761. get('ul').should(notBeVisible())
  762. get('[real-toggle]').click()
  763. get('ul').should(beVisible())
  764. get('[option="2"]').click()
  765. get('ul').should(beVisible())
  766. get('[normal-toggle]').should(haveText('Arlene Mccoy'))
  767. },
  768. )
  769. test('input reset',
  770. [html`
  771. <div
  772. x-data="{
  773. query: '',
  774. selected: null,
  775. people: [
  776. { id: 1, name: 'Wade Cooper' },
  777. { id: 2, name: 'Arlene Mccoy' },
  778. { id: 3, name: 'Devon Webb' },
  779. { id: 4, name: 'Tom Cook' },
  780. { id: 5, name: 'Tanya Fox', disabled: true },
  781. { id: 6, name: 'Hellen Schmidt' },
  782. { id: 7, name: 'Caroline Schultz' },
  783. { id: 8, name: 'Mason Heaney' },
  784. { id: 9, name: 'Claudie Smitham' },
  785. { id: 10, name: 'Emil Schaefer' },
  786. ],
  787. get filteredPeople() {
  788. return this.query === ''
  789. ? this.people
  790. : this.people.filter((person) => {
  791. return person.name.toLowerCase().includes(this.query.toLowerCase())
  792. })
  793. },
  794. }"
  795. >
  796. <div x-combobox x-model="selected">
  797. <label x-combobox:label>Select person</label>
  798. <div>
  799. <div>
  800. <input
  801. x-combobox:input
  802. :display-value="person => person.name"
  803. @change="query = $event.target.value"
  804. placeholder="Search..."
  805. />
  806. <button x-combobox:button>Toggle</button>
  807. </div>
  808. <div x-combobox:options>
  809. <ul>
  810. <template
  811. x-for="person in filteredPeople"
  812. :key="person.id"
  813. hidden
  814. >
  815. <li
  816. x-combobox:option
  817. :option="person.id"
  818. :value="person"
  819. :disabled="person.disabled"
  820. x-text="person.name"
  821. >
  822. </li>
  823. </template>
  824. </ul>
  825. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  826. </div>
  827. </div>
  828. </div>
  829. <article>lorem ipsum</article>
  830. </div>
  831. `],
  832. ({ get }) => {
  833. // Test after closing with button
  834. get('button').click()
  835. get('input').type('w')
  836. get('button').click()
  837. get('input').should(haveValue(''))
  838. // Test correct state after closing with ESC
  839. get('button').click()
  840. get('input').type('w')
  841. get('input').type('{esc}')
  842. get('input').should(haveValue(''))
  843. // Test correct state after closing with TAB
  844. get('button').click()
  845. get('input').type('w')
  846. get('input').tab()
  847. get('input').should(haveValue(''))
  848. // Test correct state after closing with external click
  849. get('button').click()
  850. get('input').type('w')
  851. get('article').click()
  852. get('input').should(haveValue(''))
  853. // Select something
  854. get('button').click()
  855. get('ul').should(beVisible())
  856. get('[option="2"]').click()
  857. get('input').should(haveValue('Arlene Mccoy'))
  858. // Test after closing with button
  859. get('button').click()
  860. get('input').type('w')
  861. get('button').click()
  862. get('input').should(haveValue('Arlene Mccoy'))
  863. // Test correct state after closing with ESC and reopening
  864. get('button').click()
  865. get('input').type('w')
  866. get('input').type('{esc}')
  867. get('input').should(haveValue('Arlene Mccoy'))
  868. // Test correct state after closing with TAB and reopening
  869. get('button').click()
  870. get('input').type('w')
  871. get('input').tab()
  872. get('input').should(haveValue('Arlene Mccoy'))
  873. // Test correct state after closing with external click and reopening
  874. get('button').click()
  875. get('input').type('w')
  876. get('article').click()
  877. get('input').should(haveValue('Arlene Mccoy'))
  878. },
  879. )
  880. test('combobox shows all options when opening',
  881. [html`
  882. <div
  883. x-data="{
  884. query: '',
  885. selected: null,
  886. people: [
  887. { id: 1, name: 'Wade Cooper' },
  888. { id: 2, name: 'Arlene Mccoy' },
  889. { id: 3, name: 'Devon Webb' },
  890. { id: 4, name: 'Tom Cook' },
  891. { id: 5, name: 'Tanya Fox', disabled: true },
  892. { id: 6, name: 'Hellen Schmidt' },
  893. { id: 7, name: 'Caroline Schultz' },
  894. { id: 8, name: 'Mason Heaney' },
  895. { id: 9, name: 'Claudie Smitham' },
  896. { id: 10, name: 'Emil Schaefer' },
  897. ],
  898. get filteredPeople() {
  899. return this.query === ''
  900. ? this.people
  901. : this.people.filter((person) => {
  902. return person.name.toLowerCase().includes(this.query.toLowerCase())
  903. })
  904. },
  905. }"
  906. >
  907. <div x-combobox x-model="selected">
  908. <label x-combobox:label>Select person</label>
  909. <div>
  910. <div>
  911. <input
  912. x-combobox:input
  913. :display-value="person => person.name"
  914. @change="query = $event.target.value"
  915. placeholder="Search..."
  916. />
  917. <button x-combobox:button>Toggle</button>
  918. </div>
  919. <div x-combobox:options>
  920. <ul>
  921. <template
  922. x-for="person in filteredPeople"
  923. :key="person.id"
  924. hidden
  925. >
  926. <li
  927. x-combobox:option
  928. :option="person.id"
  929. :value="person"
  930. :disabled="person.disabled"
  931. x-text="person.name"
  932. >
  933. </li>
  934. </template>
  935. </ul>
  936. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  937. </div>
  938. </div>
  939. </div>
  940. <article>lorem ipsum</article>
  941. </div>
  942. `],
  943. ({ get }) => {
  944. get('button').click()
  945. get('li').should(haveLength('10'))
  946. // Test after closing with button and reopening
  947. get('input').type('w').trigger('input')
  948. get('li').should(haveLength('2'))
  949. get('button').click()
  950. get('button').click()
  951. get('li').should(haveLength('10'))
  952. // Test correct state after closing with ESC and reopening
  953. get('input').type('w').trigger('input')
  954. get('li').should(haveLength('2'))
  955. get('input').type('{esc}')
  956. get('button').click()
  957. get('li').should(haveLength('10'))
  958. // Test correct state after closing with TAB and reopening
  959. get('input').type('w').trigger('input')
  960. get('li').should(haveLength('2'))
  961. get('input').tab()
  962. get('button').click()
  963. get('li').should(haveLength('10'))
  964. // Test correct state after closing with external click and reopening
  965. get('input').type('w').trigger('input')
  966. get('li').should(haveLength('2'))
  967. get('article').click()
  968. get('button').click()
  969. get('li').should(haveLength('10'))
  970. },
  971. )
  972. test('active element logic when opening a combobox',
  973. [html`
  974. <div
  975. x-data="{
  976. query: '',
  977. selected: null,
  978. people: [
  979. { id: 1, name: 'Wade Cooper' },
  980. { id: 2, name: 'Arlene Mccoy' },
  981. { id: 3, name: 'Devon Webb' },
  982. { id: 4, name: 'Tom Cook' },
  983. { id: 5, name: 'Tanya Fox', disabled: true },
  984. { id: 6, name: 'Hellen Schmidt' },
  985. { id: 7, name: 'Caroline Schultz' },
  986. { id: 8, name: 'Mason Heaney' },
  987. { id: 9, name: 'Claudie Smitham' },
  988. { id: 10, name: 'Emil Schaefer' },
  989. ],
  990. get filteredPeople() {
  991. return this.query === ''
  992. ? this.people
  993. : this.people.filter((person) => {
  994. return person.name.toLowerCase().includes(this.query.toLowerCase())
  995. })
  996. },
  997. }"
  998. >
  999. <div x-combobox x-model="selected">
  1000. <label x-combobox:label>Select person</label>
  1001. <div>
  1002. <div>
  1003. <input
  1004. x-combobox:input
  1005. :display-value="person => person.name"
  1006. @change="query = $event.target.value"
  1007. placeholder="Search..."
  1008. />
  1009. <button x-combobox:button>Toggle</button>
  1010. </div>
  1011. <div x-combobox:options>
  1012. <ul>
  1013. <template
  1014. x-for="person in filteredPeople"
  1015. :key="person.id"
  1016. hidden
  1017. >
  1018. <li
  1019. x-combobox:option
  1020. :option="person.id"
  1021. :value="person"
  1022. :disabled="person.disabled"
  1023. x-text="person.name"
  1024. >
  1025. </li>
  1026. </template>
  1027. </ul>
  1028. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  1029. </div>
  1030. </div>
  1031. </div>
  1032. </div>
  1033. `],
  1034. ({ get }) => {
  1035. get('button').click()
  1036. // First option is selected on opening if no preselection
  1037. get('ul').should(beVisible())
  1038. get('[option="1"]').should(haveAttribute('aria-selected', 'true'))
  1039. // First match is selected while typing
  1040. get('[option="4"]').should(haveAttribute('aria-selected', 'false'))
  1041. get('input').type('T')
  1042. get('input').trigger('change')
  1043. get('[option="4"]').should(haveAttribute('aria-selected', 'true'))
  1044. // Reset state and select option 3
  1045. get('button').click()
  1046. get('button').click()
  1047. get('[option="3"]').click()
  1048. // Previous selection is selected
  1049. get('button').click()
  1050. get('[option="4"]').should(haveAttribute('aria-selected', 'false'))
  1051. get('[option="3"]').should(haveAttribute('aria-selected', 'true'))
  1052. }
  1053. )