Skip to content

Commit

Permalink
[controls] mock getFactory in control unit tests (#163752)
Browse files Browse the repository at this point in the history
ControlGroupEmbeddable.addOptionsListControl results in
Container.onPanelAdded adding embeddable. If Containter.getFactory does
not return an embeddable, the added embeddable is an `ErrorEmbeddable`.

```
private async onPanelAdded(panel: PanelState) {
    this.updateOutput({
      embeddableLoaded: {
        ...this.output.embeddableLoaded,
        [panel.explicitInput.id]: false,
      },
    } as Partial<TContainerOutput>);
    let embeddable: IEmbeddable | ErrorEmbeddable | undefined;
    const inputForChild = this.getInputForChild(panel.explicitInput.id);
    try {
      const factory = this.getFactory(panel.type);
      if (!factory) {
        throw new EmbeddableFactoryNotFoundError(panel.type);
      }

      // TODO: lets get rid of this distinction with factories, I don't think it will be needed after this change.
      embeddable = isSavedObjectEmbeddableInput(inputForChild)
        ? await factory.createFromSavedObject(inputForChild.savedObjectId, inputForChild, this)
        : await factory.create(inputForChild, this);
    } catch (e) {
      embeddable = new ErrorEmbeddable(e, { id: panel.explicitInput.id }, this);
    }
```


This PR updates all control embeddable tests to ensure returned
embeddable is of expected type that tests are not running against an
`ErrorEmbeddable`.

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nreese and kibanamachine authored Aug 15, 2023
1 parent 95d607b commit ccc8661
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@

import { ErrorEmbeddable } from '@kbn/embeddable-plugin/public';

import { OPTIONS_LIST_CONTROL } from '../../../common';
import { ControlOutput } from '../../types';
import { ControlGroupInput } from '../types';
import { pluginServices } from '../../services';
import { DeleteControlAction } from './delete_control_action';
import { OptionsListEmbeddableInput } from '../../options_list';
import { controlGroupInputBuilder } from '../external_api/control_group_input_builder';
import { ControlGroupContainer } from '../embeddable/control_group_container';
import { OptionsListEmbeddableFactory } from '../../options_list/embeddable/options_list_embeddable_factory';
import { OptionsListEmbeddable } from '../../options_list/embeddable/options_list_embeddable';
import { mockedReduxEmbeddablePackage } from '@kbn/presentation-util-plugin/public/mocks';

let container: ControlGroupContainer;
let embeddable: OptionsListEmbeddable;

beforeAll(async () => {
pluginServices.getServices().controls.getControlFactory = jest
.fn()
.mockImplementation((type: string) => {
if (type === OPTIONS_LIST_CONTROL) return new OptionsListEmbeddableFactory();
});

const controlGroupInput = { chainingSystem: 'NONE', panels: {} } as ControlGroupInput;
controlGroupInputBuilder.addOptionsListControl(controlGroupInput, {
dataViewId: 'test-data-view',
Expand All @@ -34,6 +42,7 @@ beforeAll(async () => {
await container.untilInitialized();

embeddable = container.getChild(container.getChildIds()[0]);
expect(embeddable.type).toBe(OPTIONS_LIST_CONTROL);
});

test('Action is incompatible with Error Embeddables', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { ErrorEmbeddable } from '@kbn/embeddable-plugin/public';

import { OPTIONS_LIST_CONTROL } from '../../../common';
import { ControlOutput } from '../../types';
import { ControlGroupInput } from '../types';
import { pluginServices } from '../../services';
Expand Down Expand Up @@ -55,13 +56,14 @@ test('Action is compatible with embeddables that are editable', async () => {
const editControlAction = new EditControlAction(deleteControlAction);
const emptyContainer = new ControlGroupContainer(mockedReduxEmbeddablePackage, controlGroupInput);
await emptyContainer.untilInitialized();
await emptyContainer.addOptionsListControl({
const control = await emptyContainer.addOptionsListControl({
dataViewId: 'test-data-view',
title: 'test',
fieldName: 'test-field',
width: 'medium',
grow: false,
});
expect(emptyContainer.getInput().panels[control.getInput().id].type).toBe(OPTIONS_LIST_CONTROL);

expect(
await editControlAction.isCompatible({
Expand All @@ -88,18 +90,16 @@ test('Execute should open a flyout', async () => {

const emptyContainer = new ControlGroupContainer(mockedReduxEmbeddablePackage, controlGroupInput);
await emptyContainer.untilInitialized();
await emptyContainer.addOptionsListControl({
const control = (await emptyContainer.addOptionsListControl({
dataViewId: 'test-data-view',
title: 'test',
fieldName: 'test-field',
width: 'medium',
grow: false,
});
const embeddable: OptionsListEmbeddable = emptyContainer.getChild(
emptyContainer.getChildIds()[0]
);
})) as OptionsListEmbeddable;
expect(emptyContainer.getInput().panels[control.getInput().id].type).toBe(OPTIONS_LIST_CONTROL);

const editControlAction = new EditControlAction(deleteControlAction);
await editControlAction.execute({ embeddable });
await editControlAction.execute({ embeddable: control });
expect(spyOn).toHaveBeenCalled();
});

0 comments on commit ccc8661

Please sign in to comment.