diff --git a/src/plugins/dashboard/common/dashboard_saved_object/persistable_state/dashboard_saved_object_references.ts b/src/plugins/dashboard/common/dashboard_saved_object/persistable_state/dashboard_saved_object_references.ts index 80644fa94dc36..1ede56a2b67a7 100644 --- a/src/plugins/dashboard/common/dashboard_saved_object/persistable_state/dashboard_saved_object_references.ts +++ b/src/plugins/dashboard/common/dashboard_saved_object/persistable_state/dashboard_saved_object_references.ts @@ -16,6 +16,10 @@ import { } from '../../lib/dashboard_panel_converters'; import { DashboardAttributesAndReferences, ParsedDashboardAttributesWithType } from '../../types'; import { DashboardAttributes, SavedDashboardPanel } from '../../content_management'; +import { + createExtract, + createInject, +} from '../../dashboard_container/persistable_state/dashboard_container_references'; export interface InjectExtractDeps { embeddablePersistableStateService: EmbeddablePersistableStateService; @@ -45,10 +49,8 @@ export function injectReferences( const parsedAttributes = parseDashboardAttributesWithType(attributes); // inject references back into panels via the Embeddable persistable state service. - const injectedState = deps.embeddablePersistableStateService.inject( - parsedAttributes, - references - ) as ParsedDashboardAttributesWithType; + const inject = createInject(deps.embeddablePersistableStateService); + const injectedState = inject(parsedAttributes, references) as ParsedDashboardAttributesWithType; const injectedPanels = convertPanelMapToSavedPanels(injectedState.panels); const newAttributes = { @@ -74,11 +76,11 @@ export function extractReferences( ); } - const { references: extractedReferences, state: extractedState } = - deps.embeddablePersistableStateService.extract(parsedAttributes) as { - references: Reference[]; - state: ParsedDashboardAttributesWithType; - }; + const extract = createExtract(deps.embeddablePersistableStateService); + const { references: extractedReferences, state: extractedState } = extract(parsedAttributes) as { + references: Reference[]; + state: ParsedDashboardAttributesWithType; + }; const extractedPanels = convertPanelMapToSavedPanels(extractedState.panels); const newAttributes = { diff --git a/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.test.tsx b/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.test.tsx index fd41fdd5e764d..6a81a8c4fd601 100644 --- a/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.test.tsx +++ b/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.test.tsx @@ -18,11 +18,12 @@ import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/common'; import { setStubKibanaServices as setPresentationPanelMocks } from '@kbn/presentation-panel-plugin/public/mocks'; import { BehaviorSubject } from 'rxjs'; import { DashboardContainerFactory } from '..'; -import { DASHBOARD_CONTAINER_TYPE, DashboardCreationOptions } from '../..'; -import { embeddableService } from '../../services/kibana_services'; +import { DashboardCreationOptions } from '../..'; import { DashboardContainer } from '../embeddable/dashboard_container'; import { DashboardRenderer } from './dashboard_renderer'; +jest.mock('../embeddable/dashboard_container_factory', () => ({})); + describe('dashboard renderer', () => { let mockDashboardContainer: DashboardContainer; let mockDashboardFactory: DashboardContainerFactory; @@ -38,7 +39,10 @@ describe('dashboard renderer', () => { mockDashboardFactory = { create: jest.fn().mockReturnValue(mockDashboardContainer), } as unknown as DashboardContainerFactory; - embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockDashboardFactory); + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest + .fn() + .mockReturnValue(mockDashboardFactory); setPresentationPanelMocks(); }); @@ -46,7 +50,6 @@ describe('dashboard renderer', () => { await act(async () => { mountWithIntl(); }); - expect(embeddableService.getEmbeddableFactory).toHaveBeenCalledWith(DASHBOARD_CONTAINER_TYPE); expect(mockDashboardFactory.create).toHaveBeenCalled(); }); @@ -103,7 +106,10 @@ describe('dashboard renderer', () => { mockDashboardFactory = { create: jest.fn().mockReturnValue(mockErrorEmbeddable), } as unknown as DashboardContainerFactory; - embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockDashboardFactory); + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest + .fn() + .mockReturnValue(mockDashboardFactory); let wrapper: ReactWrapper; await act(async () => { @@ -125,7 +131,10 @@ describe('dashboard renderer', () => { const mockErrorFactory = { create: jest.fn().mockReturnValue(mockErrorEmbeddable), } as unknown as DashboardContainerFactory; - embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockErrorFactory); + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest + .fn() + .mockReturnValue(mockErrorFactory); // render the dashboard - it should run into an error and render the error embeddable. let wrapper: ReactWrapper; @@ -146,7 +155,10 @@ describe('dashboard renderer', () => { const mockSuccessFactory = { create: jest.fn().mockReturnValue(mockSuccessEmbeddable), } as unknown as DashboardContainerFactory; - embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockSuccessFactory); + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest + .fn() + .mockReturnValue(mockSuccessFactory); // update the saved object id to trigger another dashboard load. await act(async () => { @@ -175,7 +187,10 @@ describe('dashboard renderer', () => { const mockErrorFactory = { create: jest.fn().mockReturnValue(mockErrorEmbeddable), } as unknown as DashboardContainerFactory; - embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockErrorFactory); + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest + .fn() + .mockReturnValue(mockErrorFactory); // render the dashboard - it should run into an error and render the error embeddable. let wrapper: ReactWrapper; @@ -238,7 +253,10 @@ describe('dashboard renderer', () => { const mockSuccessFactory = { create: jest.fn().mockReturnValue(mockSuccessEmbeddable), } as unknown as DashboardContainerFactory; - embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockSuccessFactory); + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest + .fn() + .mockReturnValue(mockSuccessFactory); let wrapper: ReactWrapper; await act(async () => { @@ -263,7 +281,10 @@ describe('dashboard renderer', () => { const mockUseMarginFalseFactory = { create: jest.fn().mockReturnValue(mockUseMarginFalseEmbeddable), } as unknown as DashboardContainerFactory; - embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockUseMarginFalseFactory); + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest + .fn() + .mockReturnValue(mockUseMarginFalseFactory); let wrapper: ReactWrapper; await act(async () => { diff --git a/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.tsx b/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.tsx index a43bd6ddbc75b..40b54e42e6ffa 100644 --- a/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.tsx +++ b/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.tsx @@ -20,15 +20,11 @@ import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/common'; import { useStateFromPublishingSubject } from '@kbn/presentation-publishing'; import { LocatorPublic } from '@kbn/share-plugin/common'; -import { DASHBOARD_CONTAINER_TYPE } from '..'; import { DashboardContainerInput } from '../../../common'; import { DashboardApi } from '../../dashboard_api/types'; import { embeddableService, screenshotModeService } from '../../services/kibana_services'; import type { DashboardContainer } from '../embeddable/dashboard_container'; -import { - DashboardContainerFactory, - DashboardContainerFactoryDefinition, -} from '../embeddable/dashboard_container_factory'; +import { DashboardContainerFactoryDefinition } from '../embeddable/dashboard_container_factory'; import type { DashboardCreationOptions } from '../..'; import { DashboardLocatorParams, DashboardRedirect } from '../types'; import { Dashboard404Page } from './dashboard_404'; @@ -91,12 +87,8 @@ export function DashboardRenderer({ (async () => { const creationOptions = await getCreationOptions?.(); - const dashboardFactory = embeddableService.getEmbeddableFactory( - DASHBOARD_CONTAINER_TYPE - ) as DashboardContainerFactory & { - create: DashboardContainerFactoryDefinition['create']; - }; - const container = await dashboardFactory?.create( + const dashboardFactory = new DashboardContainerFactoryDefinition(embeddableService); + const container = await dashboardFactory.create( { id } as unknown as DashboardContainerInput, // Input from creationOptions is used instead. undefined, creationOptions, diff --git a/src/plugins/dashboard/public/dashboard_container/index.ts b/src/plugins/dashboard/public/dashboard_container/index.ts index 16314f52d38f8..b4ecb30f3c25d 100644 --- a/src/plugins/dashboard/public/dashboard_container/index.ts +++ b/src/plugins/dashboard/public/dashboard_container/index.ts @@ -15,10 +15,7 @@ export const DASHBOARD_CONTAINER_TYPE = 'dashboard'; export const LATEST_DASHBOARD_CONTAINER_VERSION = convertNumberToDashboardVersion(LATEST_VERSION); export type { DashboardContainer } from './embeddable/dashboard_container'; -export { - type DashboardContainerFactory, - DashboardContainerFactoryDefinition, -} from './embeddable/dashboard_container_factory'; +export { type DashboardContainerFactory } from './embeddable/dashboard_container_factory'; export { LazyDashboardRenderer } from './external_api/lazy_dashboard_renderer'; export type { DashboardLocatorParams } from './types'; diff --git a/src/plugins/dashboard/public/plugin.tsx b/src/plugins/dashboard/public/plugin.tsx index b1d60adc84d0f..0957bf9364524 100644 --- a/src/plugins/dashboard/public/plugin.tsx +++ b/src/plugins/dashboard/public/plugin.tsx @@ -72,7 +72,6 @@ import { LEGACY_DASHBOARD_APP_ID, SEARCH_SESSION_ID, } from './dashboard_constants'; -import { DashboardContainerFactoryDefinition } from './dashboard_container/embeddable/dashboard_container_factory'; import { GetPanelPlacementSettings, registerDashboardPanelPlacementSetting, @@ -227,14 +226,6 @@ export class DashboardPlugin }, }); - core.getStartServices().then(([, deps]) => { - const dashboardContainerFactory = new DashboardContainerFactoryDefinition(deps.embeddable); - embeddable.registerEmbeddableFactory( - dashboardContainerFactory.type, - dashboardContainerFactory - ); - }); - this.stopUrlTracking = () => { stopUrlTracker(); };