LokiQueryBuilderOptions.test.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { fireEvent, render, screen } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { LokiQuery, LokiQueryType } from '../../types';
  5. import { LokiQueryBuilderOptions } from './LokiQueryBuilderOptions';
  6. describe('LokiQueryBuilderOptions', () => {
  7. it('Can change query type', async () => {
  8. const { props } = setup();
  9. screen.getByTitle('Click to edit options').click();
  10. expect(screen.getByLabelText('Range')).toBeChecked();
  11. screen.getByLabelText('Instant').click();
  12. expect(props.onChange).toHaveBeenCalledWith({
  13. ...props.query,
  14. queryType: LokiQueryType.Instant,
  15. });
  16. });
  17. it('Can change legend format', async () => {
  18. const { props } = setup();
  19. screen.getByTitle('Click to edit options').click();
  20. const element = screen.getByLabelText('Legend');
  21. await userEvent.type(element, 'asd');
  22. fireEvent.keyDown(element, { key: 'Enter', code: 'Enter', charCode: 13 });
  23. expect(props.onChange).toHaveBeenCalledWith({
  24. ...props.query,
  25. legendFormat: 'asd',
  26. });
  27. });
  28. });
  29. function setup(queryOverrides: Partial<LokiQuery> = {}) {
  30. const props = {
  31. query: {
  32. refId: 'A',
  33. expr: '',
  34. ...queryOverrides,
  35. },
  36. onRunQuery: jest.fn(),
  37. onChange: jest.fn(),
  38. };
  39. const { container } = render(<LokiQueryBuilderOptions {...props} />);
  40. return { container, props };
  41. }