grafana/public/app/plugins/datasource/loki/configuration/DerivedField.test.tsx
Matias Chomicki 9441692fe9
Loki Derived Fields: Refactor legacy form components and add validation (#68015)
* Derived fields: validate duplicated names

* Derived fields: rename prop

* Update tests

* Derived fields: do not validate empty names as repeated

* Derived Field: use non-legacy Field and Input

* Derived Field: integrate name validation

* Derived field: align delete button

* Derived Field: add tooltips

* Derived Field: migrate and style internal link field

* Update tests

* Derived Field: ask user to select data source

Otherwise it's bugged

* Remove unnecessary onchange handler

* Initialize all derived fields attributes

Otherwise we trigger controlled-to-uncontrolled React errors

* Update public/app/plugins/datasource/loki/configuration/DerivedField.tsx

Co-authored-by: Gábor Farkas <gabor.farkas@gmail.com>

---------

Co-authored-by: Gábor Farkas <gabor.farkas@gmail.com>
2023-05-15 13:48:50 +03:00

135 lines
3.6 KiB
TypeScript

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { DataSourceInstanceSettings, DataSourcePluginMeta } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { setDataSourceSrv } from '@grafana/runtime';
import { DerivedField } from './DerivedField';
const mockList = jest.fn();
const validateMock = jest.fn();
describe('DerivedField', () => {
beforeEach(() => {
setDataSourceSrv({
get: jest.fn(),
reload: jest.fn(),
getInstanceSettings: jest.fn(),
getList: mockList.mockImplementation(() => [
{
id: 1,
uid: 'metrics',
name: 'metrics_ds',
meta: {
tracing: false,
info: {
logos: {
small: '',
},
},
} as DataSourcePluginMeta,
} as DataSourceInstanceSettings,
{
id: 2,
uid: 'tracing',
name: 'tracing_ds',
meta: {
tracing: true,
info: {
logos: {
small: '',
},
},
} as DataSourcePluginMeta,
} as DataSourceInstanceSettings,
]),
});
});
it('shows internal link if uid is set', async () => {
const value = {
matcherRegex: '',
name: '',
datasourceUid: 'test',
};
// Render and wait for the Name field to be visible
// using findBy to wait for asynchronous operations to complete
render(
<DerivedField
validateName={validateMock}
value={value}
onChange={() => {}}
onDelete={() => {}}
suggestions={[]}
/>
);
expect(await screen.findByText('Name')).toBeInTheDocument();
expect(screen.getByLabelText(selectors.components.DataSourcePicker.inputV2)).toBeInTheDocument();
});
it('shows url link if uid is not set', async () => {
const value = {
matcherRegex: '',
name: '',
url: 'test',
};
// Render and wait for the Name field to be visible
// using findBy to wait for asynchronous operations to complete
render(
<DerivedField
validateName={validateMock}
value={value}
onChange={() => {}}
onDelete={() => {}}
suggestions={[]}
/>
);
expect(await screen.findByText('Name')).toBeInTheDocument();
expect(screen.queryByLabelText(selectors.components.DataSourcePicker.inputV2)).not.toBeInTheDocument();
});
it('shows only tracing datasources for internal link', async () => {
const value = {
matcherRegex: '',
name: '',
datasourceUid: 'test',
};
// Render and wait for the Name field to be visible
// using findBy to wait for asynchronous operations to complete
render(
<DerivedField
validateName={validateMock}
value={value}
onChange={() => {}}
onDelete={() => {}}
suggestions={[]}
/>
);
expect(await screen.findByText('Name')).toBeInTheDocument();
expect(mockList).toHaveBeenCalledWith(
expect.objectContaining({
tracing: true,
})
);
});
it('validates the field name', async () => {
const value = {
matcherRegex: '',
name: 'field-name',
datasourceUid: 'test',
};
const validate = jest.fn().mockReturnValue(false);
render(
<DerivedField validateName={validate} value={value} onChange={() => {}} onDelete={() => {}} suggestions={[]} />
);
userEvent.click(await screen.findByDisplayValue(value.name));
expect(await screen.findByText('The name is already in use')).toBeInTheDocument();
});
});