CommonButton.spec.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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(['-:inline-flex', 'bg-transparent', 'btn-sm'])
  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')).toBeDisabled()
  42. })
  43. it('supports block prop', async () => {
  44. const view = renderComponent(CommonButton, {
  45. props: {
  46. block: true,
  47. },
  48. })
  49. expect(view.getByRole('button')).toHaveClasses(['w-full'])
  50. })
  51. it.each([
  52. {
  53. variant: 'primary',
  54. classes: ['bg-blue-800'],
  55. },
  56. {
  57. variant: 'secondary',
  58. classes: ['bg-transparent'],
  59. },
  60. {
  61. variant: 'tertiary',
  62. classes: ['bg-green-200'],
  63. },
  64. {
  65. variant: 'submit',
  66. classes: ['bg-yellow-300'],
  67. },
  68. {
  69. variant: 'danger',
  70. classes: ['bg-pink-100'],
  71. },
  72. {
  73. variant: 'remove',
  74. classes: ['bg-red-400'],
  75. },
  76. {
  77. variant: 'subtle',
  78. classes: ['bg-blue-600'],
  79. },
  80. {
  81. variant: 'neutral',
  82. classes: ['bg-transparent'],
  83. },
  84. ])('supports $variant variant', async ({ variant, classes }) => {
  85. const view = renderComponent(CommonButton, {
  86. props: {
  87. variant,
  88. },
  89. })
  90. expect(view.getByRole('button')).toHaveClasses(classes)
  91. })
  92. it('supports prefix/suffix icon props', async () => {
  93. const view = renderComponent(CommonButton, {
  94. props: {
  95. prefixIcon: 'logo',
  96. suffixIcon: 'logo',
  97. },
  98. })
  99. expect(view.getAllByIconName('logo').length).toBe(2)
  100. })
  101. it('supports icon prop', async () => {
  102. const view = renderComponent(CommonButton, {
  103. props: {
  104. icon: 'logo',
  105. },
  106. slots: {
  107. default: 'foobar',
  108. },
  109. })
  110. expect(view.getByIconName('logo')).toBeInTheDocument()
  111. expect(
  112. view.queryByRole('button', { name: 'foobar' }),
  113. ).not.toBeInTheDocument()
  114. })
  115. })