PromQueryBuilderOptions.test.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { selectOptionInTest } from 'test/helpers/selectOptionInTest';
  4. import { CoreApp } from '@grafana/data';
  5. import { PromQuery } from '../../types';
  6. import { getQueryWithDefaults } from '../state';
  7. import { PromQueryBuilderOptions } from './PromQueryBuilderOptions';
  8. describe('PromQueryBuilderOptions', () => {
  9. it('Can change query type', async () => {
  10. const { props } = setup();
  11. screen.getByTitle('Click to edit options').click();
  12. expect(screen.getByLabelText('Range')).toBeChecked();
  13. screen.getByLabelText('Instant').click();
  14. expect(props.onChange).toHaveBeenCalledWith({
  15. ...props.query,
  16. instant: true,
  17. range: false,
  18. exemplar: false,
  19. });
  20. });
  21. it('Legend format default to Auto', async () => {
  22. setup();
  23. expect(screen.getByText('Legend: Auto')).toBeInTheDocument();
  24. });
  25. it('Can change legend format to verbose', async () => {
  26. const { props } = setup();
  27. screen.getByTitle('Click to edit options').click();
  28. let legendModeSelect = screen.getByText('Auto').parentElement!;
  29. legendModeSelect.click();
  30. await selectOptionInTest(legendModeSelect as HTMLElement, 'Verbose');
  31. expect(props.onChange).toHaveBeenCalledWith({
  32. ...props.query,
  33. legendFormat: '',
  34. });
  35. });
  36. it('Can change legend format to custom', async () => {
  37. const { props } = setup();
  38. screen.getByTitle('Click to edit options').click();
  39. let legendModeSelect = screen.getByText('Auto').parentElement!;
  40. legendModeSelect.click();
  41. await selectOptionInTest(legendModeSelect as HTMLElement, 'Custom');
  42. expect(props.onChange).toHaveBeenCalledWith({
  43. ...props.query,
  44. legendFormat: '{{label_name}}',
  45. });
  46. });
  47. it('Handle defaults with undefined range', async () => {
  48. setup(getQueryWithDefaults({ refId: 'A', expr: '', range: undefined, instant: true }, CoreApp.Dashboard));
  49. expect(screen.getByText('Type: Instant')).toBeInTheDocument();
  50. });
  51. });
  52. function setup(queryOverrides: Partial<PromQuery> = {}) {
  53. const props = {
  54. query: {
  55. ...getQueryWithDefaults({ refId: 'A' } as PromQuery, CoreApp.PanelEditor),
  56. ...queryOverrides,
  57. },
  58. onRunQuery: jest.fn(),
  59. onChange: jest.fn(),
  60. uiOptions: {
  61. exemplars: true,
  62. type: true,
  63. format: true,
  64. minStep: true,
  65. legend: true,
  66. resolution: true,
  67. },
  68. };
  69. const { container } = render(<PromQueryBuilderOptions {...props} />);
  70. return { container, props };
  71. }