CommonSimpleTable.spec.ts 6.8 KB

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