CommonDropdown.spec.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. console.log(wrapper.html())
  40. await wrapper.events.click(
  41. wrapper.getByRole('button', { name: dropdownItems[0].label }),
  42. )
  43. expect(wrapper.emitted('handle-action')).toEqual([[dropdownItems[0]]])
  44. })
  45. it('supports displaying of dropdown with select items', async () => {
  46. const selectedItem = dropdownItems[0]
  47. const wrapper = renderComponent(CommonDropdown, {
  48. props: {
  49. dropdownItems,
  50. items: dropdownItems,
  51. },
  52. vModel: {
  53. modelValue: selectedItem,
  54. },
  55. })
  56. expect(
  57. wrapper.getByRole('button', { name: selectedItem.label }),
  58. ).toBeInTheDocument()
  59. await wrapper.events.click(wrapper.getByText(dropdownItems[0].label))
  60. const checkboxes = await wrapper.findAllByRole('checkbox')
  61. expect(checkboxes).toHaveLength(2)
  62. await wrapper.events.click(wrapper.getByText(dropdownItems[1].label))
  63. expect(
  64. await wrapper.findByRole('button', { name: dropdownItems[1].label }),
  65. ).toBeInTheDocument()
  66. })
  67. })