Skip to content

Commit

Permalink
fixed prettier, added more tests. Hid unlink action behind setting from
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomThomson committed Jul 31, 2020
1 parent 77a7e94 commit 171268b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ beforeEach(async () => {
}
});

test('Unlink is not compatible when embeddable does not have a savedObjectId', async () => {
const action = new UnlinkFromLibraryAction(coreStart);
embeddable.updateInput({ savedObjectId: undefined });
expect(await action.isCompatible({ embeddable })).toBe(false);
});

test('Unlink is not compatible when embeddable is not in a dashboard container', async () => {
const orphanContactCard = await container.addNewEmbeddable<
ContactCardEmbeddableInput,
ContactCardEmbeddableOutput,
ContactCardEmbeddable
>(CONTACT_CARD_EMBEDDABLE, {
firstName: 'Orphan',
});
orphanContactCard.updateInput({ savedObjectId: 'coolestSavedObjectId' });
const action = new UnlinkFromLibraryAction(coreStart);
expect(await action.isCompatible({ embeddable: orphanContactCard })).toBe(false);
});

test('Unlink is compatible when embeddable on dashboard has a savedObjectId', async () => {
const action = new UnlinkFromLibraryAction(coreStart);
embeddable.updateInput({ savedObjectId: undefined });
expect(await action.isCompatible({ embeddable })).toBe(false);
});

test('Unlink replaces embeddableId but retains panel count', async () => {
const dashboard = embeddable.getRoot() as IContainer;
const originalPanelCount = Object.keys(dashboard.getInput().panels).length;
Expand All @@ -99,7 +124,7 @@ test('Unlink replaces embeddableId but retains panel count', async () => {
expect(Object.keys(container.getInput().panels).length).toEqual(originalPanelCount);

const newPanelId = Object.keys(container.getInput().panels).find(
key => !originalPanelKeySet.has(key)
(key) => !originalPanelKeySet.has(key)
);
expect(newPanelId).toBeDefined();
const newPanel = container.getInput().panels[newPanelId!];
Expand All @@ -122,7 +147,7 @@ test('Unlink unwraps all attributes from savedObject', async () => {
const action = new UnlinkFromLibraryAction(coreStart);
await action.execute({ embeddable });
const newPanelId = Object.keys(container.getInput().panels).find(
key => !originalPanelKeySet.has(key)
(key) => !originalPanelKeySet.has(key)
);
expect(newPanelId).toBeDefined();
const newPanel = container.getInput().panels[newPanelId!];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ export class UnlinkFromLibraryAction implements ActionByType<typeof ACTION_UNLIN
};
dashboard.replacePanel(panelToReplace, newPanel);
}
}
}
15 changes: 10 additions & 5 deletions src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ export class DashboardPlugin
private stopUrlTracking: (() => void) | undefined = undefined;
private getActiveUrl: (() => string) | undefined = undefined;
private currentHistory: ScopedHistory | undefined = undefined;
private dashboardFeatureFlagConfig?: DashboardFeatureFlagConfig;

private dashboardUrlGenerator?: DashboardUrlGenerator;

public setup(
core: CoreSetup<StartDependencies, DashboardStart>,
{ share, uiActions, embeddable, home, kibanaLegacy, data, usageCollection }: SetupDependencies
): Setup {
this.dashboardFeatureFlagConfig = this.initializerContext.config.get<
DashboardFeatureFlagConfig
>();
const expandPanelAction = new ExpandPanelAction();
uiActions.registerAction(expandPanelAction);
uiActions.attachAction(CONTEXT_MENU_TRIGGER, expandPanelAction.id);
Expand Down Expand Up @@ -405,10 +409,11 @@ export class DashboardPlugin
uiActions.registerAction(clonePanelAction);
uiActions.attachAction(CONTEXT_MENU_TRIGGER, clonePanelAction.id);

// TODO: once https://github.com/elastic/kibana/pull/73870 is merges, make this unlink from library action dependent on that config value.
const unlinkFromLibraryAction = new UnlinkFromLibraryAction(core);
uiActions.registerAction(unlinkFromLibraryAction);
uiActions.attachAction(CONTEXT_MENU_TRIGGER, unlinkFromLibraryAction.id);
if (this.dashboardFeatureFlagConfig?.allowByValueEmbeddables) {
const unlinkFromLibraryAction = new UnlinkFromLibraryAction(core);
uiActions.registerAction(unlinkFromLibraryAction);
uiActions.attachAction(CONTEXT_MENU_TRIGGER, unlinkFromLibraryAction.id);
}

const savedDashboardLoader = createSavedDashboardLoader({
savedObjectsClient: core.savedObjects.client,
Expand All @@ -425,7 +430,7 @@ export class DashboardPlugin
getSavedDashboardLoader: () => savedDashboardLoader,
addEmbeddableToDashboard: this.addEmbeddableToDashboard.bind(this, core),
dashboardUrlGenerator: this.dashboardUrlGenerator,
dashboardFeatureFlagConfig: this.initializerContext.config.get<DashboardFeatureFlagConfig>(),
dashboardFeatureFlagConfig: this.dashboardFeatureFlagConfig!,
DashboardContainerByValueRenderer: createDashboardContainerByValueRenderer({
factory: dashboardContainerFactory,
}),
Expand Down

0 comments on commit 171268b

Please sign in to comment.