TableContainer.test.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { screen, render, within } from '@testing-library/react';
  2. import React from 'react';
  3. import { DataFrame, toDataFrame, FieldType, InternalTimeZones } from '@grafana/data';
  4. import { ExploreId } from 'app/types/explore';
  5. import { TableContainer } from './TableContainer';
  6. function getTable(): HTMLElement {
  7. return screen.getAllByRole('table')[0];
  8. }
  9. function getRowsData(rows: HTMLElement[]): Object[] {
  10. let content = [];
  11. for (let i = 1; i < rows.length; i++) {
  12. content.push({
  13. time: within(rows[i]).getByText(/2021*/).textContent,
  14. text: within(rows[i]).getByText(/test_string_*/).textContent,
  15. });
  16. }
  17. return content;
  18. }
  19. const dataFrame = toDataFrame({
  20. name: 'A',
  21. fields: [
  22. {
  23. name: 'time',
  24. type: FieldType.time,
  25. values: [1609459200000, 1609470000000, 1609462800000, 1609466400000],
  26. config: {
  27. custom: {
  28. filterable: false,
  29. },
  30. },
  31. },
  32. {
  33. name: 'text',
  34. type: FieldType.string,
  35. values: ['test_string_1', 'test_string_2', 'test_string_3', 'test_string_4'],
  36. config: {
  37. custom: {
  38. filterable: false,
  39. },
  40. },
  41. },
  42. ],
  43. });
  44. const defaultProps = {
  45. exploreId: ExploreId.left as ExploreId,
  46. loading: false,
  47. width: 800,
  48. onCellFilterAdded: jest.fn(),
  49. tableResult: dataFrame,
  50. splitOpen: (() => {}) as any,
  51. range: {} as any,
  52. timeZone: InternalTimeZones.utc,
  53. };
  54. describe('TableContainer', () => {
  55. it('should render component', () => {
  56. render(<TableContainer {...defaultProps} />);
  57. expect(getTable()).toBeInTheDocument();
  58. const rows = within(getTable()).getAllByRole('row');
  59. expect(rows).toHaveLength(5);
  60. expect(getRowsData(rows)).toEqual([
  61. { time: '2021-01-01 00:00:00', text: 'test_string_1' },
  62. { time: '2021-01-01 03:00:00', text: 'test_string_2' },
  63. { time: '2021-01-01 01:00:00', text: 'test_string_3' },
  64. { time: '2021-01-01 02:00:00', text: 'test_string_4' },
  65. ]);
  66. });
  67. it('should render 0 series returned on no items', () => {
  68. const emptyFrames = {
  69. name: 'TableResultName',
  70. fields: [],
  71. length: 0,
  72. } as DataFrame;
  73. render(<TableContainer {...defaultProps} tableResult={emptyFrames} />);
  74. expect(screen.getByText('0 series returned')).toBeInTheDocument();
  75. });
  76. it('should update time when timezone changes', () => {
  77. const { rerender } = render(<TableContainer {...defaultProps} />);
  78. const rowsBeforeChange = within(getTable()).getAllByRole('row');
  79. expect(getRowsData(rowsBeforeChange)).toEqual([
  80. { time: '2021-01-01 00:00:00', text: 'test_string_1' },
  81. { time: '2021-01-01 03:00:00', text: 'test_string_2' },
  82. { time: '2021-01-01 01:00:00', text: 'test_string_3' },
  83. { time: '2021-01-01 02:00:00', text: 'test_string_4' },
  84. ]);
  85. rerender(<TableContainer {...defaultProps} timeZone="cest" />);
  86. const rowsAfterChange = within(getTable()).getAllByRole('row');
  87. expect(getRowsData(rowsAfterChange)).toEqual([
  88. { time: '2020-12-31 19:00:00', text: 'test_string_1' },
  89. { time: '2020-12-31 22:00:00', text: 'test_string_2' },
  90. { time: '2020-12-31 20:00:00', text: 'test_string_3' },
  91. { time: '2020-12-31 21:00:00', text: 'test_string_4' },
  92. ]);
  93. });
  94. });