CommonSelect.spec.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { ref } from 'vue'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import { i18n } from '#shared/i18n.ts'
  5. import CommonSelect, { type Props } from '../CommonSelect.vue'
  6. import type { Ref } from 'vue'
  7. const options = [
  8. {
  9. value: 0,
  10. label: 'Item A',
  11. },
  12. {
  13. value: 1,
  14. label: 'Item B',
  15. },
  16. {
  17. value: 2,
  18. label: 'Item C',
  19. },
  20. ]
  21. const html = String.raw
  22. const renderSelect = (props: Props, modelValue?: Ref) => {
  23. return renderComponent(CommonSelect, {
  24. props,
  25. slots: {
  26. default: html` <template #default="{ open }">
  27. <button @click="open()">Open Select</button>
  28. </template>`,
  29. },
  30. vModel: {
  31. modelValue,
  32. },
  33. })
  34. }
  35. beforeEach(() => {
  36. i18n.setTranslationMap(new Map([]))
  37. })
  38. describe('interacting with CommonSelect', () => {
  39. test('can select and deselect value', async () => {
  40. const modelValue = ref()
  41. const view = renderSelect({ options }, modelValue)
  42. await view.events.click(view.getByText('Open Select'))
  43. await view.events.click(view.getByText('Item A'))
  44. expect(view.emitted().select).toEqual([[options[0]]])
  45. expect(
  46. view.queryByTestId('dialog-overlay'),
  47. 'dialog is hidden',
  48. ).not.toBeInTheDocument()
  49. expect(modelValue.value).toBe(0)
  50. await view.events.click(view.getByText('Open Select'))
  51. expect(
  52. view.getByIconName((name, node) => {
  53. return (
  54. name === '#icon-check' &&
  55. !node?.parentElement?.classList.contains('invisible')
  56. )
  57. }),
  58. ).toBeInTheDocument()
  59. await view.events.click(view.getByText('Item A'))
  60. expect(view.emitted().select).toEqual([[options[0]], [options[0]]])
  61. expect(modelValue.value).toBe(undefined)
  62. })
  63. test("doesn't close select with noClose props", async () => {
  64. const view = renderSelect({ options, noClose: true })
  65. await view.events.click(view.getByText('Open Select'))
  66. await view.events.click(view.getByRole('option', { name: 'Item A' }))
  67. expect(view.getByRole('dialog')).toBeInTheDocument()
  68. })
  69. test('can select and deselect multiple values', async () => {
  70. const modelValue = ref()
  71. const view = renderSelect({ options, multiple: true }, modelValue)
  72. await view.events.click(view.getByText('Open Select'))
  73. await view.events.click(view.getByText('Item A'))
  74. expect(modelValue.value).toEqual([0])
  75. expect(view.queryAllByIconName('check-box-yes')).toHaveLength(1)
  76. await view.events.click(view.getByText('Item A'))
  77. expect(modelValue.value).toEqual([])
  78. await view.events.click(view.getByText('Item A'))
  79. await view.events.click(view.getByText('Item B'))
  80. expect(modelValue.value).toEqual([0, 1])
  81. expect(view.queryAllByIconName('check-box-yes')).toHaveLength(2)
  82. })
  83. test("passive mode doesn't change local value, but emits select", async () => {
  84. const modelValue = ref()
  85. const view = renderSelect({ options, passive: true }, modelValue)
  86. await view.events.click(view.getByText('Open Select'))
  87. await view.events.click(view.getByText('Item A'))
  88. expect(view.emitted().select).toBeDefined()
  89. expect(modelValue.value).toBeUndefined()
  90. })
  91. test("can't select disabled values", async () => {
  92. const modelValue = ref()
  93. const view = renderSelect(
  94. { options: [{ ...options[0], disabled: true }] },
  95. modelValue,
  96. )
  97. await view.events.click(view.getByText('Open Select'))
  98. await view.events.click(view.getByText('Item A'))
  99. expect(view.emitted().select).toBeUndefined()
  100. expect(modelValue.value).toBeUndefined()
  101. })
  102. test('translated values', async () => {
  103. i18n.setTranslationMap(new Map([[options[0].label, 'Translated Item A']]))
  104. const view = renderSelect({ options })
  105. await view.events.click(view.getByText('Open Select'))
  106. expect(view.getByText('Translated Item A')).toBeInTheDocument()
  107. })
  108. test("doesn't translate with no-translate prop", async () => {
  109. i18n.setTranslationMap(new Map([[options[0].label, 'Translated Item A']]))
  110. const view = renderSelect({ options, noOptionsLabelTranslation: true })
  111. await view.events.click(view.getByText('Open Select'))
  112. expect(view.getByText(/^Item A$/)).toBeInTheDocument()
  113. })
  114. test('can use boolean as value', async () => {
  115. const modelValue = ref()
  116. const view = renderSelect(
  117. {
  118. options: [
  119. { value: true, label: 'Yes' },
  120. { value: false, label: 'No' },
  121. ],
  122. },
  123. modelValue,
  124. )
  125. await view.events.click(view.getByText('Open Select'))
  126. await view.events.click(view.getByText('Yes'))
  127. expect(modelValue.value).toBe(true)
  128. })
  129. test('has an accessible name', async () => {
  130. const view = renderSelect({ options })
  131. await view.events.click(view.getByText('Open Select'))
  132. expect(view.getByRole('dialog')).toHaveAccessibleName(
  133. 'Dialog window with selections',
  134. )
  135. })
  136. })
  137. describe('traversing and focusing select', () => {
  138. it('focuses on the first element, when no option is selected', async () => {
  139. const view = renderSelect({ options })
  140. await view.events.click(view.getByText('Open Select'))
  141. expect(view.getByRole('option', { name: 'Item A' })).toHaveFocus()
  142. })
  143. it('focuses selected element, when option is selected', async () => {
  144. const modelValue = ref(1)
  145. const view = renderSelect({ options }, modelValue)
  146. await view.events.click(view.getByText('Open Select'))
  147. expect(view.getByRole('option', { name: 'Item B' })).toHaveFocus()
  148. })
  149. it('emits close when closing, so children can refocus select', async () => {
  150. const view = renderSelect({ options })
  151. await view.events.click(view.getByText('Open Select'))
  152. await view.events.click(view.getByRole('option', { name: 'Item A' }))
  153. expect(view.emitted().close).toBeDefined()
  154. })
  155. it('can travers with keyboard and select with a space', async () => {
  156. const modelValue = ref()
  157. const view = renderSelect({ options }, modelValue)
  158. await view.events.click(view.getByText('Open Select'))
  159. const optionsElements = view.getAllByRole('option')
  160. expect(optionsElements).toHaveLength(3)
  161. const [itemI, itemII, itemIII] = optionsElements
  162. expect(itemI).toHaveFocus()
  163. await view.events.keyboard('{ArrowDown}')
  164. expect(itemII).toHaveFocus()
  165. await view.events.keyboard('{ArrowDown}')
  166. expect(itemIII).toHaveFocus()
  167. await view.events.keyboard('{ArrowDown}')
  168. expect(itemI).toHaveFocus()
  169. await view.events.keyboard('{ArrowUp}')
  170. expect(itemIII).toHaveFocus()
  171. await view.events.keyboard('{ArrowUp}')
  172. expect(itemII).toHaveFocus()
  173. await view.events.keyboard(' ')
  174. expect(modelValue.value).toBe(1)
  175. })
  176. it('locks tab inside select', async () => {
  177. const modelValue = ref()
  178. const view = renderSelect({ options }, modelValue)
  179. await view.events.click(view.getByText('Open Select'))
  180. const optionsElements = view.getAllByRole('option')
  181. const [itemI, itemII, itemIII] = optionsElements
  182. expect(itemI).toHaveFocus()
  183. await view.events.keyboard('{Tab}')
  184. expect(itemII).toHaveFocus()
  185. await view.events.keyboard('{Tab}')
  186. expect(itemIII).toHaveFocus()
  187. await view.events.keyboard('{Tab}')
  188. expect(itemI).toHaveFocus()
  189. })
  190. it('refocuses on the last element that opened select', async () => {
  191. const view = renderSelect({ options })
  192. await view.events.click(view.getByText('Open Select'))
  193. await view.events.keyboard('{Escape}')
  194. expect(view.getByText('Open Select')).toHaveFocus()
  195. })
  196. it("doesn't refocuses on the last element that opened select, when specified", async () => {
  197. const view = renderSelect({ options, noRefocus: true })
  198. await view.events.click(view.getByText('Open Select'))
  199. await view.events.keyboard('{Escape}')
  200. expect(view.getByText('Open Select')).not.toHaveFocus()
  201. })
  202. it('focuses by filtered words', async () => {
  203. const view = renderSelect({ options })
  204. await view.events.click(view.getByText('Open Select'))
  205. expect(view.getByRole('option', { name: 'Item A' })).toHaveFocus()
  206. await view.events.debounced(() => view.events.keyboard('Item C'))
  207. expect(view.getByRole('option', { name: 'Item C' })).toHaveFocus()
  208. })
  209. })