CommonButton.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { renderComponent } from '#tests/support/components/index.ts'
  3. import CommonButton from '../CommonButton.vue'
  4. describe('CommonButton.vue', () => {
  5. it('renders with default prop values', async () => {
  6. const view = renderComponent(CommonButton)
  7. const button = view.getByRole('button')
  8. expect(button).toHaveAttribute('type', 'button')
  9. expect(button).toHaveClasses(['bg-gray-500', 'text-white'])
  10. })
  11. it('renders default slot as the button label', async () => {
  12. const view = renderComponent(CommonButton, {
  13. slots: {
  14. default: 'Button',
  15. },
  16. })
  17. expect(view.getByRole('button', { name: 'Button' })).toBeInTheDocument()
  18. })
  19. it('supports type prop', async () => {
  20. const view = renderComponent(CommonButton, {
  21. props: {
  22. type: 'submit',
  23. },
  24. })
  25. expect(view.getByRole('button')).toHaveAttribute('type', 'submit')
  26. })
  27. it('supports form prop', async () => {
  28. const view = renderComponent(CommonButton, {
  29. props: {
  30. form: 'foobar',
  31. },
  32. })
  33. expect(view.getByRole('button')).toHaveAttribute('form', 'foobar')
  34. })
  35. it('supports disabled prop', async () => {
  36. const view = renderComponent(CommonButton, {
  37. props: {
  38. disabled: true,
  39. },
  40. })
  41. expect(view.getByRole('button')).toHaveAttribute('disabled')
  42. })
  43. it.each([
  44. {
  45. variant: 'primary',
  46. classes: ['bg-blue', 'text-white'],
  47. },
  48. {
  49. variant: 'secondary',
  50. classes: ['bg-gray-500', 'text-white'],
  51. },
  52. {
  53. variant: 'submit',
  54. classes: ['bg-yellow', 'font-semibold', 'text-black-full'],
  55. },
  56. {
  57. variant: 'danger',
  58. classes: ['bg-red-dark', 'text-red-bright'],
  59. },
  60. ])('supports $variant variant', async ({ variant, classes }) => {
  61. const view = renderComponent(CommonButton, {
  62. props: {
  63. variant,
  64. },
  65. })
  66. expect(view.getByRole('button')).toHaveClasses(classes)
  67. })
  68. it.each([
  69. {
  70. variant: 'primary',
  71. classes: ['text-blue'],
  72. },
  73. {
  74. variant: 'secondary',
  75. classes: ['text-white'],
  76. },
  77. {
  78. variant: 'submit',
  79. classes: ['font-semibold', 'text-yellow'],
  80. },
  81. {
  82. variant: 'danger',
  83. classes: ['text-red-bright'],
  84. },
  85. ])(
  86. 'supports $variant variant with transparent background',
  87. async ({ variant, classes }) => {
  88. const view = renderComponent(CommonButton, {
  89. props: {
  90. variant,
  91. transparentBackground: true,
  92. },
  93. })
  94. const button = view.getByRole('button')
  95. expect(button).toHaveClass('bg-transparent')
  96. expect(button).toHaveClasses(classes)
  97. },
  98. )
  99. })