PromQueryBuilderContainer.test.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { render, screen, waitFor } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { PrometheusDatasource } from '../../datasource';
  5. import PromQlLanguageProvider from '../../language_provider';
  6. import { EmptyLanguageProviderMock } from '../../language_provider.mock';
  7. import { PromQuery } from '../../types';
  8. import { addOperation } from '../shared/OperationList.testUtils';
  9. import { getOperationParamId } from '../shared/operationUtils';
  10. import { PromQueryBuilderContainer } from './PromQueryBuilderContainer';
  11. describe('PromQueryBuilderContainer', () => {
  12. it('translates query between string and model', async () => {
  13. const { props } = setup({ expr: 'rate(metric_test{job="testjob"}[$__rate_interval])' });
  14. expect(screen.getByText('metric_test')).toBeInTheDocument();
  15. await addOperation('Range functions', 'Rate');
  16. expect(props.onChange).toBeCalledWith({
  17. expr: 'rate(metric_test{job="testjob"}[$__rate_interval])',
  18. refId: 'A',
  19. });
  20. });
  21. it('Can add rest param', async () => {
  22. const { container } = setup({ expr: 'sum(ALERTS)' });
  23. await userEvent.click(screen.getByTestId('operations.0.add-rest-param'));
  24. waitFor(() => {
  25. expect(container.querySelector(`${getOperationParamId(0, 0)}`)).toBeInTheDocument();
  26. });
  27. });
  28. });
  29. function setup(queryOverrides: Partial<PromQuery> = {}) {
  30. const languageProvider = new EmptyLanguageProviderMock() as unknown as PromQlLanguageProvider;
  31. const datasource = new PrometheusDatasource(
  32. {
  33. url: '',
  34. jsonData: {},
  35. meta: {} as any,
  36. } as any,
  37. undefined,
  38. undefined,
  39. languageProvider
  40. );
  41. const props = {
  42. datasource,
  43. query: {
  44. refId: 'A',
  45. expr: '',
  46. ...queryOverrides,
  47. },
  48. onRunQuery: jest.fn(),
  49. onChange: jest.fn(),
  50. };
  51. const { container } = render(<PromQueryBuilderContainer {...props} />);
  52. return { languageProvider, datasource, container, props };
  53. }