LogsPanel.test.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { render, screen } from '@testing-library/react';
  2. import React, { ComponentProps } from 'react';
  3. import { LoadingState, MutableDataFrame, FieldType, LogsSortOrder } from '@grafana/data';
  4. import { LogsPanel } from './LogsPanel';
  5. type LogsPanelProps = ComponentProps<typeof LogsPanel>;
  6. describe('LogsPanel', () => {
  7. describe('when returned series include common labels', () => {
  8. const seriesWithCommonLabels = [
  9. new MutableDataFrame({
  10. fields: [
  11. {
  12. name: 'time',
  13. type: FieldType.time,
  14. values: ['2019-04-26T09:28:11.352440161Z', '2019-04-26T14:42:50.991981292Z'],
  15. },
  16. {
  17. name: 'message',
  18. type: FieldType.string,
  19. values: [
  20. 't=2019-04-26T11:05:28+0200 lvl=info msg="Initializing DatasourceCacheService" logger=server',
  21. 't=2019-04-26T16:42:50+0200 lvl=eror msg="new token…t unhashed token=56d9fdc5c8b7400bd51b060eea8ca9d7',
  22. ],
  23. labels: {
  24. app: 'common_app',
  25. job: 'common_job',
  26. },
  27. },
  28. ],
  29. }),
  30. ];
  31. it('shows common labels when showCommonLabels is set to true', () => {
  32. setup({ data: { series: seriesWithCommonLabels }, options: { showCommonLabels: true } });
  33. expect(screen.getByText(/common labels:/i)).toBeInTheDocument();
  34. expect(screen.getByText(/common_app/i)).toBeInTheDocument();
  35. expect(screen.getByText(/common_job/i)).toBeInTheDocument();
  36. });
  37. it('shows common labels on top when descending sort order', () => {
  38. const { container } = setup({
  39. data: { series: seriesWithCommonLabels },
  40. options: { showCommonLabels: true, sortOrder: LogsSortOrder.Descending },
  41. });
  42. expect(container.firstChild?.childNodes[0].textContent).toMatch(/^Common labels:common_appcommon_job/);
  43. });
  44. it('shows common labels on bottom when ascending sort order', () => {
  45. const { container } = setup({
  46. data: { series: seriesWithCommonLabels },
  47. options: { showCommonLabels: true, sortOrder: LogsSortOrder.Ascending },
  48. });
  49. expect(container.firstChild?.childNodes[0].textContent).toMatch(/Common labels:common_appcommon_job$/);
  50. });
  51. it('does not show common labels when showCommonLabels is set to false', () => {
  52. setup({ data: { series: seriesWithCommonLabels }, options: { showCommonLabels: false } });
  53. expect(screen.queryByText(/common labels:/i)).not.toBeInTheDocument();
  54. expect(screen.queryByText(/common_app/i)).not.toBeInTheDocument();
  55. expect(screen.queryByText(/common_job/i)).not.toBeInTheDocument();
  56. });
  57. });
  58. describe('when returned series does not include common labels', () => {
  59. const seriesWithoutCommonLabels = [
  60. new MutableDataFrame({
  61. fields: [
  62. {
  63. name: 'time',
  64. type: FieldType.time,
  65. values: ['2019-04-26T09:28:11.352440161Z', '2019-04-26T14:42:50.991981292Z'],
  66. },
  67. {
  68. name: 'message',
  69. type: FieldType.string,
  70. values: [
  71. 't=2019-04-26T11:05:28+0200 lvl=info msg="Initializing DatasourceCacheService" logger=server',
  72. 't=2019-04-26T16:42:50+0200 lvl=eror msg="new token…t unhashed token=56d9fdc5c8b7400bd51b060eea8ca9d7',
  73. ],
  74. },
  75. ],
  76. }),
  77. ];
  78. it('shows (no common labels) when showCommonLabels is set to true', () => {
  79. setup({ data: { series: seriesWithoutCommonLabels }, options: { showCommonLabels: true } });
  80. expect(screen.getByText(/common labels:/i)).toBeInTheDocument();
  81. expect(screen.getByText(/(no common labels)/i)).toBeInTheDocument();
  82. });
  83. it('does not show common labels when showCommonLabels is set to false', () => {
  84. setup({ data: { series: seriesWithoutCommonLabels }, options: { showCommonLabels: false } });
  85. expect(screen.queryByText(/common labels:/i)).not.toBeInTheDocument();
  86. expect(screen.queryByText(/(no common labels)/i)).not.toBeInTheDocument();
  87. });
  88. });
  89. });
  90. const setup = (propsOverrides?: {}) => {
  91. const props = {
  92. data: {
  93. error: undefined,
  94. request: {
  95. panelId: 4,
  96. dashboardId: 123,
  97. app: 'dashboard',
  98. requestId: 'A',
  99. timezone: 'browser',
  100. interval: '30s',
  101. intervalMs: 30000,
  102. maxDataPoints: 823,
  103. targets: [],
  104. range: {},
  105. },
  106. series: [],
  107. state: LoadingState.Done,
  108. },
  109. timeZone: 'utc',
  110. options: {},
  111. title: 'Logs panel',
  112. id: 1,
  113. ...propsOverrides,
  114. } as unknown as LogsPanelProps;
  115. return render(<LogsPanel {...props} />);
  116. };