CommonButtonGroup.spec.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { getByIconName } from '#tests/support/components/iconQueries.ts'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import { getTestRouter } from '#tests/support/components/renderComponent.ts'
  5. import CommonButtonGroup from '../CommonButtonGroup.vue'
  6. import type { CommonButtonOption } from '../types.ts'
  7. describe('buttons group', () => {
  8. it('renders a list of buttons', async () => {
  9. const onAction = vi.fn()
  10. const options: CommonButtonOption[] = [
  11. {
  12. label: 'link %s',
  13. labelPlaceholder: ['text'],
  14. link: '/example',
  15. value: 'link',
  16. },
  17. { label: 'button', onAction, value: 'button' },
  18. {
  19. label: 'with-icon',
  20. onAction,
  21. icon: 'home',
  22. disabled: true,
  23. value: 'icon',
  24. },
  25. ]
  26. const view = renderComponent(CommonButtonGroup, {
  27. props: { options, modelValue: 'button' },
  28. router: true,
  29. store: true,
  30. })
  31. expect(view.getByRole('link', { name: 'link text' })).toHaveAttribute(
  32. 'href',
  33. '/mobile/example',
  34. )
  35. const button = view.getByRole('button', { name: 'button' })
  36. expect(button).toBeEnabled()
  37. expect(button, 'selected button has a class').toHaveClass('!bg-gray-200')
  38. await view.events.click(button)
  39. expect(onAction).toHaveBeenCalled()
  40. const iconButton = view.getByRole('button', { name: 'with-icon' })
  41. expect(iconButton).toBeDisabled()
  42. expect(getByIconName(iconButton, 'home')).toBeInTheDocument()
  43. })
  44. it("doesn't call action, if disabled", async () => {
  45. const onAction = vi.fn()
  46. const options: CommonButtonOption[] = [
  47. { label: 'link', link: '/example', disabled: true },
  48. { label: 'button', onAction, disabled: true },
  49. ]
  50. const view = renderComponent(CommonButtonGroup, {
  51. props: { options },
  52. })
  53. const button = view.getByRole('button', { name: 'button' })
  54. const link = view.getByRole('link', { name: 'link' })
  55. expect(button).toBeDisabled()
  56. const router = getTestRouter()
  57. const currentUrl = router.currentRoute.value.fullPath
  58. await view.events.click(button)
  59. await view.events.click(link)
  60. expect(onAction).not.toHaveBeenCalled()
  61. expect(router.currentRoute.value.fullPath).toBe(currentUrl)
  62. })
  63. })