Chore: Update tests and remove `MockDataSourceSrv` (#97748)

pull/101043/head
Tom Ratcliffe 4 months ago committed by GitHub
parent e2149e35e4
commit c9da1ec850
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      .betterer.results
  2. 87
      public/app/features/alerting/unified/mocks.ts
  3. 6
      public/app/features/alerting/unified/mocks/server/db.ts
  4. 48
      public/app/features/correlations/CorrelationsPage.test.tsx
  5. 4
      public/app/features/correlations/Forms/QueryEditorField.test.tsx
  6. 52
      public/app/features/dashboard-scene/panel-edit/PanelDataPane/PanelDataAlertingTab.test.tsx
  7. 13
      public/app/features/dashboard-scene/panel-edit/PanelEditor.test.ts
  8. 43
      public/app/features/dashboard/components/DashboardSettings/AnnotationsSettings.test.tsx
  9. 6
      public/app/features/dashboard/state/DashboardMigrator.test.ts
  10. 13
      public/app/features/templating/template_srv.test.ts
  11. 12
      public/app/features/trails/DataTrail.test.tsx
  12. 12
      public/app/features/trails/TrailStore/TrailStore.test.ts
  13. 24
      public/app/features/trails/otel/utils.test.ts
  14. 10
      public/app/plugins/datasource/dashboard/DashboardQueryEditor.test.tsx

@ -2865,10 +2865,7 @@ exports[`better eslint`] = {
[0, 0, 0, "Do not use any type assertions.", "2"], [0, 0, 0, "Do not use any type assertions.", "2"],
[0, 0, 0, "Do not use any type assertions.", "3"], [0, 0, 0, "Do not use any type assertions.", "3"],
[0, 0, 0, "Do not use any type assertions.", "4"], [0, 0, 0, "Do not use any type assertions.", "4"],
[0, 0, 0, "Unexpected any. Specify a different type.", "5"], [0, 0, 0, "Unexpected any. Specify a different type.", "5"]
[0, 0, 0, "Unexpected any. Specify a different type.", "6"],
[0, 0, 0, "Unexpected any. Specify a different type.", "7"],
[0, 0, 0, "Unexpected any. Specify a different type.", "8"]
], ],
"public/app/features/alerting/unified/plugins/PluginOriginBadge.tsx:5381": [ "public/app/features/alerting/unified/plugins/PluginOriginBadge.tsx:5381": [
[0, 0, 0, "\'@grafana/ui/src/components/Icon/utils\' import is restricted from being used by a pattern. Import from the public export instead.", "0"] [0, 0, 0, "\'@grafana/ui/src/components/Icon/utils\' import is restricted from being used by a pattern. Import from the public export instead.", "0"]

@ -1,28 +1,19 @@
import { produce } from 'immer'; import { produce } from 'immer';
import { isEmpty, pick } from 'lodash'; import { isEmpty, pick } from 'lodash';
import { Observable } from 'rxjs';
import { import {
DataQuery,
DataQueryRequest,
DataQueryResponse,
DataSourceApi,
DataSourceInstanceSettings, DataSourceInstanceSettings,
DataSourceJsonData, DataSourceJsonData,
DataSourcePluginMeta, DataSourcePluginMeta,
DataSourceRef,
PluginExtensionLink, PluginExtensionLink,
PluginExtensionTypes, PluginExtensionTypes,
ReducerID, ReducerID,
ScopedVars,
TestDataSourceResponse,
} from '@grafana/data'; } from '@grafana/data';
import { DataSourceSrv, GetDataSourceListFilters, RuntimeDataSourceRegistration, config } from '@grafana/runtime'; import { config } from '@grafana/runtime';
import { defaultDashboard } from '@grafana/schema'; import { defaultDashboard } from '@grafana/schema';
import { contextSrv } from 'app/core/services/context_srv'; import { contextSrv } from 'app/core/services/context_srv';
import { MOCK_GRAFANA_ALERT_RULE_TITLE } from 'app/features/alerting/unified/mocks/server/handlers/grafanaRuler'; import { MOCK_GRAFANA_ALERT_RULE_TITLE } from 'app/features/alerting/unified/mocks/server/handlers/grafanaRuler';
import { ExpressionQuery, ExpressionQueryType, ReducerMode } from 'app/features/expressions/types'; import { ExpressionQuery, ExpressionQueryType, ReducerMode } from 'app/features/expressions/types';
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
import { import {
AlertManagerCortexConfig, AlertManagerCortexConfig,
AlertState, AlertState,
@ -400,82 +391,6 @@ export const mockReceiversState = (partial: Partial<ReceiversState> = {}): Recei
}; };
}; };
class MockDataSourceApi extends DataSourceApi {
constructor(instanceSettings: DataSourceInstanceSettings<DataSourceJsonData>) {
super(instanceSettings);
}
query(request: DataQueryRequest<DataQuery>): Promise<DataQueryResponse> | Observable<DataQueryResponse> {
throw new Error('Method not implemented.');
}
testDatasource(): Promise<TestDataSourceResponse> {
throw new Error('Method not implemented.');
}
}
/** @deprecated use `setupDatasources` instead */
export class MockDataSourceSrv implements DataSourceSrv {
datasources: Record<string, DataSourceApi> = {};
// @ts-ignore
private settingsMapByName: Record<string, DataSourceInstanceSettings> = {};
private settingsMapByUid: Record<string, DataSourceInstanceSettings> = {};
private settingsMapById: Record<string, DataSourceInstanceSettings> = {};
// @ts-ignore
private templateSrv = {
getVariables: () => [],
replace: (name: any) => name,
};
defaultName = '';
constructor(datasources: Record<string, DataSourceInstanceSettings>) {
this.datasources = {};
this.settingsMapByName = Object.values(datasources).reduce<Record<string, DataSourceInstanceSettings>>(
(acc, ds) => {
acc[ds.name] = ds;
return acc;
},
{}
);
for (const dsSettings of Object.values(this.settingsMapByName)) {
this.settingsMapByUid[dsSettings.uid] = dsSettings;
this.settingsMapById[dsSettings.id] = dsSettings;
if (dsSettings.isDefault) {
this.defaultName = dsSettings.name;
}
this.datasources[dsSettings.uid] = new MockDataSourceApi(dsSettings);
}
}
registerRuntimeDataSource(entry: RuntimeDataSourceRegistration): void {}
get(name?: string | null | DataSourceRef, scopedVars?: ScopedVars): Promise<DataSourceApi> {
return DatasourceSrv.prototype.get.call(this, name, scopedVars);
//return Promise.reject(new Error('not implemented'));
}
/**
* Get a list of data sources
*/
getList(filters?: GetDataSourceListFilters): DataSourceInstanceSettings[] {
return DatasourceSrv.prototype.getList.call(this, filters);
}
/**
* Get settings and plugin metadata by name or uid
*/
getInstanceSettings(nameOrUid: string | null | undefined): DataSourceInstanceSettings | undefined {
return DatasourceSrv.prototype.getInstanceSettings.call(this, nameOrUid);
}
async loadDatasource(name: string): Promise<DataSourceApi<any, any>> {
return DatasourceSrv.prototype.loadDatasource.call(this, name);
}
reload() {}
}
export const mockGrafanaReceiver = ( export const mockGrafanaReceiver = (
type: string, type: string,
overrides: Partial<GrafanaManagedReceiverConfig> = {} overrides: Partial<GrafanaManagedReceiverConfig> = {}

@ -2,7 +2,7 @@ import { Factory } from 'fishery';
import { uniqueId } from 'lodash'; import { uniqueId } from 'lodash';
import { DataSourceInstanceSettings, PluginType } from '@grafana/data'; import { DataSourceInstanceSettings, PluginType } from '@grafana/data';
import { config, setDataSourceSrv } from '@grafana/runtime'; import { config } from '@grafana/runtime';
import { FolderDTO } from 'app/types'; import { FolderDTO } from 'app/types';
import { import {
PromAlertingRuleDTO, PromAlertingRuleDTO,
@ -14,7 +14,7 @@ import {
RulerRuleGroupDTO, RulerRuleGroupDTO,
} from 'app/types/unified-alerting-dto'; } from 'app/types/unified-alerting-dto';
import { MockDataSourceSrv } from '../../mocks'; import { setupDataSources } from '../../testSetup/datasources';
import { DataSourceType } from '../../utils/datasource'; import { DataSourceType } from '../../utils/datasource';
const prometheusRuleFactory = Factory.define<PromAlertingRuleDTO>(({ sequence }) => ({ const prometheusRuleFactory = Factory.define<PromAlertingRuleDTO>(({ sequence }) => ({
@ -62,7 +62,7 @@ const prometheusRuleGroupFactory = Factory.define<PromRuleGroupDTO>(({ sequence
const dataSourceFactory = Factory.define<DataSourceInstanceSettings>(({ sequence, params, afterBuild }) => { const dataSourceFactory = Factory.define<DataSourceInstanceSettings>(({ sequence, params, afterBuild }) => {
afterBuild((dataSource) => { afterBuild((dataSource) => {
config.datasources[dataSource.name] = dataSource; config.datasources[dataSource.name] = dataSource;
setDataSourceSrv(new MockDataSourceSrv(config.datasources)); setupDataSources(...Object.values(config.datasources));
}); });
const uid = params.uid ?? `mock-ds-${sequence}`; const uid = params.uid ?? `mock-ds-${sequence}`;

@ -4,16 +4,17 @@ import { merge, uniqueId } from 'lodash';
import { openMenu } from 'react-select-event'; import { openMenu } from 'react-select-event';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { TestProvider } from 'test/helpers/TestProvider'; import { TestProvider } from 'test/helpers/TestProvider';
import { MockDataSourceApi } from 'test/mocks/datasource_srv';
import { getGrafanaContextMock } from 'test/mocks/getGrafanaContextMock'; import { getGrafanaContextMock } from 'test/mocks/getGrafanaContextMock';
import { DataSourcePluginMeta, SupportedTransformationType } from '@grafana/data'; import { DataSourceInstanceSettings, SupportedTransformationType } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors'; import { selectors } from '@grafana/e2e-selectors';
import { BackendSrv, setDataSourceSrv, BackendSrvRequest, reportInteraction } from '@grafana/runtime'; import { BackendSrv, BackendSrvRequest, reportInteraction, setBackendSrv, setAppEvents } from '@grafana/runtime';
import appEvents from 'app/core/app_events';
import { contextSrv } from 'app/core/services/context_srv'; import { contextSrv } from 'app/core/services/context_srv';
import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { configureStore } from 'app/store/configureStore'; import { configureStore } from 'app/store/configureStore';
import { mockDataSource, MockDataSourceSrv } from '../alerting/unified/mocks'; import { mockDataSource } from '../alerting/unified/mocks';
import CorrelationsPage from './CorrelationsPage'; import CorrelationsPage from './CorrelationsPage';
import { import {
@ -25,8 +26,11 @@ import {
} from './__mocks__/useCorrelations.mocks'; } from './__mocks__/useCorrelations.mocks';
import { Correlation, CreateCorrelationParams, OmitUnion } from './types'; import { Correlation, CreateCorrelationParams, OmitUnion } from './types';
// Set app events up, otherwise plugin modules will fail to load
setAppEvents(appEvents);
const renderWithContext = async ( const renderWithContext = async (
datasources: ConstructorParameters<typeof MockDataSourceSrv>[0] = {}, datasources: Record<string, DataSourceInstanceSettings>,
correlations: Correlation[] = [] correlations: Correlation[] = []
) => { ) => {
const backend = { const backend = {
@ -86,17 +90,8 @@ const renderWithContext = async (
}, },
} as unknown as BackendSrv; } as unknown as BackendSrv;
const grafanaContext = getGrafanaContextMock({ backend }); const grafanaContext = getGrafanaContextMock({ backend });
setBackendSrv(backend);
const dsServer = new MockDataSourceSrv(datasources); setupDataSources(...Object.values(datasources));
dsServer.get = (name: string) => {
const dsApi = new MockDataSourceApi(name);
dsApi.components = {
QueryEditor: () => <>{name} query editor</>,
};
return Promise.resolve(dsApi);
};
setDataSourceSrv(dsServer);
const renderResult = render( const renderResult = render(
<TestProvider store={configureStore({})} grafanaContext={grafanaContext}> <TestProvider store={configureStore({})} grafanaContext={grafanaContext}>
@ -210,10 +205,9 @@ describe('CorrelationsPage', () => {
name: 'loki', name: 'loki',
readOnly: false, readOnly: false,
jsonData: {}, jsonData: {},
access: 'direct',
type: 'datasource', type: 'datasource',
}, },
{ logs: true } { logs: true, module: 'core:plugin/loki' }
), ),
prometheus: mockDataSource( prometheus: mockDataSource(
{ {
@ -221,10 +215,9 @@ describe('CorrelationsPage', () => {
name: 'prometheus', name: 'prometheus',
readOnly: false, readOnly: false,
jsonData: {}, jsonData: {},
access: 'direct',
type: 'datasource', type: 'datasource',
}, },
{ metrics: true } { metrics: true, module: 'core:plugin/prometheus' }
), ),
}); });
}); });
@ -319,11 +312,11 @@ describe('CorrelationsPage', () => {
name: 'loki', name: 'loki',
readOnly: false, readOnly: false,
jsonData: {}, jsonData: {},
access: 'direct',
type: 'datasource', type: 'datasource',
}, },
{ {
logs: true, logs: true,
module: 'core:plugin/loki',
} }
), ),
prometheus: mockDataSource( prometheus: mockDataSource(
@ -332,11 +325,11 @@ describe('CorrelationsPage', () => {
name: 'prometheus', name: 'prometheus',
readOnly: false, readOnly: false,
jsonData: {}, jsonData: {},
access: 'direct',
type: 'datasource', type: 'datasource',
}, },
{ {
metrics: true, metrics: true,
module: 'core:plugin/prometheus',
} }
), ),
elastic: mockDataSource( elastic: mockDataSource(
@ -345,12 +338,12 @@ describe('CorrelationsPage', () => {
name: 'elastic', name: 'elastic',
readOnly: false, readOnly: false,
jsonData: {}, jsonData: {},
access: 'direct',
type: 'datasource', type: 'datasource',
}, },
{ {
metrics: true, metrics: true,
logs: true, logs: true,
module: 'core:plugin/elasticsearch',
} }
), ),
}, },
@ -589,6 +582,7 @@ describe('CorrelationsPage', () => {
}, },
{ {
logs: true, logs: true,
module: 'core:plugin/loki',
} }
), ),
}, },
@ -675,15 +669,17 @@ describe('CorrelationsPage', () => {
beforeEach(async () => { beforeEach(async () => {
await renderWithContext( await renderWithContext(
{ {
loki: mockDataSource({ loki: mockDataSource(
{
uid: 'loki', uid: 'loki',
name: 'loki', name: 'loki',
readOnly: true, readOnly: true,
jsonData: {}, jsonData: {},
access: 'direct', access: 'direct',
meta: { info: { logos: {} } } as DataSourcePluginMeta,
type: 'datasource', type: 'datasource',
}), },
{ logs: true, module: 'core:plugin/loki' }
),
}, },
correlations correlations
); );

@ -4,7 +4,7 @@ import { FormProvider, useForm } from 'react-hook-form';
import { MockDataSourceApi } from 'test/mocks/datasource_srv'; import { MockDataSourceApi } from 'test/mocks/datasource_srv';
import { setDataSourceSrv } from '@grafana/runtime'; import { setDataSourceSrv } from '@grafana/runtime';
import { MockDataSourceSrv } from 'app/features/alerting/unified/mocks'; import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { QueryEditorField } from './QueryEditorField'; import { QueryEditorField } from './QueryEditorField';
@ -25,7 +25,7 @@ const renderWithContext = (
children: ReactNode, children: ReactNode,
getHandler: (name: string) => Promise<MockDataSourceApi> = defaultGetHandler getHandler: (name: string) => Promise<MockDataSourceApi> = defaultGetHandler
) => { ) => {
const dsServer = new MockDataSourceSrv({}); const dsServer = setupDataSources();
dsServer.get = getHandler; dsServer.get = getHandler;
setDataSourceSrv(dsServer); setDataSourceSrv(dsServer);

@ -2,16 +2,14 @@ import userEvent from '@testing-library/user-event';
import { render } from 'test/test-utils'; import { render } from 'test/test-utils';
import { byTestId } from 'testing-library-selector'; import { byTestId } from 'testing-library-selector';
import { DataSourceApi } from '@grafana/data'; import { PromOptions } from '@grafana/prometheus';
import { PromOptions, PrometheusDatasource } from '@grafana/prometheus'; import { config, locationService, setPluginLinksHook } from '@grafana/runtime';
import { config, locationService, setDataSourceSrv, setPluginLinksHook } from '@grafana/runtime';
import { backendSrv } from 'app/core/services/backend_srv'; import { backendSrv } from 'app/core/services/backend_srv';
import * as ruler from 'app/features/alerting/unified/api/ruler'; import * as ruler from 'app/features/alerting/unified/api/ruler';
import * as ruleActionButtons from 'app/features/alerting/unified/components/rules/RuleActionsButtons'; import * as ruleActionButtons from 'app/features/alerting/unified/components/rules/RuleActionsButtons';
import * as alertingAbilities from 'app/features/alerting/unified/hooks/useAbilities'; import * as alertingAbilities from 'app/features/alerting/unified/hooks/useAbilities';
import { mockAlertRuleApi, setupMswServer } from 'app/features/alerting/unified/mockApi'; import { mockAlertRuleApi, setupMswServer } from 'app/features/alerting/unified/mockApi';
import { import {
MockDataSourceSrv,
grantUserPermissions, grantUserPermissions,
mockDataSource, mockDataSource,
mockFolder, mockFolder,
@ -20,13 +18,12 @@ import {
mockRulerAlertingRule, mockRulerAlertingRule,
mockRulerRuleGroup, mockRulerRuleGroup,
} from 'app/features/alerting/unified/mocks'; } from 'app/features/alerting/unified/mocks';
import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { RuleFormValues } from 'app/features/alerting/unified/types/rule-form'; import { RuleFormValues } from 'app/features/alerting/unified/types/rule-form';
import * as configDS from 'app/features/alerting/unified/utils/config';
import { Annotation } from 'app/features/alerting/unified/utils/constants'; import { Annotation } from 'app/features/alerting/unified/utils/constants';
import { DataSourceType, GRAFANA_RULES_SOURCE_NAME } from 'app/features/alerting/unified/utils/datasource'; import { DataSourceType, GRAFANA_RULES_SOURCE_NAME } from 'app/features/alerting/unified/utils/datasource';
import { DashboardModel } from 'app/features/dashboard/state/DashboardModel'; import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
import { PanelModel } from 'app/features/dashboard/state/PanelModel'; import { PanelModel } from 'app/features/dashboard/state/PanelModel';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
import { configureStore } from 'app/store/configureStore'; import { configureStore } from 'app/store/configureStore';
import { AccessControlAction, DashboardDataDTO } from 'app/types'; import { AccessControlAction, DashboardDataDTO } from 'app/types';
import { AlertQuery, PromRulesResponse } from 'app/types/unified-alerting-dto'; import { AlertQuery, PromRulesResponse } from 'app/types/unified-alerting-dto';
@ -44,7 +41,6 @@ import { PanelDataAlertingTab, PanelDataAlertingTabRendered } from './PanelDataA
jest.mock('app/features/alerting/unified/api/prometheus'); jest.mock('app/features/alerting/unified/api/prometheus');
jest.mock('app/features/alerting/unified/api/ruler'); jest.mock('app/features/alerting/unified/api/ruler');
jest.spyOn(configDS, 'getAllDataSources');
jest.spyOn(ruleActionButtons, 'matchesWidth').mockReturnValue(false); jest.spyOn(ruleActionButtons, 'matchesWidth').mockReturnValue(false);
jest.spyOn(ruler, 'rulerUrlBuilder'); jest.spyOn(ruler, 'rulerUrlBuilder');
jest.spyOn(alertingAbilities, 'useAlertRuleAbility'); jest.spyOn(alertingAbilities, 'useAlertRuleAbility');
@ -55,22 +51,34 @@ setPluginLinksHook(() => ({
})); }));
const dataSources = { const dataSources = {
prometheus: mockDataSource<PromOptions>({ prometheus: mockDataSource<PromOptions>(
{
name: 'Prometheus', name: 'Prometheus',
type: DataSourceType.Prometheus, type: DataSourceType.Prometheus,
isDefault: false, isDefault: false,
}), },
default: mockDataSource<PromOptions>({ { alerting: true, module: 'core:plugin/prometheus' }
),
default: mockDataSource<PromOptions>(
{
name: 'Default', name: 'Default',
type: DataSourceType.Prometheus, type: DataSourceType.Prometheus,
isDefault: true, isDefault: true,
}), },
{ alerting: true, module: 'core:plugin/prometheus' }
),
prometheusMinInterval: mockDataSource<PromOptions>(
{
name: 'Prometheus Min Interval',
type: DataSourceType.Prometheus,
isDefault: false,
jsonData: { manageAlerts: true, timeInterval: '7m' },
},
{ alerting: true, module: 'core:plugin/prometheus' }
),
}; };
dataSources.prometheus.meta.alerting = true;
dataSources.default.meta.alerting = true;
const mocks = { const mocks = {
getAllDataSources: jest.mocked(configDS.getAllDataSources),
useAlertRuleAbilityMock: jest.mocked(alertingAbilities.useAlertRuleAbility), useAlertRuleAbilityMock: jest.mocked(alertingAbilities.useAlertRuleAbility),
rulerBuilderMock: jest.mocked(ruler.rulerUrlBuilder), rulerBuilderMock: jest.mocked(ruler.rulerUrlBuilder),
}; };
@ -182,14 +190,8 @@ describe('PanelAlertTabContent', () => {
]); ]);
jest.spyOn(backendSrv, 'getFolderByUid').mockResolvedValue(mockFolder()); jest.spyOn(backendSrv, 'getFolderByUid').mockResolvedValue(mockFolder());
setupDataSources(...Object.values(dataSources));
mocks.getAllDataSources.mockReturnValue(Object.values(dataSources));
const dsService = new MockDataSourceSrv(dataSources);
dsService.datasources[dataSources.prometheus.uid] = new PrometheusDatasource(
dataSources.prometheus
) as DataSourceApi;
dsService.datasources[dataSources.default.uid] = new PrometheusDatasource(dataSources.default) as DataSourceApi;
setDataSourceSrv(dsService);
mocks.rulerBuilderMock.mockReturnValue({ mocks.rulerBuilderMock.mockReturnValue({
rules: () => ({ path: `api/ruler/${GRAFANA_RULES_SOURCE_NAME}/api/v1/rules` }), rules: () => ({ path: `api/ruler/${GRAFANA_RULES_SOURCE_NAME}/api/v1/rules` }),
namespace: () => ({ path: 'ruler' }), namespace: () => ({ path: 'ruler' }),
@ -281,11 +283,13 @@ describe('PanelAlertTabContent', () => {
}); });
it('Will take into account datasource minInterval', async () => { it('Will take into account datasource minInterval', async () => {
(getDatasourceSrv() as unknown as MockDataSourceSrv).datasources[dataSources.prometheus.uid].interval = '7m';
dashboard.panels = [ dashboard.panels = [
new PanelModel({ new PanelModel({
...panel, ...panel,
datasource: {
type: 'prometheus',
uid: dataSources.prometheusMinInterval.uid,
},
maxDataPoints: 100, maxDataPoints: 100,
}), }),
]; ];
@ -298,7 +302,7 @@ describe('PanelAlertTabContent', () => {
refId: 'A', refId: 'A',
datasource: { datasource: {
type: 'prometheus', type: 'prometheus',
uid: 'mock-ds-2', uid: 'mock-ds-4',
}, },
interval: '', interval: '',
intervalMs: 420000, intervalMs: 420000,

@ -2,7 +2,6 @@ import { of } from 'rxjs';
import { DataQueryRequest, DataSourceApi, LoadingState, PanelPlugin } from '@grafana/data'; import { DataQueryRequest, DataSourceApi, LoadingState, PanelPlugin } from '@grafana/data';
import { getPanelPlugin } from '@grafana/data/test/__mocks__/pluginMocks'; import { getPanelPlugin } from '@grafana/data/test/__mocks__/pluginMocks';
import { setDataSourceSrv } from '@grafana/runtime';
import { import {
CancelActivationHandler, CancelActivationHandler,
CustomVariable, CustomVariable,
@ -14,7 +13,8 @@ import {
SceneVariableSet, SceneVariableSet,
VizPanel, VizPanel,
} from '@grafana/scenes'; } from '@grafana/scenes';
import { mockDataSource, MockDataSourceSrv } from 'app/features/alerting/unified/mocks'; import { mockDataSource } from 'app/features/alerting/unified/mocks';
import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { DataSourceType } from 'app/features/alerting/unified/utils/datasource'; import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
import * as libAPI from 'app/features/library-panels/state/api'; import * as libAPI from 'app/features/library-panels/state/api';
@ -61,13 +61,16 @@ jest.mock('@grafana/runtime', () => ({
})); }));
const dataSources = { const dataSources = {
ds1: mockDataSource({ ds1: mockDataSource(
{
uid: 'ds1', uid: 'ds1',
type: DataSourceType.Prometheus, type: DataSourceType.Prometheus,
}), },
{ module: 'core:plugin/prometheus' }
),
}; };
setDataSourceSrv(new MockDataSourceSrv(dataSources)); setupDataSources(...Object.values(dataSources));
let deactivate: CancelActivationHandler | undefined; let deactivate: CancelActivationHandler | undefined;

@ -1,9 +1,8 @@
import { render, screen, within } from '@testing-library/react'; import { render, screen, within } from 'test/test-utils';
import userEvent from '@testing-library/user-event';
import { TestProvider } from 'test/helpers/TestProvider';
import { locationService, setAngularLoader, setDataSourceSrv } from '@grafana/runtime'; import { locationService, setAngularLoader } from '@grafana/runtime';
import { mockDataSource, MockDataSourceSrv } from 'app/features/alerting/unified/mocks'; import { mockDataSource } from 'app/features/alerting/unified/mocks';
import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { DashboardModel } from '../../state/DashboardModel'; import { DashboardModel } from '../../state/DashboardModel';
import { createDashboardModelFixture } from '../../state/__fixtures__/dashboardFixtures'; import { createDashboardModelFixture } from '../../state/__fixtures__/dashboardFixtures';
@ -18,11 +17,7 @@ function setup(dashboard: DashboardModel, editIndex?: number) {
}, },
}; };
return render( return render(<AnnotationsSettings sectionNav={sectionNav} dashboard={dashboard} editIndex={editIndex} />);
<TestProvider>
<AnnotationsSettings sectionNav={sectionNav} dashboard={dashboard} editIndex={editIndex} />
</TestProvider>
);
} }
describe('AnnotationsSettings', () => { describe('AnnotationsSettings', () => {
@ -56,7 +51,7 @@ describe('AnnotationsSettings', () => {
), ),
}; };
setDataSourceSrv(new MockDataSourceSrv(dataSources)); setupDataSources(...Object.values(dataSources));
const getTableBody = () => screen.getAllByRole('rowgroup')[1]; const getTableBody = () => screen.getAllByRole('rowgroup')[1];
const getTableBodyRows = () => within(getTableBody()).getAllByRole('row'); const getTableBodyRows = () => within(getTableBody()).getAllByRole('row');
@ -149,7 +144,7 @@ describe('AnnotationsSettings', () => {
}, },
]; ];
setup(dashboard); const { user } = setup(dashboard);
// Check that we have sorting buttons // Check that we have sorting buttons
expect(within(getTableBodyRows()[0]).queryByRole('button', { name: 'Move up' })).not.toBeInTheDocument(); expect(within(getTableBodyRows()[0]).queryByRole('button', { name: 'Move up' })).not.toBeInTheDocument();
@ -166,9 +161,9 @@ describe('AnnotationsSettings', () => {
expect(within(getTableBodyRows()[1]).queryByText(/annotation 2/i)).toBeInTheDocument(); expect(within(getTableBodyRows()[1]).queryByText(/annotation 2/i)).toBeInTheDocument();
expect(within(getTableBodyRows()[2]).queryByText(/annotation 3/i)).toBeInTheDocument(); expect(within(getTableBodyRows()[2]).queryByText(/annotation 3/i)).toBeInTheDocument();
await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'Move down' })[0]); await user.click(within(getTableBody()).getAllByRole('button', { name: 'Move down' })[0]);
await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'Move down' })[1]); await user.click(within(getTableBody()).getAllByRole('button', { name: 'Move down' })[1]);
await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'Move up' })[0]); await user.click(within(getTableBody()).getAllByRole('button', { name: 'Move up' })[0]);
// Checking if it has changed the sorting accordingly // Checking if it has changed the sorting accordingly
expect(within(getTableBodyRows()[0]).queryByText(/annotation 3/i)).toBeInTheDocument(); expect(within(getTableBodyRows()[0]).queryByText(/annotation 3/i)).toBeInTheDocument();
@ -177,9 +172,9 @@ describe('AnnotationsSettings', () => {
}); });
test('Adding a new annotation', async () => { test('Adding a new annotation', async () => {
setup(dashboard); const { user } = setup(dashboard);
await userEvent.click(screen.getByRole('button', { name: 'Add annotation query' })); await user.click(screen.getByRole('button', { name: 'Add annotation query' }));
expect(locationService.getSearchObject().editIndex).toBe('1'); expect(locationService.getSearchObject().editIndex).toBe('1');
expect(dashboard.annotations.list.length).toBe(2); expect(dashboard.annotations.list.length).toBe(2);
@ -193,18 +188,18 @@ describe('AnnotationsSettings', () => {
enable: true, enable: true,
}); });
setup(dashboard, 1); const { user } = setup(dashboard, 1);
const nameInput = screen.getByRole('textbox', { name: /name/i }); const nameInput = screen.getByRole('textbox', { name: /name/i });
await userEvent.clear(nameInput); await user.clear(nameInput);
await userEvent.type(nameInput, 'My Prometheus Annotation'); await user.type(nameInput, 'My Prometheus Annotation');
await userEvent.click(screen.getByPlaceholderText(/testdata/i)); await user.click(screen.getByPlaceholderText(/testdata/i));
expect(await screen.findByText(/Prometheus/i)).toBeVisible(); expect(await screen.findByText(/Prometheus/i)).toBeVisible();
expect(screen.queryAllByText(/testdata/i)).toHaveLength(1); expect(screen.queryAllByText(/testdata/i)).toHaveLength(1);
await userEvent.click(screen.getByText(/prometheus/i)); await user.click(screen.getByText(/prometheus/i));
expect(screen.getByRole('checkbox', { name: /hidden/i })).not.toBeChecked(); expect(screen.getByRole('checkbox', { name: /hidden/i })).not.toBeChecked();
}); });
@ -222,9 +217,9 @@ describe('AnnotationsSettings', () => {
type: 'dashboard', type: 'dashboard',
}, },
]; ];
setup(dashboard, 1); // Edit the not built-in annotations const { user } = setup(dashboard, 1); // Edit the not built-in annotations
await userEvent.click(screen.getByRole('button', { name: 'Delete' })); await user.click(screen.getByRole('button', { name: 'Delete' }));
expect(locationService.getSearchObject().editIndex).toBe(undefined); expect(locationService.getSearchObject().editIndex).toBe(undefined);
expect(dashboard.annotations.list.length).toBe(1); // started with two expect(dashboard.annotations.list.length).toBe(1); // started with two

