checkSimpleTableContent.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { within } from '@testing-library/vue'
  3. import type { ExtendedRenderResult } from '#tests/support/components/renderComponent.ts'
  4. export const checkSimpleTableHeader = (
  5. view: ExtendedRenderResult,
  6. tableHeaders: string[],
  7. tableLabel?: string,
  8. ) => {
  9. const table = within(view.getByRole('table', { name: tableLabel }))
  10. tableHeaders.forEach((header) => {
  11. expect(
  12. table.getByRole('columnheader', { name: header }),
  13. ).toBeInTheDocument()
  14. })
  15. }
  16. export const checkSimpleTableContent = (
  17. view: ExtendedRenderResult,
  18. rowContents: (string | string[])[][],
  19. tableLabel?: string,
  20. ) => {
  21. const table = within(view.getByRole('table', { name: tableLabel }))
  22. const rows = table.getAllByRole('row')
  23. expect(rows).toHaveLength(rowContents.length + 1) // +1 for header row
  24. rows.forEach((row, index) => {
  25. const cells = within(row).queryAllByRole('cell')
  26. if (!cells.length)
  27. cells.forEach((cell, cellIndex) => {
  28. if (!cell) return
  29. const content = rowContents[index][cellIndex]
  30. if (content) {
  31. const withinCell = within(cell)
  32. if (Array.isArray(content)) {
  33. const dateTime = withinCell.getByLabelText(content[0])
  34. expect(dateTime).toHaveTextContent(content[1])
  35. } else {
  36. expect(withinCell.getByText(content)).toBeInTheDocument()
  37. }
  38. }
  39. })
  40. })
  41. }