Chore: Remove global mock of plugin_loader (#97351)

* Remove global mock of `plugin_loader`

Removing this means we can get more accurate datasources behaviour in tests

* Fix circular dependency/undefined method when plugin_loader is unmocked

* Use optional chaining for trusted policies to stop tests failing when `bootData` is partially set

* Add plugin_loader mock back into single test that is still broken

* Revert trusted type policies changes

* Fix tests that break with trusted type policies
This commit is contained in:
Tom Ratcliffe 2024-12-11 14:50:49 +00:00 committed by GitHub
parent 1a22575613
commit 4e10507c84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 24 additions and 41 deletions

View File

@ -2,18 +2,12 @@ jest.mock('app/core/core', () => ({}));
jest.mock('app/core/config', () => {
return {
...jest.requireActual('app/core/config'),
bootData: {
user: {},
},
panels: {
test: {
id: 'test',
name: 'test',
},
},
config: {
appSubUrl: 'test',
},
};
});

View File

@ -44,6 +44,9 @@ jest.mock('../services/PreferencesService', () => ({
},
}));
// FIXME: Tests break unless plugin loader is mocked. This is likely due to a circular dependency
jest.mock('app/features/plugins/plugin_loader', () => ({}));
describe('RichHistoryRemoteStorage', () => {
let storage: RichHistoryRemoteStorage;

View File

@ -6,17 +6,6 @@ import { cleanUpAction } from '../actions/cleanUp';
import { createRootReducer } from './root';
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
config: {
...jest.requireActual('@grafana/runtime').config,
bootData: {
navTree: [],
user: {},
},
},
}));
describe('rootReducer', () => {
const rootReducer = createRootReducer();

View File

@ -4,7 +4,7 @@ import { config } from '@grafana/runtime';
import { DataQuery } from '@grafana/schema';
import { RefreshPicker } from '@grafana/ui';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { DEFAULT_RANGE } from 'app/features/explore/state/utils';
import { DEFAULT_RANGE } from 'app/features/explore/state/constants';
import { getVariablesUrlParams } from 'app/features/variables/getAllVariableValuesForUrl';
import { DatasourceSrvMock, MockDataSourceApi } from '../../../test/mocks/datasource_srv';

View File

@ -45,6 +45,7 @@ jest.mock('@grafana/runtime', () => ({
newDashboardWithFiltersAndGroupBy: false,
},
bootData: {
...jest.requireActual('@grafana/runtime').config.bootData,
user: {
timezone: 'Africa/Abidjan',
},

View File

@ -3,10 +3,10 @@ import { isEqual } from 'lodash';
import { CoreApp, DataSourceApi, ExploreUrlState, isTruthy } from '@grafana/data';
import { DataQuery, DataSourceRef } from '@grafana/schema';
import { getLastUsedDatasourceUID } from 'app/core/utils/explore';
import { DEFAULT_RANGE } from 'app/features/explore/state/utils';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
import { MIXED_DATASOURCE_NAME } from 'app/plugins/datasource/mixed/MixedDataSource';
import { DEFAULT_RANGE } from '../../state/constants';
import { isFulfilled } from '../utils';
export type InitState = 'pending' | 'done' | 'notstarted';

View File

@ -1,4 +1,4 @@
import { DEFAULT_RANGE } from 'app/features/explore/state/utils';
import { DEFAULT_RANGE } from 'app/features/explore/state/constants';
import { v0Migrator } from './v0';

View File

@ -1,5 +1,5 @@
import { ExploreUrlState } from '@grafana/data';
import { DEFAULT_RANGE } from 'app/features/explore/state/utils';
import { DEFAULT_RANGE } from 'app/features/explore/state/constants';
import { BaseExploreURL, MigrationHandler } from './types';

View File

@ -1,4 +1,4 @@
import { DEFAULT_RANGE } from 'app/features/explore/state/utils';
import { DEFAULT_RANGE } from 'app/features/explore/state/constants';
import { v1Migrator } from './v1';

View File

@ -1,6 +1,6 @@
import { ExploreUrlState } from '@grafana/data';
import { ID_ALPHABET, generateExploreId } from 'app/core/utils/explore';
import { DEFAULT_RANGE } from 'app/features/explore/state/utils';
import { DEFAULT_RANGE } from 'app/features/explore/state/constants';
import { hasKey } from '../../utils';

View File

@ -0,0 +1,6 @@
import { config } from '@grafana/runtime';
export const DEFAULT_RANGE = {
from: `now-${config.exploreDefaultTimeOffset}`,
to: 'now',
};

View File

@ -14,8 +14,9 @@ import { RichHistorySearchFilters, RichHistorySettings } from '../../../core/uti
import { createAsyncThunk, ThunkResult } from '../../../types';
import { withUniqueRefIds } from '../utils/queries';
import { DEFAULT_RANGE } from './constants';
import { initializeExplore, InitializeExploreOptions, paneReducer } from './explorePane';
import { DEFAULT_RANGE, makeExplorePaneState } from './utils';
import { makeExplorePaneState } from './utils';
//
// Actions and Payloads
@ -157,7 +158,7 @@ export const navigateToExplore = (
/**
* Global Explore state that handles multiple Explore areas and the split state
*/
const initialExploreItemState = makeExplorePaneState();
const initialExploreItemState = () => makeExplorePaneState();
export const initialExploreState: ExploreState = {
syncedTimes: false,
panes: {},
@ -265,7 +266,7 @@ export const exploreReducer = (state = initialExploreState, action: AnyAction):
...state,
panes: {
...state.panes,
[action.meta.arg.exploreId]: initialExploreItemState,
[action.meta.arg.exploreId]: initialExploreItemState(),
},
};
}
@ -274,7 +275,7 @@ export const exploreReducer = (state = initialExploreState, action: AnyAction):
const initialPanes = Object.entries(state.panes);
const before = initialPanes.slice(0, action.meta.arg.position);
const after = initialPanes.slice(before.length);
const panes = [...before, [action.meta.arg.exploreId, initialExploreItemState] as const, ...after].reduce(
const panes = [...before, [action.meta.arg.exploreId, initialExploreItemState()] as const, ...after].reduce(
(acc, [id, pane]) => ({ ...acc, [id]: pane }),
{}
);

View File

@ -20,7 +20,7 @@ import {
URLRange,
URLRangeValue,
} from '@grafana/data';
import { config, getDataSourceSrv } from '@grafana/runtime';
import { getDataSourceSrv } from '@grafana/runtime';
import { DataQuery, DataSourceJsonData, DataSourceRef, TimeZone } from '@grafana/schema';
import { getLocalRichHistoryStorage } from 'app/core/history/richHistoryStorageProvider';
import { SortOrder } from 'app/core/utils/richHistoryTypes';
@ -33,12 +33,9 @@ import { setLastUsedDatasourceUID } from '../../../core/utils/explore';
import { getDatasourceSrv } from '../../plugins/datasource_srv';
import { loadSupplementaryQueries } from '../utils/supplementaryQueries';
export const MAX_HISTORY_AUTOCOMPLETE_ITEMS = 100;
import { DEFAULT_RANGE } from './constants';
export const DEFAULT_RANGE = {
from: `now-${config.exploreDefaultTimeOffset}`,
to: 'now',
};
export const MAX_HISTORY_AUTOCOMPLETE_ITEMS = 100;
const GRAPH_STYLE_KEY = 'grafana.explore.style.graph';
export const storeGraphStyle = (graphStyle: string): void => {

View File

@ -13,9 +13,6 @@ import { SuggestionName } from 'app/types/suggestions';
import { getAllSuggestions, panelsToCheckFirst } from './getAllSuggestions';
jest.unmock('app/core/core');
jest.unmock('app/features/plugins/plugin_loader');
for (const pluginId of panelsToCheckFirst) {
config.panels[pluginId] = {
module: `core:plugin/${pluginId}`,

View File

@ -1,6 +1,3 @@
// Use the real plugin_loader (stubbed by default)
jest.unmock('app/features/plugins/plugin_loader');
jest.mock('app/core/core', () => {
return {
coreModule: {

View File

@ -30,7 +30,6 @@ jest.mock('@grafana/runtime', () => ({
licenseUrl: '',
},
featureToggles: { accesscontrol: true },
bootData: { navTree: [], user: {} },
buildInfo: {
edition: 'Open Source',
version: '7.5.0',

View File

@ -78,7 +78,6 @@ jest.mock('../app/core/core', () => ({
appEvents: testAppEvents,
}));
jest.mock('../app/angular/partials', () => ({}));
jest.mock('../app/features/plugins/plugin_loader', () => ({}));
const throwUnhandledRejections = () => {
process.on('unhandledRejection', (err) => {