AdHocVariableEditor.test.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { selectOptionInTest, getSelectParent } from 'test/helpers/selectOptionInTest';
  4. import { adHocBuilder } from '../shared/testing/builders';
  5. import { AdHocVariableEditorUnConnected as AdHocVariableEditor } from './AdHocVariableEditor';
  6. const props = {
  7. extended: {
  8. dataSources: [
  9. { text: 'Prometheus', value: null }, // default datasource
  10. { text: 'Loki', value: { type: 'loki-ds', uid: 'abc' } },
  11. ],
  12. },
  13. variable: adHocBuilder().withId('adhoc').withRootStateKey('key').withName('adhoc').build(),
  14. onPropChange: jest.fn(),
  15. // connected actions
  16. initAdHocVariableEditor: jest.fn(),
  17. changeVariableDatasource: jest.fn(),
  18. };
  19. describe('AdHocVariableEditor', () => {
  20. beforeEach(() => {
  21. props.changeVariableDatasource.mockReset();
  22. });
  23. it('has a datasource select menu', async () => {
  24. render(<AdHocVariableEditor {...props} />);
  25. const selectContainer = getSelectParent(screen.getByLabelText('Data source'));
  26. expect(selectContainer).toHaveTextContent('Prometheus');
  27. });
  28. it('calls the callback when changing the datasource', async () => {
  29. render(<AdHocVariableEditor {...props} />);
  30. await selectOptionInTest(screen.getByLabelText('Data source'), 'Loki');
  31. expect(props.changeVariableDatasource).toBeCalledWith(
  32. { type: 'adhoc', id: 'adhoc', rootStateKey: 'key' },
  33. { type: 'loki-ds', uid: 'abc' }
  34. );
  35. });
  36. it('renders informational text', () => {
  37. const extended = {
  38. ...props.extended,
  39. infoText: "Here's a message that should help you",
  40. };
  41. render(<AdHocVariableEditor {...props} extended={extended} />);
  42. const alert = screen.getByText("Here's a message that should help you");
  43. expect(alert).toBeInTheDocument();
  44. });
  45. });