TableHeaderCell.spec.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import React from 'react';
  2. import { screen, within } from '@testing-library/react';
  3. import { render } from 'lib/testHelpers';
  4. import TableHeaderCell, {
  5. TableHeaderCellProps,
  6. } from 'components/common/table/TableHeaderCell/TableHeaderCell';
  7. import { TopicColumnsToSort } from 'generated-sources';
  8. import theme from 'theme/theme';
  9. import userEvent from '@testing-library/user-event';
  10. const SPACE_KEY = ' ';
  11. const testTitle = 'test title';
  12. const testPreviewText = 'test preview text';
  13. const handleOrderBy = jest.fn();
  14. const onPreview = jest.fn();
  15. const sortIconTitle = 'Sort icon';
  16. describe('TableHeaderCell', () => {
  17. const setupComponent = (props: Partial<TableHeaderCellProps> = {}) =>
  18. render(
  19. <table>
  20. <thead>
  21. <tr>
  22. <TableHeaderCell {...props} />;
  23. </tr>
  24. </thead>
  25. </table>
  26. );
  27. it('renders without props', () => {
  28. setupComponent();
  29. expect(screen.getByRole('columnheader')).toBeInTheDocument();
  30. });
  31. it('renders with title & preview text', () => {
  32. setupComponent({
  33. title: testTitle,
  34. previewText: testPreviewText,
  35. });
  36. const columnheader = screen.getByRole('columnheader');
  37. expect(within(columnheader).getByText(testTitle)).toBeInTheDocument();
  38. expect(within(columnheader).getByText(testPreviewText)).toBeInTheDocument();
  39. });
  40. it('renders with orderable props', () => {
  41. setupComponent({
  42. title: testTitle,
  43. orderBy: TopicColumnsToSort.NAME,
  44. orderValue: TopicColumnsToSort.NAME,
  45. handleOrderBy,
  46. });
  47. const columnheader = screen.getByRole('columnheader');
  48. const title = within(columnheader).getByRole('button');
  49. expect(title).toBeInTheDocument();
  50. expect(title).toHaveTextContent(testTitle);
  51. expect(title).toHaveStyle(`color: ${theme.table.th.color.active};`);
  52. expect(title).toHaveStyle('cursor: pointer;');
  53. });
  54. it('renders click on title triggers handler', () => {
  55. setupComponent({
  56. title: testTitle,
  57. orderBy: TopicColumnsToSort.NAME,
  58. orderValue: TopicColumnsToSort.NAME,
  59. handleOrderBy,
  60. });
  61. const columnheader = screen.getByRole('columnheader');
  62. const title = within(columnheader).getByRole('button');
  63. userEvent.click(title);
  64. expect(handleOrderBy.mock.calls.length).toBe(1);
  65. });
  66. it('renders space on title triggers handler', () => {
  67. setupComponent({
  68. title: testTitle,
  69. orderBy: TopicColumnsToSort.NAME,
  70. orderValue: TopicColumnsToSort.NAME,
  71. handleOrderBy,
  72. });
  73. const columnheader = screen.getByRole('columnheader');
  74. const title = within(columnheader).getByRole('button');
  75. userEvent.type(title, SPACE_KEY);
  76. // userEvent.type clicks and only then presses space
  77. expect(handleOrderBy.mock.calls.length).toBe(2);
  78. });
  79. it('click on preview triggers handler', () => {
  80. setupComponent({
  81. title: testTitle,
  82. previewText: testPreviewText,
  83. onPreview,
  84. });
  85. const columnheader = screen.getByRole('columnheader');
  86. const preview = within(columnheader).getByRole('button');
  87. userEvent.click(preview);
  88. expect(onPreview.mock.calls.length).toBe(1);
  89. });
  90. it('click on preview triggers handler', () => {
  91. setupComponent({
  92. title: testTitle,
  93. previewText: testPreviewText,
  94. onPreview,
  95. });
  96. const columnheader = screen.getByRole('columnheader');
  97. const preview = within(columnheader).getByRole('button');
  98. userEvent.type(preview, SPACE_KEY);
  99. // userEvent.type clicks and only then presses space
  100. expect(onPreview.mock.calls.length).toBe(2);
  101. });
  102. it('renders without sort indication', () => {
  103. setupComponent({
  104. title: testTitle,
  105. orderBy: TopicColumnsToSort.NAME,
  106. });
  107. const columnheader = screen.getByRole('columnheader');
  108. const title = within(columnheader).getByText(testTitle);
  109. expect(within(title).queryByTitle(sortIconTitle)).not.toBeInTheDocument();
  110. expect(title).toHaveStyle('cursor: default;');
  111. });
  112. it('renders with hightlighted title when orderBy and orderValue are equal', () => {
  113. setupComponent({
  114. title: testTitle,
  115. orderBy: TopicColumnsToSort.NAME,
  116. orderValue: TopicColumnsToSort.NAME,
  117. handleOrderBy: jest.fn(),
  118. });
  119. const columnheader = screen.getByRole('columnheader');
  120. const title = within(columnheader).getByText(testTitle);
  121. expect(title).toHaveStyle(`color: ${theme.table.th.color.active};`);
  122. });
  123. it('renders without hightlighted title when orderBy and orderValue are not equal', () => {
  124. setupComponent({
  125. title: testTitle,
  126. orderBy: TopicColumnsToSort.NAME,
  127. orderValue: TopicColumnsToSort.OUT_OF_SYNC_REPLICAS,
  128. handleOrderBy: jest.fn(),
  129. });
  130. const columnheader = screen.getByRole('columnheader');
  131. const title = within(columnheader).getByText(testTitle);
  132. expect(title).toHaveStyle(`color: ${theme.table.th.color.normal}`);
  133. });
  134. it('renders with default (primary) theme', () => {
  135. setupComponent({
  136. title: testTitle,
  137. });
  138. const columnheader = screen.getByRole('columnheader');
  139. const title = within(columnheader).getByText(testTitle);
  140. expect(title).toHaveStyle(
  141. `background: ${theme.table.th.backgroundColor.normal};`
  142. );
  143. });
  144. });