From 4e10507c847c74da8b08ff224220e99a066d74f2 Mon Sep 17 00:00:00 2001 From: Tom Ratcliffe Date: Wed, 11 Dec 2024 14:50:49 +0000 Subject: [PATCH] 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 --- .../angular/panel/specs/metrics_panel_ctrl.test.ts | 6 ------ .../app/core/history/RichHistoryRemoteStorage.test.ts | 3 +++ public/app/core/reducers/root.test.ts | 11 ----------- public/app/core/utils/explore.test.ts | 2 +- .../serialization/buildNewDashboardSaveModel.test.ts | 1 + .../explore/hooks/useStateSync/internal.utils.ts | 2 +- .../explore/hooks/useStateSync/migrators/v0.test.ts | 2 +- .../explore/hooks/useStateSync/migrators/v0.ts | 2 +- .../explore/hooks/useStateSync/migrators/v1.test.ts | 2 +- .../explore/hooks/useStateSync/migrators/v1.ts | 2 +- public/app/features/explore/state/constants.ts | 6 ++++++ public/app/features/explore/state/main.ts | 9 +++++---- public/app/features/explore/state/utils.ts | 9 +++------ .../features/panel/state/getAllSuggestions.test.ts | 3 --- .../app/features/plugins/tests/plugin_loader.test.ts | 3 --- public/app/features/teams/TeamPages.test.tsx | 1 - public/test/jest-setup.ts | 1 - 17 files changed, 24 insertions(+), 41 deletions(-) create mode 100644 public/app/features/explore/state/constants.ts diff --git a/public/app/angular/panel/specs/metrics_panel_ctrl.test.ts b/public/app/angular/panel/specs/metrics_panel_ctrl.test.ts index 04f72d46fe4..da8129dde1f 100644 --- a/public/app/angular/panel/specs/metrics_panel_ctrl.test.ts +++ b/public/app/angular/panel/specs/metrics_panel_ctrl.test.ts @@ -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', - }, }; }); diff --git a/public/app/core/history/RichHistoryRemoteStorage.test.ts b/public/app/core/history/RichHistoryRemoteStorage.test.ts index 31d5c726834..766712d1c0a 100644 --- a/public/app/core/history/RichHistoryRemoteStorage.test.ts +++ b/public/app/core/history/RichHistoryRemoteStorage.test.ts @@ -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; diff --git a/public/app/core/reducers/root.test.ts b/public/app/core/reducers/root.test.ts index 3396fc0d18c..6b584776233 100644 --- a/public/app/core/reducers/root.test.ts +++ b/public/app/core/reducers/root.test.ts @@ -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(); diff --git a/public/app/core/utils/explore.test.ts b/public/app/core/utils/explore.test.ts index 75da07f4577..76da92421a9 100644 --- a/public/app/core/utils/explore.test.ts +++ b/public/app/core/utils/explore.test.ts @@ -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'; diff --git a/public/app/features/dashboard-scene/serialization/buildNewDashboardSaveModel.test.ts b/public/app/features/dashboard-scene/serialization/buildNewDashboardSaveModel.test.ts index 520cc777009..bfe7a4b658d 100644 --- a/public/app/features/dashboard-scene/serialization/buildNewDashboardSaveModel.test.ts +++ b/public/app/features/dashboard-scene/serialization/buildNewDashboardSaveModel.test.ts @@ -45,6 +45,7 @@ jest.mock('@grafana/runtime', () => ({ newDashboardWithFiltersAndGroupBy: false, }, bootData: { + ...jest.requireActual('@grafana/runtime').config.bootData, user: { timezone: 'Africa/Abidjan', }, diff --git a/public/app/features/explore/hooks/useStateSync/internal.utils.ts b/public/app/features/explore/hooks/useStateSync/internal.utils.ts index e77445939a8..1a12484d533 100644 --- a/public/app/features/explore/hooks/useStateSync/internal.utils.ts +++ b/public/app/features/explore/hooks/useStateSync/internal.utils.ts @@ -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'; diff --git a/public/app/features/explore/hooks/useStateSync/migrators/v0.test.ts b/public/app/features/explore/hooks/useStateSync/migrators/v0.test.ts index bde44b7c2e3..84544301aa3 100644 --- a/public/app/features/explore/hooks/useStateSync/migrators/v0.test.ts +++ b/public/app/features/explore/hooks/useStateSync/migrators/v0.test.ts @@ -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'; diff --git a/public/app/features/explore/hooks/useStateSync/migrators/v0.ts b/public/app/features/explore/hooks/useStateSync/migrators/v0.ts index a2948163f00..0fdf4b9f548 100644 --- a/public/app/features/explore/hooks/useStateSync/migrators/v0.ts +++ b/public/app/features/explore/hooks/useStateSync/migrators/v0.ts @@ -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'; diff --git a/public/app/features/explore/hooks/useStateSync/migrators/v1.test.ts b/public/app/features/explore/hooks/useStateSync/migrators/v1.test.ts index f436ecc5f00..810d8800d65 100644 --- a/public/app/features/explore/hooks/useStateSync/migrators/v1.test.ts +++ b/public/app/features/explore/hooks/useStateSync/migrators/v1.test.ts @@ -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'; diff --git a/public/app/features/explore/hooks/useStateSync/migrators/v1.ts b/public/app/features/explore/hooks/useStateSync/migrators/v1.ts index 57b23ce4076..58fe595e49a 100644 --- a/public/app/features/explore/hooks/useStateSync/migrators/v1.ts +++ b/public/app/features/explore/hooks/useStateSync/migrators/v1.ts @@ -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'; diff --git a/public/app/features/explore/state/constants.ts b/public/app/features/explore/state/constants.ts new file mode 100644 index 00000000000..a4c20f15e0b --- /dev/null +++ b/public/app/features/explore/state/constants.ts @@ -0,0 +1,6 @@ +import { config } from '@grafana/runtime'; + +export const DEFAULT_RANGE = { + from: `now-${config.exploreDefaultTimeOffset}`, + to: 'now', +}; diff --git a/public/app/features/explore/state/main.ts b/public/app/features/explore/state/main.ts index 3e1375434c9..ed3e789cecc 100644 --- a/public/app/features/explore/state/main.ts +++ b/public/app/features/explore/state/main.ts @@ -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 }), {} ); diff --git a/public/app/features/explore/state/utils.ts b/public/app/features/explore/state/utils.ts index 8b2c6888c3f..45b8b39e0e4 100644 --- a/public/app/features/explore/state/utils.ts +++ b/public/app/features/explore/state/utils.ts @@ -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 => { diff --git a/public/app/features/panel/state/getAllSuggestions.test.ts b/public/app/features/panel/state/getAllSuggestions.test.ts index 17b236ed1ec..552c80a74b5 100644 --- a/public/app/features/panel/state/getAllSuggestions.test.ts +++ b/public/app/features/panel/state/getAllSuggestions.test.ts @@ -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}`, diff --git a/public/app/features/plugins/tests/plugin_loader.test.ts b/public/app/features/plugins/tests/plugin_loader.test.ts index d41caa1dbe1..9f6c2e3739b 100644 --- a/public/app/features/plugins/tests/plugin_loader.test.ts +++ b/public/app/features/plugins/tests/plugin_loader.test.ts @@ -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: { diff --git a/public/app/features/teams/TeamPages.test.tsx b/public/app/features/teams/TeamPages.test.tsx index f3a83495df5..f2d5c4674a9 100644 --- a/public/app/features/teams/TeamPages.test.tsx +++ b/public/app/features/teams/TeamPages.test.tsx @@ -30,7 +30,6 @@ jest.mock('@grafana/runtime', () => ({ licenseUrl: '', }, featureToggles: { accesscontrol: true }, - bootData: { navTree: [], user: {} }, buildInfo: { edition: 'Open Source', version: '7.5.0', diff --git a/public/test/jest-setup.ts b/public/test/jest-setup.ts index f625756bc8b..9511a3e6a97 100644 --- a/public/test/jest-setup.ts +++ b/public/test/jest-setup.ts @@ -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) => {