LokiQueryBuilder.test.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { render, screen, getAllByRole, waitFor } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { PanelData } from '@grafana/data';
  5. import { LokiDatasource } from '../../datasource';
  6. import { LokiOperationId, LokiVisualQuery } from '../types';
  7. import { LokiQueryBuilder } from './LokiQueryBuilder';
  8. const defaultQuery: LokiVisualQuery = {
  9. labels: [{ op: '=', label: 'baz', value: 'bar' }],
  10. operations: [],
  11. };
  12. describe('LokiQueryBuilder', () => {
  13. it('tries to load labels when no labels are selected', async () => {
  14. const { datasource } = setup();
  15. datasource.languageProvider.fetchSeriesLabels = jest.fn().mockReturnValue({ job: ['a'], instance: ['b'] });
  16. await userEvent.click(screen.getByLabelText('Add'));
  17. const labels = screen.getByText(/Labels/);
  18. const selects = getAllByRole(labels.parentElement!.parentElement!.parentElement!, 'combobox');
  19. await userEvent.click(selects[3]);
  20. await waitFor(() => expect(screen.getByText('job')).toBeInTheDocument());
  21. });
  22. it('shows error for query with operations and no stream selector', async () => {
  23. setup({ labels: [], operations: [{ id: LokiOperationId.Logfmt, params: [] }] });
  24. expect(screen.getByText('You need to specify at least 1 label filter (stream selector)')).toBeInTheDocument();
  25. });
  26. it('shows no error for query with empty __line_contains operation and no stream selector', async () => {
  27. setup({ labels: [], operations: [{ id: LokiOperationId.LineContains, params: [''] }] });
  28. expect(screen.queryByText('You need to specify at least 1 label filter (stream selector)')).not.toBeInTheDocument();
  29. });
  30. });
  31. function setup(query: LokiVisualQuery = defaultQuery, data?: PanelData) {
  32. const datasource = new LokiDatasource(
  33. {
  34. url: '',
  35. jsonData: {},
  36. meta: {} as any,
  37. } as any,
  38. undefined,
  39. undefined
  40. );
  41. const props = {
  42. datasource,
  43. onRunQuery: () => {},
  44. onChange: () => {},
  45. data,
  46. };
  47. const { container } = render(<LokiQueryBuilder {...props} query={query} />);
  48. return { datasource, container };
  49. }