QueryEditorRowHeader.test.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { fireEvent, render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { openMenu } from 'react-select-event';
  4. import { DataSourceInstanceSettings } from '@grafana/data';
  5. import { selectors } from '@grafana/e2e-selectors';
  6. import { mockDataSource } from 'app/features/alerting/unified/mocks';
  7. import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
  8. import { Props, QueryEditorRowHeader } from './QueryEditorRowHeader';
  9. const mockDS = mockDataSource({
  10. name: 'CloudManager',
  11. type: DataSourceType.Alertmanager,
  12. });
  13. const mockVariable = mockDataSource({
  14. name: '${dsVariable}',
  15. type: 'datasource',
  16. });
  17. jest.mock('@grafana/runtime/src/services/dataSourceSrv', () => {
  18. return {
  19. getDataSourceSrv: () => ({
  20. get: () => Promise.resolve(mockDS),
  21. getList: ({ variables }: { variables: boolean }) => (variables ? [mockDS, mockVariable] : [mockDS]),
  22. getInstanceSettings: () => mockDS,
  23. }),
  24. };
  25. });
  26. describe('QueryEditorRowHeader', () => {
  27. it('Can edit title', () => {
  28. const scenario = renderScenario({});
  29. screen.getByTestId('query-name-div').click();
  30. const input = screen.getByTestId('query-name-input');
  31. fireEvent.change(input, { target: { value: 'new name' } });
  32. fireEvent.blur(input);
  33. expect((scenario.props.onChange as any).mock.calls[0][0].refId).toBe('new name');
  34. });
  35. it('Show error when other query with same name exists', async () => {
  36. renderScenario({});
  37. screen.getByTestId('query-name-div').click();
  38. const input = screen.getByTestId('query-name-input');
  39. fireEvent.change(input, { target: { value: 'B' } });
  40. const alert = await screen.findByRole('alert');
  41. expect(alert.textContent).toBe('Query name already exists');
  42. });
  43. it('Show error when empty name is specified', async () => {
  44. renderScenario({});
  45. screen.getByTestId('query-name-div').click();
  46. const input = screen.getByTestId('query-name-input');
  47. fireEvent.change(input, { target: { value: '' } });
  48. const alert = await screen.findByRole('alert');
  49. expect(alert.textContent).toBe('An empty query name is not allowed');
  50. });
  51. it('should show data source picker when callback is passed', async () => {
  52. renderScenario({ onChangeDataSource: () => {} });
  53. expect(screen.queryByLabelText(selectors.components.DataSourcePicker.container)).not.toBeNull();
  54. });
  55. it('should not show data source picker when no callback is passed', async () => {
  56. renderScenario({ onChangeDataSource: undefined });
  57. expect(screen.queryByLabelText(selectors.components.DataSourcePicker.container)).toBeNull();
  58. });
  59. it('should render variables in the data source picker', async () => {
  60. renderScenario({ onChangeDataSource: () => {} });
  61. const dsSelect = screen.getByLabelText(selectors.components.DataSourcePicker.inputV2);
  62. openMenu(dsSelect);
  63. expect(await screen.findByText('${dsVariable}')).toBeInTheDocument();
  64. });
  65. });
  66. function renderScenario(overrides: Partial<Props>) {
  67. const props: Props = {
  68. query: {
  69. refId: 'A',
  70. },
  71. queries: [
  72. {
  73. refId: 'A',
  74. },
  75. {
  76. refId: 'B',
  77. },
  78. ],
  79. dataSource: {} as DataSourceInstanceSettings,
  80. disabled: false,
  81. onChange: jest.fn(),
  82. onClick: jest.fn(),
  83. collapsedText: '',
  84. };
  85. Object.assign(props, overrides);
  86. return {
  87. props,
  88. renderResult: render(<QueryEditorRowHeader {...props} />),
  89. };
  90. }