LogsNavigation.test.tsx 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { render, screen, fireEvent } from '@testing-library/react';
  2. import React, { ComponentProps } from 'react';
  3. import { LogsSortOrder } from '@grafana/data';
  4. import LogsNavigation from './LogsNavigation';
  5. // we have to mock out reportInteraction, otherwise it crashes the test.
  6. jest.mock('@grafana/runtime', () => ({
  7. ...jest.requireActual('@grafana/runtime'),
  8. reportInteraction: () => null,
  9. }));
  10. type LogsNavigationProps = ComponentProps<typeof LogsNavigation>;
  11. const defaultProps: LogsNavigationProps = {
  12. absoluteRange: { from: 1637319381811, to: 1637322981811 },
  13. timeZone: 'local',
  14. queries: [],
  15. loading: false,
  16. logsSortOrder: undefined,
  17. visibleRange: { from: 1637322959000, to: 1637322981811 },
  18. onChangeTime: jest.fn(),
  19. scrollToTopLogs: jest.fn(),
  20. addResultsToCache: jest.fn(),
  21. clearCache: jest.fn(),
  22. };
  23. const setup = (propOverrides?: object) => {
  24. const props = {
  25. ...defaultProps,
  26. ...propOverrides,
  27. };
  28. return render(<LogsNavigation {...props} />);
  29. };
  30. describe('LogsNavigation', () => {
  31. it('should always render 3 navigation buttons', () => {
  32. setup();
  33. expect(screen.getByTestId('newerLogsButton')).toBeInTheDocument();
  34. expect(screen.getByTestId('olderLogsButton')).toBeInTheDocument();
  35. expect(screen.getByTestId('scrollToTop')).toBeInTheDocument();
  36. });
  37. it('should render 3 navigation buttons in correct order when default logs order', () => {
  38. const { container } = setup();
  39. const expectedOrder = ['newerLogsButton', 'olderLogsButton', 'scrollToTop'];
  40. const elements = container.querySelectorAll(
  41. '[data-testid=newerLogsButton],[data-testid=olderLogsButton],[data-testid=scrollToTop]'
  42. );
  43. expect(Array.from(elements).map((el) => el.getAttribute('data-testid'))).toMatchObject(expectedOrder);
  44. });
  45. it('should render 3 navigation buttons in correct order when flipped logs order', () => {
  46. const { container } = setup({ logsSortOrder: LogsSortOrder.Ascending });
  47. const expectedOrder = ['olderLogsButton', 'newerLogsButton', 'scrollToTop'];
  48. const elements = container.querySelectorAll(
  49. '[data-testid=newerLogsButton],[data-testid=olderLogsButton],[data-testid=scrollToTop]'
  50. );
  51. expect(Array.from(elements).map((el) => el.getAttribute('data-testid'))).toMatchObject(expectedOrder);
  52. });
  53. it('should disable fetch buttons when logs are loading', () => {
  54. setup({ loading: true });
  55. const olderLogsButton = screen.getByTestId('olderLogsButton');
  56. const newerLogsButton = screen.getByTestId('newerLogsButton');
  57. expect(olderLogsButton).toBeDisabled();
  58. expect(newerLogsButton).toBeDisabled();
  59. });
  60. it('should render logs navigation pages section', () => {
  61. setup();
  62. expect(screen.getByTestId('logsNavigationPages')).toBeInTheDocument();
  63. });
  64. it('should correctly request older logs when flipped order', () => {
  65. const onChangeTimeMock = jest.fn();
  66. const { rerender } = setup({ onChangeTime: onChangeTimeMock });
  67. fireEvent.click(screen.getByTestId('olderLogsButton'));
  68. expect(onChangeTimeMock).toHaveBeenCalledWith({ from: 1637319359000, to: 1637322959000 });
  69. rerender(
  70. <LogsNavigation
  71. {...defaultProps}
  72. absoluteRange={{ from: 1637319359000, to: 1637322959000 }}
  73. visibleRange={{ from: 1637322938000, to: 1637322959000 }}
  74. onChangeTime={onChangeTimeMock}
  75. logsSortOrder={LogsSortOrder.Ascending}
  76. />
  77. );
  78. fireEvent.click(screen.getByTestId('olderLogsButton'));
  79. expect(onChangeTimeMock).toHaveBeenCalledWith({ from: 1637319338000, to: 1637322938000 });
  80. });
  81. });