@ -2,11 +2,11 @@ import { each, map } from 'lodash';
import { DataLinkBuiltInVars, MappingType, VariableHide } from '@grafana/data'; import { DataLinkBuiltInVars, MappingType, VariableHide } from '@grafana/data';
import { getPanelPlugin } from '@grafana/data/test/__mocks__/pluginMocks'; import { getPanelPlugin } from '@grafana/data/test/__mocks__/pluginMocks';
import { setDataSourceSrv } from '@grafana/runtime';
import { FieldConfigSource } from '@grafana/schema'; import { FieldConfigSource } from '@grafana/schema';
import { config } from 'app/core/config'; import { config } from 'app/core/config';
import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN } from 'app/core/constants'; import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN } from 'app/core/constants';
import { mockDataSource, MockDataSourceSrv } from 'app/features/alerting/unified/mocks'; import { mockDataSource } from 'app/features/alerting/unified/mocks';
import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { MIXED_DATASOURCE_NAME } from 'app/plugins/datasource/mixed/MixedDataSource'; import { MIXED_DATASOURCE_NAME } from 'app/plugins/datasource/mixed/MixedDataSource';
import { DashboardModel } from '../state/DashboardModel'; import { DashboardModel } from '../state/DashboardModel';
@ -41,7 +41,7 @@ const dataSources = {
}), }),
}; };
setDataSourceSrv(new MockDataSourceSrv(dataSources)); setupDataSources(...Object.values(dataSources));
describe('DashboardModel', () => { describe('DashboardModel', () => {
describe('when creating dashboard with old schema', () => { describe('when creating dashboard with old schema', () => {

@ -1,5 +1,5 @@
import { dateTime, QueryVariableModel, TimeRange, TypedVariableModel } from '@grafana/data'; import { dateTime, QueryVariableModel, TimeRange, TypedVariableModel } from '@grafana/data';
import { setDataSourceSrv, VariableInterpolation } from '@grafana/runtime'; import { VariableInterpolation } from '@grafana/runtime';
import { import {
ConstantVariable, ConstantVariable,
CustomVariable, CustomVariable,
@ -13,10 +13,11 @@ import {
TestVariable, TestVariable,
} from '@grafana/scenes'; } from '@grafana/scenes';
import { VariableFormatID } from '@grafana/schema'; import { VariableFormatID } from '@grafana/schema';
import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { silenceConsoleOutput } from '../../../test/core/utils/silenceConsoleOutput'; import { silenceConsoleOutput } from '../../../test/core/utils/silenceConsoleOutput';
import { initTemplateSrv } from '../../../test/helpers/initTemplateSrv'; import { initTemplateSrv } from '../../../test/helpers/initTemplateSrv';
import { mockDataSource, MockDataSourceSrv } from '../alerting/unified/mocks'; import { mockDataSource } from '../alerting/unified/mocks';
import { VariableAdapter, variableAdapters } from '../variables/adapters'; import { VariableAdapter, variableAdapters } from '../variables/adapters';
import { createAdHocVariableAdapter } from '../variables/adhoc/adapter'; import { createAdHocVariableAdapter } from '../variables/adhoc/adapter';
import { createQueryVariableAdapter } from '../variables/query/adapter'; import { createQueryVariableAdapter } from '../variables/query/adapter';
@ -218,16 +219,14 @@ describe('templateSrv', () => {
{ type: 'adhoc', name: 'test', datasource: { uid: 'oogle' }, filters: [1] }, { type: 'adhoc', name: 'test', datasource: { uid: 'oogle' }, filters: [1] },
{ type: 'adhoc', name: 'test2', datasource: { uid: '$ds' }, filters: [2] }, { type: 'adhoc', name: 'test2', datasource: { uid: '$ds' }, filters: [2] },
]); ]);
setDataSourceSrv( setupDataSources(
new MockDataSourceSrv({ mockDataSource({
oogle: mockDataSource({
name: 'oogle', name: 'oogle',
uid: 'oogle', uid: 'oogle',
}), }),
logstash: mockDataSource({ mockDataSource({
name: 'logstash', name: 'logstash',
uid: 'logstash-id', uid: 'logstash-id',
}),
}) })
); );
}); });

@ -1,9 +1,10 @@
import { VariableHide } from '@grafana/data'; import { VariableHide } from '@grafana/data';
import { locationService, setDataSourceSrv } from '@grafana/runtime'; import { locationService } from '@grafana/runtime';
import { AdHocFiltersVariable, ConstantVariable, sceneGraph } from '@grafana/scenes'; import { AdHocFiltersVariable, ConstantVariable, sceneGraph } from '@grafana/scenes';
import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { DataSourceType } from 'app/features/alerting/unified/utils/datasource'; import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
import { MockDataSourceSrv, mockDataSource } from '../alerting/unified/mocks'; import { mockDataSource } from '../alerting/unified/mocks';
import { activateFullSceneTree } from '../dashboard-scene/utils/test-utils'; import { activateFullSceneTree } from '../dashboard-scene/utils/test-utils';
import { DataTrail } from './DataTrail'; import { DataTrail } from './DataTrail';
@ -26,12 +27,11 @@ jest.mock('./otel/api', () => ({
describe('DataTrail', () => { describe('DataTrail', () => {
beforeAll(() => { beforeAll(() => {
jest.spyOn(DataTrail.prototype, 'checkDataSourceForOTelResources').mockImplementation(() => Promise.resolve()); jest.spyOn(DataTrail.prototype, 'checkDataSourceForOTelResources').mockImplementation(() => Promise.resolve());
setDataSourceSrv(
new MockDataSourceSrv({ setupDataSources(
prom: mockDataSource({ mockDataSource({
name: 'Prometheus', name: 'Prometheus',
type: DataSourceType.Prometheus, type: DataSourceType.Prometheus,
}),
}) })
); );
}); });

@ -1,8 +1,9 @@
import { locationService, setDataSourceSrv } from '@grafana/runtime'; import { locationService } from '@grafana/runtime';
import { AdHocFiltersVariable, sceneGraph, sceneUtils } from '@grafana/scenes'; import { AdHocFiltersVariable, sceneGraph, sceneUtils } from '@grafana/scenes';
import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { DataSourceType } from 'app/features/alerting/unified/utils/datasource'; import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
import { MockDataSourceSrv, mockDataSource } from '../../alerting/unified/mocks'; import { mockDataSource } from '../../alerting/unified/mocks';
import { DataTrail } from '../DataTrail'; import { DataTrail } from '../DataTrail';
import { TRAIL_BOOKMARKS_KEY, RECENT_TRAILS_KEY, VAR_FILTERS } from '../shared'; import { TRAIL_BOOKMARKS_KEY, RECENT_TRAILS_KEY, VAR_FILTERS } from '../shared';
@ -31,13 +32,10 @@ describe('TrailStore', () => {
jest.useFakeTimers(); jest.useFakeTimers();
// Having the mock service set up is required for activating the loaded trails // Having the mock service set up is required for activating the loaded trails
setDataSourceSrv( setupDataSources(
new MockDataSourceSrv({ mockDataSource({
prom: mockDataSource({
name: 'Prometheus', name: 'Prometheus',
type: DataSourceType.Prometheus, type: DataSourceType.Prometheus,
uid: 'ds',
}),
}) })
); );
}); });

@ -1,7 +1,8 @@
import { AdHocVariableFilter, MetricFindValue } from '@grafana/data'; import { AdHocVariableFilter, MetricFindValue } from '@grafana/data';
import { locationService, setDataSourceSrv } from '@grafana/runtime'; import { locationService } from '@grafana/runtime';
import { AdHocFiltersVariable, ConstantVariable, sceneGraph } from '@grafana/scenes'; import { AdHocFiltersVariable, ConstantVariable, sceneGraph } from '@grafana/scenes';
import { mockDataSource, MockDataSourceSrv } from 'app/features/alerting/unified/mocks'; import { mockDataSource } from 'app/features/alerting/unified/mocks';
import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { DataSourceType } from 'app/features/alerting/unified/utils/datasource'; import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
import { activateFullSceneTree } from 'app/features/dashboard-scene/utils/test-utils'; import { activateFullSceneTree } from 'app/features/dashboard-scene/utils/test-utils';
@ -205,12 +206,10 @@ describe('updateOtelJoinWithGroupLeft', () => {
beforeEach(() => { beforeEach(() => {
jest.spyOn(DataTrail.prototype, 'checkDataSourceForOTelResources').mockImplementation(() => Promise.resolve()); jest.spyOn(DataTrail.prototype, 'checkDataSourceForOTelResources').mockImplementation(() => Promise.resolve());
setDataSourceSrv( setupDataSources(
new MockDataSourceSrv({ mockDataSource({
prom: mockDataSource({
name: 'Prometheus', name: 'Prometheus',
type: DataSourceType.Prometheus, type: DataSourceType.Prometheus,
}),
}) })
); );
trail = new DataTrail({ trail = new DataTrail({
@ -274,12 +273,11 @@ describe('getProdOrDefaultEnv', () => {
describe('util functions that rely on trail and variable setup', () => { describe('util functions that rely on trail and variable setup', () => {
beforeAll(() => { beforeAll(() => {
jest.spyOn(DataTrail.prototype, 'checkDataSourceForOTelResources').mockImplementation(() => Promise.resolve()); jest.spyOn(DataTrail.prototype, 'checkDataSourceForOTelResources').mockImplementation(() => Promise.resolve());
setDataSourceSrv(
new MockDataSourceSrv({ setupDataSources(
prom: mockDataSource({ mockDataSource({
name: 'Prometheus', name: 'Prometheus',
type: DataSourceType.Prometheus, type: DataSourceType.Prometheus,
}),
}) })
); );
}); });
@ -332,6 +330,12 @@ describe('util functions that rely on trail and variable setup', () => {
useOtelExperience: true, useOtelExperience: true,
nonPromotedOtelResources, nonPromotedOtelResources,
}); });
setupDataSources(
mockDataSource({
name: 'Prometheus',
type: DataSourceType.Prometheus,
})
);
locationService.push(preTrailUrl); locationService.push(preTrailUrl);
activateFullSceneTree(trail); activateFullSceneTree(trail);
getOtelGroupLeftVar(trail).setState({ value: 'attribute1,attribute2' }); getOtelGroupLeftVar(trail).setState({ value: 'attribute1,attribute2' });

@ -2,8 +2,8 @@ import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import { getDefaultTimeRange, LoadingState } from '@grafana/data'; import { getDefaultTimeRange, LoadingState } from '@grafana/data';
import { setDataSourceSrv } from '@grafana/runtime'; import { mockDataSource } from 'app/features/alerting/unified/mocks';
import { mockDataSource, MockDataSourceSrv } from 'app/features/alerting/unified/mocks'; import { setupDataSources } from 'app/features/alerting/unified/testSetup/datasources';
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv'; import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import { DashboardModel } from 'app/features/dashboard/state/DashboardModel'; import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
@ -30,11 +30,7 @@ jest.mock('app/core/config', () => ({
}, },
})); }));
setDataSourceSrv( setupDataSources(mockDataSource({ isDefault: true }));
new MockDataSourceSrv({
test: mockDataSource({ isDefault: true }),
})
);
describe('DashboardQueryEditor', () => { describe('DashboardQueryEditor', () => {
const mockOnChange = jest.fn(); const mockOnChange = jest.fn();

Loading…
Cancel
Save