DataSourceVariableEditor.test.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { render, screen, fireEvent } from '@testing-library/react';
  2. import React from 'react';
  3. import { selectOptionInTest, getSelectParent } from 'test/helpers/selectOptionInTest';
  4. import { DataSourceVariableEditorUnConnected as DataSourceVariableEditor } from './DataSourceVariableEditor';
  5. import { initialDataSourceVariableModelState } from './reducer';
  6. const props = {
  7. extended: {
  8. dataSourceTypes: [
  9. { text: 'Prometheus', value: 'ds-prom' },
  10. { text: 'Loki', value: 'ds-loki' },
  11. ],
  12. },
  13. variable: { ...initialDataSourceVariableModelState, rootStateKey: 'foo' },
  14. onPropChange: jest.fn(),
  15. // connected actions
  16. initDataSourceVariableEditor: jest.fn(),
  17. changeVariableMultiValue: jest.fn(),
  18. };
  19. describe('DataSourceVariableEditor', () => {
  20. beforeEach(() => {
  21. props.onPropChange.mockReset();
  22. });
  23. it('has a data source select menu', () => {
  24. render(<DataSourceVariableEditor {...props} />);
  25. const selectContainer = getSelectParent(screen.getByLabelText('Type'));
  26. expect(selectContainer).toHaveTextContent('Prometheus');
  27. });
  28. it('calls the handler when the data source is changed', async () => {
  29. render(<DataSourceVariableEditor {...props} />);
  30. await selectOptionInTest(screen.getByLabelText('Type'), 'Loki');
  31. expect(props.onPropChange).toBeCalledWith({ propName: 'query', propValue: 'ds-loki', updateOptions: true });
  32. });
  33. it('has a regex filter field', () => {
  34. render(<DataSourceVariableEditor {...props} />);
  35. const field = screen.getByLabelText('Instance name filter');
  36. expect(field).toBeInTheDocument();
  37. });
  38. it('calls the handler when the regex filter is changed', () => {
  39. render(<DataSourceVariableEditor {...props} />);
  40. const field = screen.getByLabelText('Instance name filter');
  41. fireEvent.change(field, { target: { value: '/prod/' } });
  42. expect(props.onPropChange).toBeCalledWith({ propName: 'regex', propValue: '/prod/' });
  43. });
  44. });