CommonDropdown.spec.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { renderComponent } from '#tests/support/components/index.ts'
  3. import CommonDropdown from '#desktop/components/CommonDropdown/CommonDropdown.vue'
  4. const dropdownItems = [
  5. {
  6. label: 'Option 1',
  7. key: 'option1',
  8. },
  9. {
  10. label: 'Option 2',
  11. key: 'option2',
  12. },
  13. ]
  14. describe('CommonDropdown', () => {
  15. it('supports displaying of dropdown with action items', async () => {
  16. const wrapper = renderComponent(CommonDropdown, {
  17. props: {
  18. items: dropdownItems,
  19. actionLabel: 'text-dropdown',
  20. },
  21. })
  22. await wrapper.events.click(
  23. wrapper.getByRole('button', { name: 'text-dropdown' }),
  24. )
  25. expect(await wrapper.findByRole('menu')).toBeInTheDocument()
  26. expect(wrapper.queryByRole('checkbox')).not.toBeInTheDocument()
  27. })
  28. it('emits event when dropdown item is clicked', async () => {
  29. const wrapper = renderComponent(CommonDropdown, {
  30. props: {
  31. items: dropdownItems,
  32. actionLabel: 'action-dropdown',
  33. },
  34. })
  35. await wrapper.events.click(
  36. wrapper.getByRole('button', { name: 'action-dropdown' }),
  37. )
  38. expect(await wrapper.findByRole('menu')).toBeInTheDocument()
  39. await wrapper.events.click(
  40. wrapper.getByRole('button', { name: dropdownItems[0].label }),
  41. )
  42. expect(wrapper.emitted('handle-action')).toEqual([[dropdownItems[0]]])
  43. })
  44. it('supports displaying of dropdown with select items', async () => {
  45. const selectedItem = dropdownItems[0]
  46. const wrapper = renderComponent(CommonDropdown, {
  47. props: {
  48. dropdownItems,
  49. items: dropdownItems,
  50. },
  51. vModel: {
  52. modelValue: selectedItem,
  53. },
  54. })
  55. expect(
  56. wrapper.getByRole('button', { name: selectedItem.label }),
  57. ).toBeInTheDocument()
  58. await wrapper.events.click(wrapper.getByText(dropdownItems[0].label))
  59. const checkboxes = await wrapper.findAllByRole('checkbox')
  60. expect(checkboxes).toHaveLength(2)
  61. await wrapper.events.click(wrapper.getByText(dropdownItems[1].label))
  62. expect(
  63. await wrapper.findByRole('button', { name: dropdownItems[1].label }),
  64. ).toBeInTheDocument()
  65. })
  66. })