TableHead.spec.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import TableHead from '../../grid/TableHead';
  2. import { fireEvent, render } from '@testing-library/react';
  3. import { vi } from 'vitest';
  4. describe('Test Table Head', () => {
  5. // it('Test no checkbox', () => {
  6. // const res = render(
  7. // <TableHead
  8. // colDefinitions={[]}
  9. // numSelected={0}
  10. // order={'desc'}
  11. // orderBy={'id'}
  12. // onSelectAllClick={() => {}}
  13. // handleSort={() => {}}
  14. // rowCount={0}
  15. // openCheckBox={false}
  16. // />
  17. // );
  18. // expect(res.getAllByText('.table-cell').length).toEqual(0);
  19. // });
  20. it('Test checkbox open', () => {
  21. const selectAllSpy = vi.fn();
  22. const res = render(
  23. <div>
  24. <TableHead
  25. colDefinitions={[]}
  26. numSelected={10}
  27. order={'desc'}
  28. orderBy={'id'}
  29. onSelectAllClick={selectAllSpy}
  30. handleSort={() => {}}
  31. rowCount={10}
  32. openCheckBox={true}
  33. />
  34. </div>
  35. );
  36. // screen.debug();
  37. const checkboxes: any = res.getAllByRole('checkbox');
  38. expect(checkboxes.length).toEqual(1);
  39. fireEvent.click(checkboxes[0]);
  40. expect(selectAllSpy).toBeCalledTimes(1);
  41. expect(checkboxes[0].checked).toBe(true);
  42. });
  43. it('Test header cells', () => {
  44. const onRequestSortSpy = vi.fn();
  45. const colDefinitions = [
  46. {
  47. id: 'id',
  48. numeric: false,
  49. disablePadding: true,
  50. label: 'id',
  51. },
  52. {
  53. id: 'name',
  54. numeric: false,
  55. disablePadding: true,
  56. label: 'name',
  57. },
  58. ];
  59. const res = render(
  60. <TableHead
  61. colDefinitions={colDefinitions}
  62. numSelected={10}
  63. order={'desc'}
  64. orderBy={'id'}
  65. onSelectAllClick={() => {}}
  66. handleSort={onRequestSortSpy}
  67. rowCount={10}
  68. openCheckBox={false}
  69. />
  70. );
  71. const headerCells = res.getAllByRole('cell');
  72. expect(headerCells.length).toEqual(colDefinitions.length);
  73. expect(headerCells[0].textContent).toContain('id');
  74. expect(headerCells[0].textContent).toContain('sorted descending');
  75. const sortButton = res.getAllByRole('button');
  76. fireEvent.click(sortButton[0]);
  77. expect(onRequestSortSpy).toBeCalledTimes(1);
  78. fireEvent.click(sortButton[0]);
  79. expect(onRequestSortSpy).toBeCalledTimes(2);
  80. });
  81. });