CommonAdvancedTable.spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { waitFor, within } from '@testing-library/vue'
  3. import { vi } from 'vitest'
  4. import { ref } from 'vue'
  5. import ticketObjectAttributes from '#tests/graphql/factories/fixtures/ticket-object-attributes.ts'
  6. import {
  7. type ExtendedMountingOptions,
  8. renderComponent,
  9. } from '#tests/support/components/index.ts'
  10. import { waitForNextTick } from '#tests/support/utils.ts'
  11. import { mockObjectManagerFrontendAttributesQuery } from '#shared/entities/object-attributes/graphql/queries/objectManagerFrontendAttributes.mocks.ts'
  12. import { EnumObjectManagerObjects } from '#shared/graphql/types.ts'
  13. import {
  14. convertToGraphQLId,
  15. getIdFromGraphQLId,
  16. } from '#shared/graphql/utils.ts'
  17. import { i18n } from '#shared/i18n.ts'
  18. import type { ObjectWithId } from '#shared/types/utils.ts'
  19. import type { MenuItem } from '#desktop/components/CommonPopoverMenu/types.ts'
  20. import CommonAdvancedTable from '../CommonAdvancedTable.vue'
  21. import type { AdvancedTableProps, TableAdvancedItem } from '../types.ts'
  22. const tableHeaders = ['title', 'owner', 'state', 'priority', 'created_at']
  23. const tableItems: TableAdvancedItem[] = [
  24. {
  25. id: convertToGraphQLId('Ticket', 1),
  26. title: 'Dummy ticket',
  27. owner: {
  28. __type: 'User',
  29. id: convertToGraphQLId('User', 1),
  30. internalId: 2,
  31. firstname: 'Agent 1',
  32. lastname: 'Test',
  33. fullname: 'Agent 1 Test',
  34. },
  35. state: {
  36. __typename: 'TicketState',
  37. id: convertToGraphQLId('TicketState', 1),
  38. name: 'open',
  39. },
  40. priority: {
  41. __typename: 'TicketPriority',
  42. id: convertToGraphQLId('TicketPriority', 3),
  43. name: '3 high',
  44. },
  45. created_at: '2021-01-01T12:00:00Z',
  46. },
  47. ]
  48. const tableActions: MenuItem[] = [
  49. {
  50. key: 'download',
  51. label: 'Download this row',
  52. icon: 'download',
  53. },
  54. {
  55. key: 'delete',
  56. label: 'Delete this row',
  57. icon: 'trash3',
  58. },
  59. ]
  60. const renderTable = async (
  61. props: AdvancedTableProps,
  62. options: ExtendedMountingOptions<AdvancedTableProps> = { form: true },
  63. ) => {
  64. const wrapper = renderComponent(CommonAdvancedTable, {
  65. ...options,
  66. props: {
  67. object: EnumObjectManagerObjects.Ticket,
  68. ...props,
  69. },
  70. })
  71. await waitForNextTick()
  72. return wrapper
  73. }
  74. beforeEach(() => {
  75. mockObjectManagerFrontendAttributesQuery({
  76. objectManagerFrontendAttributes: ticketObjectAttributes(),
  77. })
  78. i18n.setTranslationMap(new Map([['Priority', 'Wichtigkeit']]))
  79. })
  80. describe('CommonAdvancedTable', () => {
  81. it('displays the table without actions', async () => {
  82. const wrapper = await renderTable({
  83. headers: tableHeaders,
  84. items: tableItems,
  85. totalItems: 100,
  86. caption: 'Table caption',
  87. })
  88. expect(wrapper.getByText('Title')).toBeInTheDocument()
  89. expect(wrapper.getByText('Owner')).toBeInTheDocument()
  90. expect(wrapper.getByText('Wichtigkeit')).toBeInTheDocument()
  91. expect(wrapper.getByText('State')).toBeInTheDocument()
  92. expect(wrapper.getByText('Dummy ticket')).toBeInTheDocument()
  93. expect(wrapper.getByText('Agent 1 Test')).toBeInTheDocument()
  94. expect(wrapper.getByText('open')).toBeInTheDocument()
  95. expect(wrapper.getByText('3 high')).toBeInTheDocument()
  96. expect(wrapper.queryByText('Actions')).toBeNull()
  97. })
  98. it('displays the table with actions', async () => {
  99. const wrapper = await renderTable(
  100. {
  101. headers: tableHeaders,
  102. items: tableItems,
  103. totalItems: 100,
  104. actions: tableActions,
  105. caption: 'Table caption',
  106. },
  107. {
  108. router: true,
  109. form: true,
  110. },
  111. )
  112. expect(wrapper.getByText('Actions')).toBeInTheDocument()
  113. expect(wrapper.getByLabelText('Action menu button')).toBeInTheDocument()
  114. })
  115. it('displays the additional data with the item suffix slot', async () => {
  116. const wrapper = await renderTable(
  117. {
  118. headers: tableHeaders,
  119. items: tableItems,
  120. totalItems: 100,
  121. actions: tableActions,
  122. caption: 'Table caption',
  123. },
  124. {
  125. router: true,
  126. form: true,
  127. slots: {
  128. 'item-suffix-title': '<span>Additional Example</span>',
  129. },
  130. },
  131. )
  132. expect(wrapper.getByText('Additional Example')).toBeInTheDocument()
  133. })
  134. it('generates expected DOM', async () => {
  135. // TODO: check if such snapshot test is really the way we want to go.
  136. const view = await renderTable(
  137. {
  138. headers: tableHeaders,
  139. items: tableItems,
  140. totalItems: 100,
  141. actions: tableActions,
  142. caption: 'Table caption',
  143. },
  144. // NB: Please don't remove this, otherwise snapshot would contain markup of many more components other than the
  145. // one under the test, which can lead to false positives.
  146. {
  147. shallow: true,
  148. form: true,
  149. },
  150. )
  151. expect(view.baseElement.querySelector('table')).toMatchFileSnapshot(
  152. `${__filename}.snapshot.txt`,
  153. )
  154. })
  155. it('supports text truncation in cell content', async () => {
  156. const wrapper = await renderTable({
  157. headers: [...tableHeaders, 'truncated', 'untruncated'],
  158. attributes: [
  159. {
  160. name: 'truncated',
  161. label: 'Truncated',
  162. headerPreferences: {
  163. truncate: true,
  164. },
  165. columnPreferences: {},
  166. dataOption: {
  167. type: 'text',
  168. },
  169. dataType: 'input',
  170. },
  171. {
  172. name: 'untruncated',
  173. label: 'Untruncated',
  174. headerPreferences: {
  175. truncate: false,
  176. },
  177. columnPreferences: {},
  178. dataOption: {
  179. type: 'text',
  180. },
  181. dataType: 'input',
  182. },
  183. ],
  184. items: [
  185. ...tableItems,
  186. {
  187. id: convertToGraphQLId('Ticket', 2),
  188. name: 'Max Mustermann',
  189. role: 'Admin',
  190. truncated: 'Some text to be truncated',
  191. untruncated: 'Some text not to be truncated',
  192. },
  193. ],
  194. totalItems: 100,
  195. caption: 'Table caption',
  196. })
  197. const truncatedText = wrapper.getByText('Some text to be truncated')
  198. expect(truncatedText).toHaveAttribute('data-tooltip', 'true')
  199. expect(truncatedText.parentElement).toHaveClass('truncate')
  200. const untruncatedText = wrapper.getByText('Some text not to be truncated')
  201. expect(untruncatedText).not.toHaveAttribute('data-tooltip')
  202. expect(untruncatedText.parentElement).not.toHaveClass('truncate')
  203. })
  204. it('supports header slot', async () => {
  205. const wrapper = await renderTable(
  206. {
  207. headers: tableHeaders,
  208. items: tableItems,
  209. actions: tableActions,
  210. totalItems: 100,
  211. caption: 'Table caption',
  212. },
  213. {
  214. form: true,
  215. slots: {
  216. 'column-header-title': '<div>Custom header</div>',
  217. },
  218. },
  219. )
  220. expect(wrapper.getByText('Custom header')).toBeInTheDocument()
  221. })
  222. it('supports listening for row click events', async () => {
  223. const mockedCallback = vi.fn()
  224. const item = tableItems[0]
  225. const wrapper = renderComponent(
  226. {
  227. components: { CommonAdvancedTable },
  228. setup() {
  229. return {
  230. mockedCallback,
  231. tableHeaders,
  232. attributes: [
  233. {
  234. name: 'title',
  235. label: 'Title',
  236. headerPreferences: {},
  237. columnPreferences: {},
  238. dataOption: {},
  239. dataType: 'input',
  240. },
  241. ],
  242. items: [item],
  243. }
  244. },
  245. template: `<CommonAdvancedTable @click-row="mockedCallback" :headers="tableHeaders" :attributes="attributes" :items="items" :total-items="100" caption="Table caption" />`,
  246. },
  247. { form: true },
  248. )
  249. await waitForNextTick()
  250. await wrapper.events.click(wrapper.getByText('Dummy ticket'))
  251. expect(mockedCallback).toHaveBeenCalledWith(item)
  252. mockedCallback.mockClear()
  253. wrapper.getByRole('row', { description: 'Select table row' }).focus()
  254. await wrapper.events.keyboard('{enter}')
  255. expect(mockedCallback).toHaveBeenCalledWith(item)
  256. })
  257. it('supports marking row in active color', async () => {
  258. const wrapper = await renderTable({
  259. headers: tableHeaders,
  260. selectedRowId: '2',
  261. items: [
  262. {
  263. id: '2',
  264. name: 'foo',
  265. },
  266. ],
  267. totalItems: 100,
  268. caption: 'Table caption',
  269. })
  270. const row = wrapper.getByTestId('table-row')
  271. expect(row).toHaveClass('!bg-blue-800')
  272. expect(within(row).getAllByRole('cell')[1].children[0]).toHaveClass(
  273. 'text-black dark:text-white',
  274. )
  275. })
  276. it('supports adding class to table header', async () => {
  277. const wrapper = await renderTable({
  278. headers: ['name'],
  279. attributes: [
  280. {
  281. name: 'name',
  282. label: 'Awesome Cell Header',
  283. headerPreferences: {
  284. labelClass: 'text-red-500 font-bold',
  285. },
  286. columnPreferences: {},
  287. dataOption: {
  288. type: 'text',
  289. },
  290. dataType: 'input',
  291. },
  292. ],
  293. items: [],
  294. totalItems: 100,
  295. caption: 'Table caption',
  296. })
  297. expect(wrapper.getByText('Awesome Cell Header')).toHaveClass(
  298. 'text-red-500 font-bold',
  299. )
  300. })
  301. it('supports adding a link to a cell', async () => {
  302. const wrapper = await renderTable(
  303. {
  304. headers: ['title'],
  305. attributeExtensions: {
  306. title: {
  307. columnPreferences: {
  308. link: {
  309. internal: true,
  310. getLink: (item: ObjectWithId) =>
  311. `/tickets/${getIdFromGraphQLId(item.id)}`,
  312. },
  313. },
  314. },
  315. },
  316. items: [tableItems[0]],
  317. totalItems: 100,
  318. caption: 'Table caption',
  319. },
  320. {
  321. form: true,
  322. router: true,
  323. },
  324. )
  325. const linkCell = wrapper.getByRole('link')
  326. expect(linkCell).toHaveTextContent('Dummy ticket')
  327. expect(linkCell).toHaveAttribute('href', '/desktop/tickets/1')
  328. expect(linkCell).not.toHaveAttribute('target')
  329. })
  330. it.todo('supports row selection', async () => {
  331. const checkedRows = ref([])
  332. const items = [
  333. {
  334. id: convertToGraphQLId('Ticket', 1),
  335. label: 'selection data 1',
  336. },
  337. {
  338. id: convertToGraphQLId('Ticket', 2),
  339. label: 'selection data 2',
  340. },
  341. ]
  342. const wrapper = await renderTable(
  343. {
  344. headers: ['label'],
  345. items,
  346. hasCheckboxColumn: true,
  347. totalItems: 100,
  348. caption: 'Table caption',
  349. },
  350. { form: true, vModel: { checkedRows } },
  351. )
  352. expect(wrapper.getAllByRole('checkbox')).toHaveLength(3)
  353. const selectAllCheckbox = wrapper.getByLabelText('Select all entries')
  354. expect(selectAllCheckbox).not.toHaveAttribute('checked')
  355. const rowCheckboxes = wrapper.getAllByRole('checkbox', {
  356. name: 'Select this entry',
  357. })
  358. await wrapper.events.click(rowCheckboxes[0])
  359. expect(rowCheckboxes[0]).toHaveAttribute('checked')
  360. await wrapper.events.click(rowCheckboxes[1])
  361. await waitFor(() => expect(checkedRows.value).toEqual(items))
  362. await waitFor(() => expect(selectAllCheckbox).toHaveAttribute('checked'))
  363. await wrapper.events.click(wrapper.getByLabelText('Deselect all entries'))
  364. await waitFor(() => expect(rowCheckboxes[0]).not.toHaveAttribute('checked'))
  365. expect(rowCheckboxes[1]).not.toHaveAttribute('checked')
  366. await wrapper.events.click(rowCheckboxes[1])
  367. expect(
  368. await wrapper.findByLabelText('Deselect this entry'),
  369. ).toBeInTheDocument()
  370. })
  371. it.todo('supports disabling checkbox item for specific rows', async () => {
  372. const checkedRows = ref([])
  373. const items = [
  374. {
  375. id: convertToGraphQLId('Ticket', 1),
  376. checked: false,
  377. disabled: true,
  378. label: 'selection data 1',
  379. },
  380. {
  381. id: convertToGraphQLId('Ticket', 2),
  382. checked: true,
  383. disabled: true,
  384. label: 'selection data 1',
  385. },
  386. ]
  387. const wrapper = await renderTable(
  388. {
  389. headers: ['label'],
  390. items,
  391. hasCheckboxColumn: true,
  392. totalItems: 100,
  393. caption: 'Table caption',
  394. },
  395. { form: true, vModel: { checkedRows } },
  396. )
  397. const checkboxes = wrapper.getAllByRole('checkbox')
  398. expect(checkboxes).toHaveLength(3)
  399. expect(checkboxes[1]).toBeDisabled()
  400. expect(checkboxes[1]).not.toBeChecked()
  401. expect(checkboxes[2]).toHaveAttribute('value', 'true')
  402. await wrapper.events.click(checkboxes[1])
  403. expect(checkedRows.value).toEqual([])
  404. await wrapper.events.click(checkboxes[0])
  405. expect(checkedRows.value).toEqual([])
  406. })
  407. // TODO: ...
  408. // it.todo('supports sorting')
  409. // it.todo('supports grouping')
  410. // it.todo('informs the user about reached limits')
  411. // it.todo('informs the user about table end')
  412. })