DataSourcePicker: Create new data source does not work for subpath (#90536)

This commit is contained in:
Ivan Ortega Alba 2024-07-17 18:23:08 +02:00 committed by GitHub
parent 283280f329
commit bc2cdd82dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import { locationUtil } from '@grafana/data';
import { LinkButton, ButtonVariant } from '@grafana/ui'; import { LinkButton, ButtonVariant } from '@grafana/ui';
import { contextSrv } from 'app/core/core'; import { contextSrv } from 'app/core/core';
import { Trans } from 'app/core/internationalization'; import { Trans } from 'app/core/internationalization';
@ -11,7 +12,7 @@ interface AddNewDataSourceButtonProps {
export function AddNewDataSourceButton({ variant, onClick }: AddNewDataSourceButtonProps) { export function AddNewDataSourceButton({ variant, onClick }: AddNewDataSourceButtonProps) {
const hasCreateRights = contextSrv.hasPermission(AccessControlAction.DataSourcesCreate); const hasCreateRights = contextSrv.hasPermission(AccessControlAction.DataSourcesCreate);
const newDataSourceURL = CONNECTIONS_ROUTES.DataSourcesNew; const newDataSourceURL = locationUtil.assureBaseUrl(CONNECTIONS_ROUTES.DataSourcesNew);
return ( return (
<LinkButton <LinkButton

View File

@ -1,7 +1,14 @@
import { queryByTestId, render, screen } from '@testing-library/react'; import { queryByTestId, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import { DataSourceInstanceSettings, DataSourcePluginMeta, PluginMetaInfo, PluginType } from '@grafana/data'; import {
DataSourceInstanceSettings,
DataSourcePluginMeta,
GrafanaConfig,
PluginMetaInfo,
PluginType,
locationUtil,
} from '@grafana/data';
import { config } from '@grafana/runtime'; import { config } from '@grafana/runtime';
import { DataSourceModal, DataSourceModalProps } from './DataSourceModal'; import { DataSourceModal, DataSourceModalProps } from './DataSourceModal';
@ -73,6 +80,12 @@ jest.mock('@grafana/runtime/src/services/dataSourceSrv', () => {
}; };
}); });
locationUtil.initialize({
config: { appSubUrl: '/my-sub-path' } as GrafanaConfig,
getTimeRangeForUrl: jest.fn(),
getVariablesUrlParams: jest.fn(),
});
const getListMock = jest.fn(); const getListMock = jest.fn();
const getInstanceSettingsMock = jest.fn(); const getInstanceSettingsMock = jest.fn();
beforeEach(() => { beforeEach(() => {
@ -95,6 +108,10 @@ describe('DataSourceDropdown', () => {
await user.keyboard('foobarbaz'); //Search for a DS that should not exist await user.keyboard('foobarbaz'); //Search for a DS that should not exist
expect(screen.queryAllByText('Configure a new data source')).toHaveLength(2); expect(screen.queryAllByText('Configure a new data source')).toHaveLength(2);
screen.queryAllByRole('link').forEach((link) => {
// It should point to the new data source page including any sub url configured
expect(link).toHaveAttribute('href', '/my-sub-path/connections/datasources/new');
});
}); });
it('only displays the file drop area when the ff is enabled', async () => { it('only displays the file drop area when the ff is enabled', async () => {

View File

@ -2,7 +2,14 @@ import { findByText, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup'; import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup';
import { DataSourceInstanceSettings, DataSourcePluginMeta, PluginMetaInfo, PluginType } from '@grafana/data'; import {
DataSourceInstanceSettings,
DataSourcePluginMeta,
GrafanaConfig,
PluginMetaInfo,
PluginType,
locationUtil,
} from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors'; import { selectors } from '@grafana/e2e-selectors';
import { ModalRoot, ModalsProvider } from '@grafana/ui'; import { ModalRoot, ModalsProvider } from '@grafana/ui';
import config from 'app/core/config'; import config from 'app/core/config';
@ -51,6 +58,12 @@ async function setupOpenDropdown(user: UserEvent, props: DataSourcePickerProps)
await user.click(searchBox!); await user.click(searchBox!);
} }
locationUtil.initialize({
config: { appSubUrl: '/my-sub-path' } as GrafanaConfig,
getVariablesUrlParams: jest.fn(),
getTimeRangeForUrl: jest.fn(),
});
jest.mock('@grafana/runtime', () => { jest.mock('@grafana/runtime', () => {
const actual = jest.requireActual('@grafana/runtime'); const actual = jest.requireActual('@grafana/runtime');
return { return {
@ -281,6 +294,8 @@ describe('DataSourcePicker', () => {
await user.keyboard('foobarbaz'); //Search for a DS that should not exist await user.keyboard('foobarbaz'); //Search for a DS that should not exist
expect(await screen.findByText('Configure a new data source')).toBeInTheDocument(); expect(await screen.findByText('Configure a new data source')).toBeInTheDocument();
// It should point to the new data source page including any sub url configured
expect(screen.getByRole('link')).toHaveAttribute('href', '/my-sub-path/connections/datasources/new');
}); });
it('should call onChange with the default query when add csv is clicked', async () => { it('should call onChange with the default query when add csv is clicked', async () => {