2022-08-30 04:28:06 -05:00
|
|
|
import { render, screen } from '@testing-library/react';
|
2023-05-15 05:48:50 -05:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2022-04-22 08:33:13 -05:00
|
|
|
import React from 'react';
|
|
|
|
|
2022-08-24 03:06:28 -05:00
|
|
|
import { DataSourceInstanceSettings, DataSourcePluginMeta } from '@grafana/data';
|
2022-08-30 04:28:06 -05:00
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
|
|
|
import { setDataSourceSrv } from '@grafana/runtime';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { DerivedField } from './DerivedField';
|
2020-04-20 08:48:38 -05:00
|
|
|
|
2022-08-30 04:28:06 -05:00
|
|
|
const mockList = jest.fn();
|
2023-05-15 05:48:50 -05:00
|
|
|
const validateMock = jest.fn();
|
2020-04-20 08:48:38 -05:00
|
|
|
|
|
|
|
describe('DerivedField', () => {
|
2022-08-30 04:28:06 -05:00
|
|
|
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 () => {
|
2020-04-20 08:48:38 -05:00
|
|
|
const value = {
|
|
|
|
matcherRegex: '',
|
|
|
|
name: '',
|
|
|
|
datasourceUid: 'test',
|
|
|
|
};
|
2022-08-30 04:28:06 -05:00
|
|
|
// Render and wait for the Name field to be visible
|
|
|
|
// using findBy to wait for asynchronous operations to complete
|
2023-05-15 05:48:50 -05:00
|
|
|
render(
|
|
|
|
<DerivedField
|
|
|
|
validateName={validateMock}
|
|
|
|
value={value}
|
|
|
|
onChange={() => {}}
|
|
|
|
onDelete={() => {}}
|
|
|
|
suggestions={[]}
|
|
|
|
/>
|
|
|
|
);
|
2022-08-30 04:28:06 -05:00
|
|
|
expect(await screen.findByText('Name')).toBeInTheDocument();
|
2020-04-20 08:48:38 -05:00
|
|
|
|
2022-08-30 04:28:06 -05:00
|
|
|
expect(screen.getByLabelText(selectors.components.DataSourcePicker.inputV2)).toBeInTheDocument();
|
2020-04-20 08:48:38 -05:00
|
|
|
});
|
|
|
|
|
2022-08-30 04:28:06 -05:00
|
|
|
it('shows url link if uid is not set', async () => {
|
2020-04-20 08:48:38 -05:00
|
|
|
const value = {
|
|
|
|
matcherRegex: '',
|
|
|
|
name: '',
|
|
|
|
url: 'test',
|
|
|
|
};
|
2022-08-30 04:28:06 -05:00
|
|
|
// Render and wait for the Name field to be visible
|
|
|
|
// using findBy to wait for asynchronous operations to complete
|
2023-05-15 05:48:50 -05:00
|
|
|
render(
|
|
|
|
<DerivedField
|
|
|
|
validateName={validateMock}
|
|
|
|
value={value}
|
|
|
|
onChange={() => {}}
|
|
|
|
onDelete={() => {}}
|
|
|
|
suggestions={[]}
|
|
|
|
/>
|
|
|
|
);
|
2022-08-30 04:28:06 -05:00
|
|
|
expect(await screen.findByText('Name')).toBeInTheDocument();
|
|
|
|
|
|
|
|
expect(screen.queryByLabelText(selectors.components.DataSourcePicker.inputV2)).not.toBeInTheDocument();
|
2020-04-20 08:48:38 -05:00
|
|
|
});
|
2020-05-05 10:08:31 -05:00
|
|
|
|
2022-08-30 04:28:06 -05:00
|
|
|
it('shows only tracing datasources for internal link', async () => {
|
2020-05-05 10:08:31 -05:00
|
|
|
const value = {
|
|
|
|
matcherRegex: '',
|
|
|
|
name: '',
|
|
|
|
datasourceUid: 'test',
|
|
|
|
};
|
2022-08-30 04:28:06 -05:00
|
|
|
// Render and wait for the Name field to be visible
|
|
|
|
// using findBy to wait for asynchronous operations to complete
|
2023-05-15 05:48:50 -05:00
|
|
|
render(
|
|
|
|
<DerivedField
|
|
|
|
validateName={validateMock}
|
|
|
|
value={value}
|
|
|
|
onChange={() => {}}
|
|
|
|
onDelete={() => {}}
|
|
|
|
suggestions={[]}
|
|
|
|
/>
|
|
|
|
);
|
2022-08-30 04:28:06 -05:00
|
|
|
expect(await screen.findByText('Name')).toBeInTheDocument();
|
|
|
|
expect(mockList).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
tracing: true,
|
|
|
|
})
|
|
|
|
);
|
2020-05-05 10:08:31 -05:00
|
|
|
});
|
2023-05-15 05:48:50 -05:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
2020-04-20 08:48:38 -05:00
|
|
|
});
|