ExploreQueryInspector.test.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { render, screen, fireEvent } from '@testing-library/react';
  2. import React, { ComponentProps } from 'react';
  3. import { Observable } from 'rxjs';
  4. import { TimeRange, LoadingState, InternalTimeZones } from '@grafana/data';
  5. import { ExploreId } from 'app/types';
  6. import { ExploreQueryInspector } from './ExploreQueryInspector';
  7. type ExploreQueryInspectorProps = ComponentProps<typeof ExploreQueryInspector>;
  8. jest.mock('../inspector/styles', () => ({
  9. getPanelInspectorStyles: () => ({}),
  10. }));
  11. jest.mock('app/core/services/backend_srv', () => ({
  12. backendSrv: {
  13. getInspectorStream: () =>
  14. new Observable((subscriber) => {
  15. subscriber.next(response());
  16. subscriber.next(response(true));
  17. }) as any,
  18. },
  19. }));
  20. jest.mock('app/core/services/context_srv', () => ({
  21. contextSrv: {
  22. user: { orgId: 1 },
  23. },
  24. }));
  25. const setup = (propOverrides = {}) => {
  26. const props: ExploreQueryInspectorProps = {
  27. loading: false,
  28. width: 100,
  29. exploreId: ExploreId.left,
  30. onClose: jest.fn(),
  31. timeZone: InternalTimeZones.utc,
  32. queryResponse: {
  33. state: LoadingState.Done,
  34. series: [],
  35. timeRange: {} as TimeRange,
  36. graphFrames: [],
  37. logsFrames: [],
  38. tableFrames: [],
  39. traceFrames: [],
  40. nodeGraphFrames: [],
  41. graphResult: null,
  42. logsResult: null,
  43. tableResult: null,
  44. },
  45. runQueries: jest.fn(),
  46. ...propOverrides,
  47. };
  48. return render(<ExploreQueryInspector {...props} />);
  49. };
  50. describe('ExploreQueryInspector', () => {
  51. it('should render closable drawer component', () => {
  52. setup();
  53. expect(screen.getByTitle(/close query inspector/i)).toBeInTheDocument();
  54. });
  55. it('should render 4 Tabs if queryResponse has no error', () => {
  56. setup();
  57. expect(screen.getAllByLabelText(/tab/i)).toHaveLength(4);
  58. });
  59. it('should render 5 Tabs if queryResponse has error', () => {
  60. setup({ queryResponse: { error: 'Bad gateway' } });
  61. expect(screen.getAllByLabelText(/tab/i)).toHaveLength(5);
  62. });
  63. it('should display query data when click on expanding', () => {
  64. setup();
  65. fireEvent.click(screen.getByLabelText(/tab query/i));
  66. fireEvent.click(screen.getByText(/expand all/i));
  67. expect(screen.getByText(/very unique test value/i)).toBeInTheDocument();
  68. });
  69. });
  70. const response = (hideFromInspector = false) => ({
  71. status: 1,
  72. statusText: '',
  73. ok: true,
  74. headers: {} as any,
  75. redirected: false,
  76. type: 'basic',
  77. url: '',
  78. request: {} as any,
  79. data: {
  80. test: {
  81. testKey: 'Very unique test value',
  82. },
  83. },
  84. config: {
  85. url: '',
  86. hideFromInspector,
  87. },
  88. });