LiveLogs.test.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { LogLevel, LogRowModel, MutableDataFrame } from '@grafana/data';
  4. import { LiveLogsWithTheme } from './LiveLogs';
  5. const setup = (rows: LogRowModel[]) =>
  6. render(
  7. <LiveLogsWithTheme
  8. logRows={rows}
  9. timeZone={'utc'}
  10. stopLive={() => {}}
  11. onPause={() => {}}
  12. onResume={() => {}}
  13. isPaused={true}
  14. />
  15. );
  16. const makeLog = (overrides: Partial<LogRowModel>): LogRowModel => {
  17. const uid = overrides.uid || '1';
  18. const entry = `log message ${uid}`;
  19. return {
  20. uid,
  21. entryFieldIndex: 0,
  22. rowIndex: 0,
  23. dataFrame: new MutableDataFrame(),
  24. logLevel: LogLevel.debug,
  25. entry,
  26. hasAnsi: false,
  27. hasUnescapedContent: false,
  28. labels: {},
  29. raw: entry,
  30. timeFromNow: '',
  31. timeEpochMs: 1,
  32. timeEpochNs: '1000000',
  33. timeLocal: '',
  34. timeUtc: '',
  35. ...overrides,
  36. };
  37. };
  38. describe('LiveLogs', () => {
  39. it('renders logs', () => {
  40. setup([makeLog({ uid: '1' }), makeLog({ uid: '2' }), makeLog({ uid: '3' })]);
  41. expect(screen.getByRole('cell', { name: 'log message 1' })).toBeInTheDocument();
  42. expect(screen.getByRole('cell', { name: 'log message 2' })).toBeInTheDocument();
  43. expect(screen.getByRole('cell', { name: 'log message 3' })).toBeInTheDocument();
  44. });
  45. it('renders new logs only when not paused', () => {
  46. const { rerender } = setup([makeLog({ uid: '1' }), makeLog({ uid: '2' }), makeLog({ uid: '3' })]);
  47. rerender(
  48. <LiveLogsWithTheme
  49. logRows={[makeLog({ uid: '4' }), makeLog({ uid: '5' }), makeLog({ uid: '6' })]}
  50. timeZone={'utc'}
  51. stopLive={() => {}}
  52. onPause={() => {}}
  53. onResume={() => {}}
  54. isPaused={true}
  55. />
  56. );
  57. expect(screen.getByRole('cell', { name: 'log message 1' })).toBeInTheDocument();
  58. expect(screen.getByRole('cell', { name: 'log message 2' })).toBeInTheDocument();
  59. expect(screen.getByRole('cell', { name: 'log message 3' })).toBeInTheDocument();
  60. expect(screen.queryByRole('cell', { name: 'log message 4' })).not.toBeInTheDocument();
  61. expect(screen.queryByRole('cell', { name: 'log message 5' })).not.toBeInTheDocument();
  62. expect(screen.queryByRole('cell', { name: 'log message 6' })).not.toBeInTheDocument();
  63. rerender(
  64. <LiveLogsWithTheme
  65. logRows={[makeLog({ uid: '4' }), makeLog({ uid: '5' }), makeLog({ uid: '6' })]}
  66. timeZone={'utc'}
  67. stopLive={() => {}}
  68. onPause={() => {}}
  69. onResume={() => {}}
  70. isPaused={false}
  71. />
  72. );
  73. expect(screen.getByRole('cell', { name: 'log message 4' })).toBeInTheDocument();
  74. expect(screen.getByRole('cell', { name: 'log message 5' })).toBeInTheDocument();
  75. expect(screen.getByRole('cell', { name: 'log message 6' })).toBeInTheDocument();
  76. });
  77. it('renders ansi logs', () => {
  78. setup([
  79. makeLog({ uid: '1' }),
  80. makeLog({ hasAnsi: true, raw: 'log message \u001B[31m2\u001B[0m', uid: '2' }),
  81. makeLog({ hasAnsi: true, raw: 'log message \u001B[33m3\u001B[0m', uid: '3' }),
  82. ]);
  83. expect(screen.getByRole('cell', { name: 'log message 1' })).toBeInTheDocument();
  84. expect(screen.getByRole('cell', { name: 'log message 2' })).toBeInTheDocument();
  85. expect(screen.getByRole('cell', { name: 'log message 3' })).toBeInTheDocument();
  86. const logList = screen.getAllByTestId('ansiLogLine');
  87. expect(logList).toHaveLength(2);
  88. expect(logList[0]).toHaveAttribute('style', 'color: rgb(204, 0, 0);');
  89. expect(logList[1]).toHaveAttribute('style', 'color: rgb(204, 102, 0);');
  90. });
  91. });