Logs.test.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { render, screen, fireEvent } from '@testing-library/react';
  2. import React from 'react';
  3. import { LoadingState, LogLevel, LogRowModel, MutableDataFrame, toUtc } from '@grafana/data';
  4. import { ExploreId } from 'app/types';
  5. import { Logs } from './Logs';
  6. describe('Logs', () => {
  7. const setup = (propOverrides?: object) => {
  8. const rows = [
  9. makeLog({ uid: '1', timeEpochMs: 1 }),
  10. makeLog({ uid: '2', timeEpochMs: 2 }),
  11. makeLog({ uid: '3', timeEpochMs: 3 }),
  12. ];
  13. return render(
  14. <Logs
  15. exploreId={ExploreId.left}
  16. logRows={rows}
  17. timeZone={'utc'}
  18. width={50}
  19. loading={false}
  20. loadingState={LoadingState.Done}
  21. absoluteRange={{
  22. from: toUtc('2019-01-01 10:00:00').valueOf(),
  23. to: toUtc('2019-01-01 16:00:00').valueOf(),
  24. }}
  25. addResultsToCache={() => {}}
  26. onChangeTime={() => {}}
  27. clearCache={() => {}}
  28. getFieldLinks={() => {
  29. return [];
  30. }}
  31. />
  32. );
  33. };
  34. beforeEach(() => {
  35. jest.useFakeTimers('modern');
  36. });
  37. afterEach(() => {
  38. jest.useRealTimers();
  39. });
  40. it('should render logs', () => {
  41. setup();
  42. const logsSection = screen.getByTestId('logRows');
  43. let logRows = logsSection.querySelectorAll('tr');
  44. expect(logRows.length).toBe(3);
  45. expect(logRows[0].textContent).toContain('log message 3');
  46. expect(logRows[2].textContent).toContain('log message 1');
  47. });
  48. it('should flip the order', () => {
  49. setup();
  50. const oldestFirstSelection = screen.getByLabelText('Oldest first');
  51. fireEvent.click(oldestFirstSelection);
  52. jest.advanceTimersByTime(1000);
  53. const logsSection = screen.getByTestId('logRows');
  54. let logRows = logsSection.querySelectorAll('tr');
  55. expect(logRows.length).toBe(3);
  56. expect(logRows[0].textContent).toContain('log message 1');
  57. expect(logRows[2].textContent).toContain('log message 3');
  58. });
  59. });
  60. const makeLog = (overrides: Partial<LogRowModel>): LogRowModel => {
  61. const uid = overrides.uid || '1';
  62. const entry = `log message ${uid}`;
  63. return {
  64. uid,
  65. entryFieldIndex: 0,
  66. rowIndex: 0,
  67. dataFrame: new MutableDataFrame(),
  68. logLevel: LogLevel.debug,
  69. entry,
  70. hasAnsi: false,
  71. hasUnescapedContent: false,
  72. labels: {},
  73. raw: entry,
  74. timeFromNow: '',
  75. timeEpochMs: 1,
  76. timeEpochNs: '1000000',
  77. timeLocal: '',
  78. timeUtc: '',
  79. ...overrides,
  80. };
  81. };