CommonTabManager.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { waitFor } from '@testing-library/vue'
  3. import { describe } from 'vitest'
  4. import { renderComponent } from '#tests/support/components/index.ts'
  5. import CommonTabManager from '#desktop/components/CommonTabManager/CommonTabManager.vue'
  6. describe('CommonTabManager', () => {
  7. describe('single tab mode', () => {
  8. const tabs = [
  9. { label: 'Tab 1', key: 'tab-1' },
  10. { label: 'Tab 2', key: 'tab-2' },
  11. { label: 'Tab 3', key: 'tab-3' },
  12. ]
  13. it('renders CommonTabManager', () => {
  14. const wrapper = renderComponent(CommonTabManager, {
  15. props: {
  16. tabs,
  17. },
  18. })
  19. expect(wrapper.getByText('Tab 1')).toBeInTheDocument()
  20. expect(wrapper.getByText('Tab 2')).toBeInTheDocument()
  21. expect(wrapper.getByText('Tab 3')).toBeInTheDocument()
  22. })
  23. it('set by default the first tab to active', () => {
  24. const wrapper = renderComponent(CommonTabManager, {
  25. props: {
  26. tabs,
  27. },
  28. })
  29. waitFor(() => {
  30. expect(wrapper.getByRole('tab', { selected: true })).toHaveTextContent(
  31. 'Tab 1',
  32. )
  33. })
  34. })
  35. it('allows setting a default active tab', () => {
  36. const wrapper = renderComponent(CommonTabManager, {
  37. props: {
  38. tabs: [
  39. ...tabs,
  40. {
  41. label: 'Tab 4',
  42. key: 'tab-4',
  43. default: true,
  44. },
  45. ],
  46. },
  47. })
  48. waitFor(() => {
  49. expect(wrapper.getByRole('tab', { selected: true })).toHaveTextContent(
  50. 'Tab 4',
  51. )
  52. })
  53. })
  54. it('switches tab on click', async () => {
  55. const wrapper = renderComponent(CommonTabManager, {
  56. props: {
  57. tabs: [...tabs],
  58. },
  59. })
  60. await wrapper.events.click(wrapper.getByRole('tab', { name: 'Tab 2' }))
  61. waitFor(() => {
  62. expect(wrapper.getByRole('tab', { selected: true })).toHaveTextContent(
  63. 'Tab 2',
  64. )
  65. })
  66. })
  67. })
  68. describe('filter mode', () => {
  69. const filters = [
  70. { label: 'Admin', key: 'admin' },
  71. { label: 'Agent', key: 'agent' },
  72. { label: 'Customer', key: 'customer' },
  73. ]
  74. it('renders CommonTabManager', () => {
  75. const wrapper = renderComponent(CommonTabManager, {
  76. props: {
  77. tabs: filters,
  78. label: 'Roles',
  79. multiple: true,
  80. },
  81. })
  82. // A11y
  83. expect(wrapper.getByText('Roles')).toBeInTheDocument()
  84. expect(wrapper.getAllByLabelText('Roles')).toHaveLength(3)
  85. expect(wrapper.getByText('Admin')).toBeInTheDocument()
  86. expect(wrapper.getByText('Agent')).toBeInTheDocument()
  87. expect(wrapper.getByText('Customer')).toBeInTheDocument()
  88. })
  89. it('selects two filters', async () => {
  90. const wrapper = renderComponent(CommonTabManager, {
  91. props: {
  92. tabs: filters,
  93. label: 'Roles',
  94. multiple: true,
  95. },
  96. })
  97. await wrapper.events.click(wrapper.getByText('Admin'))
  98. await wrapper.events.click(wrapper.getByText('Agent'))
  99. waitFor(() => {
  100. expect(wrapper.getAllByRole('option', { selected: true })).toHaveLength(
  101. 2,
  102. )
  103. })
  104. await wrapper.events.click(wrapper.getByText('Admin'))
  105. waitFor(() => {
  106. expect(wrapper.getAllByRole('option', { selected: true })).toHaveLength(
  107. 1,
  108. )
  109. })
  110. })
  111. })
  112. })