CommonSimpleTable.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { waitFor } from '@testing-library/vue'
  3. import { vi } from 'vitest'
  4. import {
  5. type ExtendedMountingOptions,
  6. renderComponent,
  7. } from '#tests/support/components/index.ts'
  8. import { i18n } from '#shared/i18n.ts'
  9. import type { MenuItem } from '#desktop/components/CommonPopoverMenu/types.ts'
  10. import CommonSimpleTable from '../CommonSimpleTable.vue'
  11. import type { SimpleTableProps } from '../types.ts'
  12. const tableHeaders = [
  13. {
  14. key: 'name',
  15. label: 'User name',
  16. },
  17. {
  18. key: 'role',
  19. label: 'Role',
  20. },
  21. ]
  22. const tableItems = [
  23. {
  24. id: '1',
  25. name: 'Lindsay Walton',
  26. role: 'Member',
  27. },
  28. ]
  29. const tableActions: MenuItem[] = [
  30. {
  31. key: 'download',
  32. label: 'Download this row',
  33. icon: 'download',
  34. },
  35. {
  36. key: 'delete',
  37. label: 'Delete this row',
  38. icon: 'trash3',
  39. },
  40. ]
  41. const renderTable = (
  42. props: SimpleTableProps,
  43. options: ExtendedMountingOptions<SimpleTableProps> = { form: true },
  44. ) => {
  45. return renderComponent(CommonSimpleTable, {
  46. ...options,
  47. props,
  48. })
  49. }
  50. beforeEach(() => {
  51. i18n.setTranslationMap(new Map([['Role', 'Rolle']]))
  52. })
  53. describe('CommonSimpleTable', () => {
  54. it('displays the table without actions', async () => {
  55. const wrapper = renderTable({
  56. headers: tableHeaders,
  57. items: tableItems,
  58. caption: 'test',
  59. })
  60. expect(wrapper.getByText('User name')).toBeInTheDocument()
  61. expect(wrapper.getByText('Rolle')).toBeInTheDocument()
  62. expect(wrapper.getByText('Lindsay Walton')).toBeInTheDocument()
  63. expect(wrapper.getByText('Member')).toBeInTheDocument()
  64. expect(wrapper.queryByText('Actions')).toBeNull()
  65. })
  66. it('displays the table with actions', async () => {
  67. const wrapper = renderTable(
  68. {
  69. caption: 'test',
  70. headers: tableHeaders,
  71. items: tableItems,
  72. actions: tableActions,
  73. },
  74. { router: true },
  75. )
  76. expect(wrapper.getByText('Actions')).toBeInTheDocument()
  77. expect(wrapper.getByLabelText('Action menu button')).toBeInTheDocument()
  78. })
  79. it('displays the additional data with the item suffix slot', async () => {
  80. const wrapper = renderTable(
  81. {
  82. headers: tableHeaders,
  83. items: tableItems,
  84. actions: tableActions,
  85. caption: 'test',
  86. },
  87. {
  88. router: true,
  89. slots: {
  90. 'item-suffix-role': '<span>Additional Example</span>',
  91. },
  92. },
  93. )
  94. expect(wrapper.getByText('Additional Example')).toBeInTheDocument()
  95. })
  96. it('generates expected DOM', async () => {
  97. // TODO: check if such snapshot test is really the way we want to go.
  98. const view = renderTable(
  99. {
  100. headers: tableHeaders,
  101. items: tableItems,
  102. actions: tableActions,
  103. caption: 'test',
  104. },
  105. // NB: Please don't remove this, otherwise snapshot would contain markup of many more components other than the
  106. // one under the test, which can lead to false positives.
  107. {
  108. shallow: true,
  109. },
  110. )
  111. expect(view.baseElement.querySelector('table')).toMatchFileSnapshot(
  112. `${__filename}.snapshot.txt`,
  113. )
  114. })
  115. it('supports text truncation in cell content', async () => {
  116. const wrapper = renderTable({
  117. caption: 'test',
  118. headers: [
  119. ...tableHeaders,
  120. {
  121. key: 'truncated',
  122. label: 'Truncated',
  123. truncate: true,
  124. },
  125. ],
  126. items: [
  127. ...tableItems,
  128. {
  129. id: '2',
  130. name: 'Max Mustermann',
  131. role: 'Admin',
  132. truncated: 'Some text to be truncated',
  133. },
  134. ],
  135. })
  136. const truncatedText = wrapper.getByText('Some text to be truncated')
  137. expect(truncatedText.parentElement).toHaveClass('truncate')
  138. })
  139. it('supports tooltip on truncated cell content', async () => {
  140. const wrapper = renderTable({
  141. caption: 'test',
  142. headers: [
  143. ...tableHeaders,
  144. {
  145. key: 'truncated',
  146. label: 'Truncated',
  147. truncate: true,
  148. },
  149. ],
  150. items: [
  151. ...tableItems,
  152. {
  153. id: '2',
  154. name: 'Max Mustermann',
  155. role: 'Admin',
  156. truncated: 'Some text to be truncated',
  157. },
  158. ],
  159. })
  160. await wrapper.events.hover(wrapper.getByText('Max Mustermann'))
  161. await waitFor(() => {
  162. expect(wrapper.getByText('Some text to be truncated')).toBeInTheDocument()
  163. expect(
  164. wrapper.getByLabelText('Some text to be truncated'),
  165. ).toBeInTheDocument()
  166. })
  167. })
  168. it('supports header slot', () => {
  169. const wrapper = renderTable(
  170. {
  171. caption: 'test',
  172. headers: tableHeaders,
  173. items: tableItems,
  174. actions: tableActions,
  175. },
  176. {
  177. slots: {
  178. 'column-header-name': '<div>Custom header</div>',
  179. },
  180. },
  181. )
  182. expect(wrapper.getByText('Custom header')).toBeInTheDocument()
  183. })
  184. it('supports listening for row click events', async () => {
  185. const mockedCallback = vi.fn()
  186. const item = tableItems[0]
  187. const wrapper = renderComponent({
  188. components: { CommonSimpleTable },
  189. setup() {
  190. return {
  191. mockedCallback,
  192. tableHeaders,
  193. items: [item],
  194. }
  195. },
  196. template: `<CommonSimpleTable caption="Test" @click-row="mockedCallback" :headers="tableHeaders" :items="items"/>`,
  197. })
  198. await wrapper.events.click(wrapper.getByText('Lindsay Walton'))
  199. expect(mockedCallback).toHaveBeenCalledWith(item)
  200. await wrapper.events.keyboard('{enter}')
  201. expect(mockedCallback).toHaveBeenCalledWith(item)
  202. })
  203. it('supports adding class to table header', () => {
  204. const wrapper = renderTable({
  205. caption: 'test',
  206. headers: [
  207. {
  208. key: 'name',
  209. label: 'Awesome Cell Header',
  210. labelClass: 'text-red-500 font-bold',
  211. },
  212. ],
  213. items: [
  214. {
  215. id: '2',
  216. name: 'foo cell',
  217. },
  218. ],
  219. })
  220. expect(wrapper.getByText('Awesome Cell Header')).toHaveClass(
  221. 'text-red-500 font-bold',
  222. )
  223. })
  224. it('supports adding a link to a cell', () => {
  225. const wrapper = renderTable(
  226. {
  227. caption: 'test',
  228. headers: [
  229. {
  230. key: 'urlTest',
  231. label: 'Link Row',
  232. type: 'link',
  233. },
  234. ],
  235. items: [
  236. {
  237. id: '1',
  238. urlTest: {
  239. label: 'Example',
  240. link: 'https://example.com',
  241. openInNewTab: true,
  242. external: true,
  243. },
  244. },
  245. ],
  246. },
  247. { router: true },
  248. )
  249. const linkCell = wrapper.getByRole('link')
  250. expect(linkCell).toHaveTextContent('Example')
  251. expect(linkCell).toHaveAttribute('href', 'https://example.com')
  252. expect(linkCell).toHaveAttribute('target', '_blank')
  253. })
  254. })