CommonSelect.spec.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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, focus }">
  27. <button @click="open()">Open Select</button>
  28. <button @click="focus()">Move Focus</button>
  29. </template>`,
  30. },
  31. vModel: {
  32. modelValue,
  33. },
  34. })
  35. }
  36. beforeEach(() => {
  37. i18n.setTranslationMap(new Map([]))
  38. })
  39. describe('CommonSelect.vue', () => {
  40. it('can select and unselect value', async () => {
  41. const modelValue = ref()
  42. const view = renderSelect({ options }, modelValue)
  43. await view.events.click(view.getByText('Open Select'))
  44. await view.events.click(view.getByText('Item A'))
  45. expect(view.emitted().select).toEqual([[options[0]]])
  46. expect(
  47. view.queryByRole('menu'),
  48. 'dropdown is hidden',
  49. ).not.toBeInTheDocument()
  50. expect(modelValue.value).toBe(0)
  51. await view.events.click(view.getByText('Open Select'))
  52. expect(
  53. view.getByIconName((name, node) => {
  54. return (
  55. name === '#icon-check2' &&
  56. !node?.parentElement?.classList.contains('invisible')
  57. )
  58. }),
  59. ).toBeInTheDocument()
  60. await view.events.click(view.getByText('Item A'))
  61. expect(view.emitted().select).toEqual([[options[0]], [options[0]]])
  62. expect(modelValue.value).toBe(undefined)
  63. })
  64. it('does not close select with noClose prop', async () => {
  65. const view = renderSelect({ options, noClose: true })
  66. await view.events.click(view.getByText('Open Select'))
  67. await view.events.click(view.getByRole('option', { name: 'Item A' }))
  68. expect(view.getByRole('menu')).toBeInTheDocument()
  69. })
  70. it('can select and unselect multiple values', async () => {
  71. const modelValue = ref()
  72. const view = renderSelect({ options, multiple: true }, modelValue)
  73. await view.events.click(view.getByText('Open Select'))
  74. await view.events.click(view.getByText('Item A'))
  75. expect(modelValue.value).toEqual([0])
  76. expect(view.queryAllByIconName('check-square')).toHaveLength(1)
  77. await view.events.click(view.getByText('Item A'))
  78. expect(modelValue.value).toEqual([])
  79. await view.events.click(view.getByText('Item A'))
  80. await view.events.click(view.getByText('Item B'))
  81. expect(modelValue.value).toEqual([0, 1])
  82. expect(view.queryAllByIconName('check-square')).toHaveLength(2)
  83. })
  84. it('can use select all action with active multiple', async () => {
  85. const modelValue = ref()
  86. const view = renderSelect({ options, multiple: true }, modelValue)
  87. await view.events.click(view.getByText('Open Select'))
  88. await view.events.click(view.getByText('select all options'))
  89. expect(modelValue.value).toEqual([0, 1, 2])
  90. expect(view.queryAllByIconName('check-square')).toHaveLength(3)
  91. })
  92. it('can add additional actions', async () => {
  93. const modelValue = ref()
  94. const actionCallbackSpy = vi.fn()
  95. const view = renderSelect(
  96. {
  97. options,
  98. multiple: true,
  99. actions: [
  100. {
  101. label: 'example action',
  102. key: 'example',
  103. onClick: actionCallbackSpy,
  104. },
  105. ],
  106. },
  107. modelValue,
  108. )
  109. await view.events.click(view.getByText('Open Select'))
  110. await view.events.click(view.getByText('example action'))
  111. expect(actionCallbackSpy).toHaveBeenCalledTimes(1)
  112. })
  113. it('passive mode does not change local value, but emits select', async () => {
  114. const modelValue = ref()
  115. const view = renderSelect({ options, passive: true }, modelValue)
  116. await view.events.click(view.getByText('Open Select'))
  117. await view.events.click(view.getByText('Item A'))
  118. expect(view.emitted().select).toBeDefined()
  119. expect(modelValue.value).toBeUndefined()
  120. })
  121. it('cannot select disabled values', async () => {
  122. const modelValue = ref()
  123. const view = renderSelect(
  124. { options: [{ ...options[0], disabled: true }] },
  125. modelValue,
  126. )
  127. await view.events.click(view.getByText('Open Select'))
  128. expect(view.getByRole('option')).toHaveAttribute('aria-disabled', 'true')
  129. await view.events.click(view.getByText('Item A'))
  130. expect(view.emitted().select).toBeUndefined()
  131. expect(modelValue.value).toBeUndefined()
  132. })
  133. it('translates labels', async () => {
  134. i18n.setTranslationMap(new Map([[options[0].label, 'Translated Item A']]))
  135. const view = renderSelect({ options })
  136. await view.events.click(view.getByText('Open Select'))
  137. expect(view.getByText('Translated Item A')).toBeInTheDocument()
  138. })
  139. it('does not translate with noOptionsLabelTranslation prop', async () => {
  140. i18n.setTranslationMap(new Map([[options[0].label, 'Translated Item A']]))
  141. const view = renderSelect({ options, noOptionsLabelTranslation: true })
  142. await view.events.click(view.getByText('Open Select'))
  143. expect(view.getByText(/^Item A$/)).toBeInTheDocument()
  144. })
  145. it('forces translation if placeholder is provided', async () => {
  146. const options = [
  147. {
  148. value: 0,
  149. label: 'Label (%s)',
  150. labelPlaceholder: ['A'],
  151. heading: 'Heading (%s)',
  152. headingPlaceholder: ['B'],
  153. },
  154. ]
  155. const view = renderSelect({ options, noOptionsLabelTranslation: true })
  156. await view.events.click(view.getByText('Open Select'))
  157. expect(
  158. view.getByRole('option', { name: 'Label (A) – Heading (B)' }),
  159. ).toBeInTheDocument()
  160. })
  161. it('can use boolean as value', async () => {
  162. const modelValue = ref()
  163. const view = renderSelect(
  164. {
  165. options: [
  166. { value: true, label: 'Yes' },
  167. { value: false, label: 'No' },
  168. ],
  169. },
  170. modelValue,
  171. )
  172. await view.events.click(view.getByText('Open Select'))
  173. await view.events.click(view.getByText('Yes'))
  174. expect(modelValue.value).toBe(true)
  175. })
  176. it('has an accessible name', async () => {
  177. const view = renderSelect({ options })
  178. await view.events.click(view.getByText('Open Select'))
  179. expect(view.getByRole('listbox')).toHaveAccessibleName('Select…')
  180. })
  181. it('supports optional headings', async () => {
  182. const view = renderSelect({
  183. options: [
  184. {
  185. value: 0,
  186. label: 'foo (%s)',
  187. labelPlaceholder: ['1'],
  188. heading: 'bar (%s)',
  189. headingPlaceholder: ['2'],
  190. },
  191. ],
  192. })
  193. await view.events.click(view.getByText('Open Select'))
  194. const option = view.getByRole('option')
  195. expect(option).toHaveTextContent('foo (1) – bar (2)')
  196. expect(option.children[1]).toHaveAttribute(
  197. 'aria-label',
  198. 'foo (1) – bar (2)',
  199. )
  200. })
  201. it('supports navigating options with children', async () => {
  202. const testChildOption = {
  203. value: 1,
  204. label: 'child',
  205. }
  206. const testParentOption = {
  207. value: 1,
  208. label: 'parent',
  209. disabled: true,
  210. children: [testChildOption],
  211. }
  212. const view = renderSelect({
  213. options: [testParentOption],
  214. })
  215. await view.events.click(view.getByText('Open Select'))
  216. expect(
  217. view.queryByRole('button', { name: 'Back to previous page' }),
  218. ).not.toBeInTheDocument()
  219. expect(view.getByRole('option')).toHaveTextContent('parent')
  220. expect(view.getByRole('option')).toHaveAttribute('aria-disabled', 'true')
  221. await view.events.click(view.getByRole('button', { name: 'Has submenu' }))
  222. expect(view.emitted().push).toEqual([[testParentOption]])
  223. await view.rerender({
  224. options: [testChildOption],
  225. isChildPage: true,
  226. })
  227. expect(view.getByRole('option')).toHaveTextContent('child')
  228. await view.events.click(
  229. view.getByRole('button', { name: 'Back to previous page' }),
  230. )
  231. expect(view.emitted().pop).toEqual([[]])
  232. await view.rerender({
  233. options: [testParentOption],
  234. isChildPage: false,
  235. })
  236. expect(
  237. view.queryByRole('button', { name: 'Back to previous page' }),
  238. ).not.toBeInTheDocument()
  239. expect(view.getByRole('option')).toHaveTextContent('parent')
  240. expect(view.getByRole('option')).toHaveAttribute('aria-disabled', 'true')
  241. await view.events.click(view.getByRole('button', { name: 'Has submenu' }))
  242. await view.rerender({
  243. options: [testChildOption],
  244. isChildPage: true,
  245. })
  246. await view.events.click(view.getByText('child'))
  247. expect(view.emitted().select).toEqual([[testChildOption]])
  248. })
  249. })