CommonButton.spec.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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([
  10. '-:inline-flex',
  11. '-:bg-transparent',
  12. 'btn-sm',
  13. ])
  14. })
  15. it('renders default slot as the button label', async () => {
  16. const view = renderComponent(CommonButton, {
  17. slots: {
  18. default: 'Button',
  19. },
  20. })
  21. expect(view.getByRole('button', { name: 'Button' })).toBeInTheDocument()
  22. })
  23. it('supports type prop', async () => {
  24. const view = renderComponent(CommonButton, {
  25. props: {
  26. type: 'submit',
  27. },
  28. })
  29. expect(view.getByRole('button')).toHaveAttribute('type', 'submit')
  30. })
  31. it('supports form prop', async () => {
  32. const view = renderComponent(CommonButton, {
  33. props: {
  34. form: 'foobar',
  35. },
  36. })
  37. expect(view.getByRole('button')).toHaveAttribute('form', 'foobar')
  38. })
  39. it('supports disabled prop', async () => {
  40. const view = renderComponent(CommonButton, {
  41. props: {
  42. disabled: true,
  43. },
  44. })
  45. expect(view.getByRole('button')).toBeDisabled()
  46. })
  47. it('supports block prop', async () => {
  48. const view = renderComponent(CommonButton, {
  49. props: {
  50. block: true,
  51. },
  52. })
  53. expect(view.getByRole('button')).toHaveClasses(['w-full'])
  54. })
  55. it.each([
  56. {
  57. variant: 'primary',
  58. classes: ['bg-blue-800'],
  59. },
  60. {
  61. variant: 'secondary',
  62. classes: ['-:bg-transparent'],
  63. },
  64. {
  65. variant: 'tertiary',
  66. classes: ['bg-green-200'],
  67. },
  68. {
  69. variant: 'submit',
  70. classes: ['bg-yellow-300'],
  71. },
  72. {
  73. variant: 'danger',
  74. classes: ['bg-pink-100'],
  75. },
  76. {
  77. variant: 'remove',
  78. classes: ['bg-red-400'],
  79. },
  80. {
  81. variant: 'subtle',
  82. classes: ['bg-blue-600'],
  83. },
  84. {
  85. variant: 'neutral',
  86. classes: ['bg-transparent'],
  87. },
  88. ])('supports $variant variant', async ({ variant, classes }) => {
  89. const view = renderComponent(CommonButton, {
  90. props: {
  91. variant,
  92. },
  93. })
  94. expect(view.getByRole('button')).toHaveClasses(classes)
  95. })
  96. it('supports prefix/suffix icon props', async () => {
  97. const view = renderComponent(CommonButton, {
  98. props: {
  99. prefixIcon: 'logo',
  100. suffixIcon: 'logo',
  101. },
  102. })
  103. expect(view.getAllByIconName('logo').length).toBe(2)
  104. })
  105. it('supports icon prop', async () => {
  106. const view = renderComponent(CommonButton, {
  107. props: {
  108. icon: 'logo',
  109. },
  110. slots: {
  111. default: 'foobar',
  112. },
  113. })
  114. expect(view.getByIconName('logo')).toBeInTheDocument()
  115. expect(
  116. view.queryByRole('button', { name: 'foobar' }),
  117. ).not.toBeInTheDocument()
  118. })
  119. })