WriteTargetConfig.test.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import '@testing-library/jest-dom';
  2. import { render, screen, waitFor } from '@testing-library/react';
  3. import userEvent from '@testing-library/user-event';
  4. import React from 'react';
  5. import { of } from 'rxjs';
  6. import { DataSourceInstanceSettings, NavModel } from '@grafana/data';
  7. import { DataSourcePickerProps } from '@grafana/runtime';
  8. import { PrometheusWriteTarget } from '../types';
  9. import { WriteTargetConfigUnconnected } from './WriteTargetConfig';
  10. function MockPicker(props: DataSourcePickerProps) {
  11. return <></>;
  12. }
  13. const settingsMock = jest.fn().mockReturnValue({ uid: 'test-datasource-uid' } as DataSourceInstanceSettings);
  14. const fetchMock = jest.fn().mockReturnValue(of({ data: { message: 'hello' } }));
  15. jest.mock('@grafana/runtime', () => {
  16. const original = jest.requireActual('@grafana/runtime');
  17. const mockedRuntime = { ...original };
  18. mockedRuntime.getDataSourceSrv = () => {
  19. return {
  20. getInstanceSettings: settingsMock,
  21. };
  22. };
  23. mockedRuntime.getBackendSrv = () => ({
  24. fetch: fetchMock,
  25. });
  26. mockedRuntime.DataSourcePicker = MockPicker;
  27. return mockedRuntime;
  28. });
  29. describe('WriteTargetConfig', () => {
  30. it('renders the datasource picker and remote_write_target', async () => {
  31. const prometheusWriteTarget: PrometheusWriteTarget = {
  32. data_source_uid: 'test-uid',
  33. remote_write_path: 'test-path',
  34. };
  35. render(
  36. <WriteTargetConfigUnconnected
  37. navModel={{ node: {}, main: {} } as NavModel}
  38. prometheusWriteTarget={prometheusWriteTarget}
  39. getPrometheusWriteTarget={() => {}}
  40. />
  41. );
  42. expect(screen.getByDisplayValue('test-path')).toBeInTheDocument();
  43. await waitFor(() => {
  44. expect(settingsMock).toHaveBeenCalledWith('test-uid');
  45. });
  46. await userEvent.click(screen.getByLabelText('Write target config save'));
  47. expect(screen.getByLabelText('Write target config save')).not.toBeDisabled();
  48. await waitFor(() => {
  49. expect(fetchMock).toHaveBeenCalledWith({
  50. method: 'POST',
  51. url: 'api/recording-rules/writer',
  52. data: { data_source_uid: 'test-datasource-uid', remote_write_path: 'test-path' },
  53. showSuccessAlert: false,
  54. showErrorAlert: false,
  55. });
  56. });
  57. });
  58. it('should disable save button if write target is not set', async () => {
  59. render(
  60. <WriteTargetConfigUnconnected
  61. navModel={{ node: {}, main: {} } as NavModel}
  62. prometheusWriteTarget={undefined}
  63. getPrometheusWriteTarget={() => {}}
  64. />
  65. );
  66. expect(screen.getByLabelText('Write target config save')).toBeDisabled();
  67. });
  68. });