Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Use dashboard factory directly instead of pulling from registry (#193480) #195615

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,15 +39,17 @@ 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();
});

test('calls create method on the Dashboard embeddable factory', async () => {
await act(async () => {
mountWithIntl(<DashboardRenderer />);
});
expect(embeddableService.getEmbeddableFactory).toHaveBeenCalledWith(DASHBOARD_CONTAINER_TYPE);
expect(mockDashboardFactory.create).toHaveBeenCalled();
});

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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;
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions src/plugins/dashboard/public/dashboard_container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
9 changes: 0 additions & 9 deletions src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
};
Expand Down