Skip to content

Commit

Permalink
test: 💍 fix Jest tests after refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Apr 8, 2020
1 parent 5fea25d commit 45821c7
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 83 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/advanced_ui_actions/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export {
DynamicActionManager as UiActionsEnhancedDynamicActionManager,
DynamicActionManagerParams as UiActionsEnhancedDynamicActionManagerParams,
DynamicActionManagerState as UiActionsEnhancedDynamicActionManagerState,
MemoryActionStorage as UiActionsEnhancedMemoryActionStorage,
} from './dynamic_actions';
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { drilldownsPluginMock } from '../../../../../../drilldowns/public/mocks'
import { ViewMode } from '../../../../../../../../src/plugins/embeddable/public';
import { uiActionsPluginMock } from '../../../../../../../../src/plugins/ui_actions/public/mocks';
import { TriggerContextMapping } from '../../../../../../../../src/plugins/ui_actions/public';
import { MockEmbeddable } from '../test_helpers';
import { MockEmbeddable, enhanceEmbeddable } from '../test_helpers';

const overlays = coreMock.createStart().overlays;
const drilldowns = drilldownsPluginMock.createStartContract();
Expand All @@ -40,85 +40,91 @@ test('icon exists', () => {
);
});

interface CompatibilityParams {
isEdit?: boolean;
isValueClickTriggerSupported?: boolean;
isEmbeddableEnhanced?: boolean;
}

describe('isCompatible', () => {
const drilldownAction = new FlyoutCreateDrilldownAction(actionParams);

function checkCompatibility(params: {
isEdit: boolean;
withUiActions: boolean;
isValueClickTriggerSupported: boolean;
}): Promise<boolean> {
return drilldownAction.isCompatible({
embeddable: new MockEmbeddable(
{ id: '', viewMode: params.isEdit ? ViewMode.EDIT : ViewMode.VIEW },
{
supportedTriggers: (params.isValueClickTriggerSupported
? ['VALUE_CLICK_TRIGGER']
: []) as Array<keyof TriggerContextMapping>,
uiActions: params.withUiActions ? uiActions : undefined, // dynamic actions support
}
),
async function assertCompatibility(
{
isEdit = true,
isValueClickTriggerSupported = true,
isEmbeddableEnhanced = true,
}: CompatibilityParams,
expectedResult: boolean = true
): Promise<void> {
let embeddable = new MockEmbeddable(
{ id: '', viewMode: isEdit ? ViewMode.EDIT : ViewMode.VIEW },
{
supportedTriggers: (isValueClickTriggerSupported ? ['VALUE_CLICK_TRIGGER'] : []) as Array<
keyof TriggerContextMapping
>,
uiActions,
}
);

if (isEmbeddableEnhanced) {
embeddable = enhanceEmbeddable(embeddable);
}

const result = await drilldownAction.isCompatible({
embeddable,
});

expect(result).toBe(expectedResult);
}

const assertNonCompatibility = (params: CompatibilityParams) =>
assertCompatibility(params, false);

test("compatible if dynamicUiActions enabled, 'VALUE_CLICK_TRIGGER' is supported, in edit mode", async () => {
expect(
await checkCompatibility({
withUiActions: true,
isEdit: true,
isValueClickTriggerSupported: true,
})
).toBe(true);
await assertCompatibility({});
});

test('not compatible if dynamicUiActions disabled', async () => {
expect(
await checkCompatibility({
withUiActions: false,
isEdit: true,
isValueClickTriggerSupported: true,
})
).toBe(false);
test('not compatible if embeddable is not enhanced', async () => {
await assertNonCompatibility({
isEmbeddableEnhanced: false,
});
});

test("not compatible if 'VALUE_CLICK_TRIGGER' is not supported", async () => {
expect(
await checkCompatibility({
withUiActions: true,
isEdit: true,
isValueClickTriggerSupported: false,
})
).toBe(false);
await assertNonCompatibility({
isValueClickTriggerSupported: false,
});
});

test('not compatible if in view mode', async () => {
expect(
await checkCompatibility({
withUiActions: true,
isEdit: false,
isValueClickTriggerSupported: true,
})
).toBe(false);
await assertNonCompatibility({
isEdit: false,
});
});
});

describe('execute', () => {
const drilldownAction = new FlyoutCreateDrilldownAction(actionParams);

test('throws error if no dynamicUiActions', async () => {
await expect(
drilldownAction.execute({
embeddable: new MockEmbeddable({ id: '' }, {}),
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Can't execute FlyoutCreateDrilldownAction without dynamicActionsManager"`
`"Need embeddable to be EnhancedEmbeddable to execute FlyoutCreateDrilldownAction."`
);
});

test('should open flyout', async () => {
const spy = jest.spyOn(overlays, 'openFlyout');
const embeddable = enhanceEmbeddable(new MockEmbeddable({ id: '' }, { uiActions }));

await drilldownAction.execute({
embeddable: new MockEmbeddable({ id: '' }, { uiActions }),
embeddable,
});

expect(spy).toBeCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@ import { coreMock } from '../../../../../../../../src/core/public/mocks';
import { drilldownsPluginMock } from '../../../../../../drilldowns/public/mocks';
import { ViewMode } from '../../../../../../../../src/plugins/embeddable/public';
import { uiActionsPluginMock } from '../../../../../../../../src/plugins/ui_actions/public/mocks';
import { MockEmbeddable } from '../test_helpers';
import { EnhancedEmbeddable } from '../../../../../../embeddable_enhanced/public';
import { MockEmbeddable, enhanceEmbeddable } from '../test_helpers';

const overlays = coreMock.createStart().overlays;
const drilldowns = drilldownsPluginMock.createStartContract();
const uiActions = uiActionsPluginMock.createStartContract();
const uiActionsPlugin = uiActionsPluginMock.createPlugin();
const uiActions = uiActionsPlugin.doStart();

uiActionsPlugin.setup.registerActionFactory({
id: 'foo',
CollectConfig: {} as any,
createConfig: () => ({}),
isConfigValid: () => true,
create: () => ({
id: 'test',
execute: async () => {},
}),
});

const actionParams: FlyoutEditDrilldownParams = {
drilldowns: () => Promise.resolve(drilldowns),
Expand All @@ -39,63 +52,93 @@ test('MenuItem exists', () => {
});

describe('isCompatible', () => {
const drilldownAction = new FlyoutEditDrilldownAction(actionParams);
function setupIsCompatible({
isEdit = true,
isEmbeddableEnhanced = true,
}: {
isEdit?: boolean;
isEmbeddableEnhanced?: boolean;
} = {}) {
const action = new FlyoutEditDrilldownAction(actionParams);
const input = {
id: '',
viewMode: isEdit ? ViewMode.EDIT : ViewMode.VIEW,
};
const embeddable = new MockEmbeddable(input, {
uiActions,
});
const context = {
embeddable: (isEmbeddableEnhanced
? enhanceEmbeddable(embeddable, uiActions)
: embeddable) as EnhancedEmbeddable<MockEmbeddable>,
};

return {
action,
context,
};
}

test('not compatible if no drilldowns', async () => {
const { action, context } = setupIsCompatible();
expect(await action.isCompatible(context)).toBe(false);
});

test('not compatible if embeddable is not enhanced', async () => {
const { action, context } = setupIsCompatible({ isEmbeddableEnhanced: false });
expect(await action.isCompatible(context)).toBe(false);
});

describe('when has at least one drilldown', () => {
test('is compatible in edit mode', async () => {
const { action, context } = setupIsCompatible();

function checkCompatibility(params: {
isEdit: boolean;
withUiActions: boolean;
}): Promise<boolean> {
return drilldownAction.isCompatible({
embeddable: new MockEmbeddable(
await context.embeddable.enhancements.dynamicActions.createEvent(
{
id: '',
viewMode: params.isEdit ? ViewMode.EDIT : ViewMode.VIEW,
config: {},
factoryId: 'foo',
name: '',
},
{
uiActions: params.withUiActions ? uiActions : undefined, // dynamic actions support
}
),
['VALUE_CLICK_TRIGGER']
);

expect(await action.isCompatible(context)).toBe(true);
});
}

// TODO: need proper DynamicActionsMock and ActionFactory mock
test.todo('compatible if dynamicUiActions enabled, in edit view, and have at least 1 drilldown');
test('not compatible in view mode', async () => {
const { action, context } = setupIsCompatible({ isEdit: false });

test('not compatible if dynamicUiActions disabled', async () => {
expect(
await checkCompatibility({
withUiActions: false,
isEdit: true,
})
).toBe(false);
});
await context.embeddable.enhancements.dynamicActions.createEvent(
{
config: {},
factoryId: 'foo',
name: '',
},
['VALUE_CLICK_TRIGGER']
);

test('not compatible if no drilldowns', async () => {
expect(
await checkCompatibility({
withUiActions: true,
isEdit: true,
})
).toBe(false);
expect(await action.isCompatible(context)).toBe(false);
});
});
});

describe('execute', () => {
const drilldownAction = new FlyoutEditDrilldownAction(actionParams);

test('throws error if no dynamicUiActions', async () => {
await expect(
drilldownAction.execute({
embeddable: new MockEmbeddable({ id: '' }, {}),
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Can't execute FlyoutEditDrilldownAction without dynamicActionsManager"`
`"Need embeddable to be EnhancedEmbeddable to execute FlyoutEditDrilldownAction."`
);
});

test('should open flyout', async () => {
const spy = jest.spyOn(overlays, 'openFlyout');
await drilldownAction.execute({
embeddable: new MockEmbeddable({ id: '' }, { uiActions }),
embeddable: enhanceEmbeddable(new MockEmbeddable({ id: '' }, { uiActions })),
});
expect(spy).toBeCalled();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Embeddable, EmbeddableInput } from '../../../../../../../src/plugins/embeddable/public/';
import { Embeddable, EmbeddableInput } from '../../../../../../../src/plugins/embeddable/public';
import { EnhancedEmbeddable } from '../../../../../embeddable_enhanced/public';
import {
UiActionsEnhancedMemoryActionStorage as MemoryActionStorage,
UiActionsEnhancedDynamicActionManager as DynamicActionManager,
} from '../../../../../advanced_ui_actions/public';
import {
TriggerContextMapping,
UiActionsStart,
} from '../../../../../../../src/plugins/ui_actions/public';
import { uiActionsPluginMock } from '../../../../../../../src/plugins/ui_actions/public/mocks';

export class MockEmbeddable extends Embeddable {
public readonly type = 'mock';
Expand All @@ -26,3 +32,17 @@ export class MockEmbeddable extends Embeddable {
return this.triggers;
}
}

export const enhanceEmbeddable = <E extends MockEmbeddable>(
embeddable: E,
uiActions: UiActionsStart = uiActionsPluginMock.createStartContract()
): EnhancedEmbeddable<E> => {
(embeddable as EnhancedEmbeddable<E>).enhancements = {
dynamicActions: new DynamicActionManager({
storage: new MemoryActionStorage(),
isCompatible: async () => true,
uiActions,
}),
};
return embeddable as EnhancedEmbeddable<E>;
};

0 comments on commit 45821c7

Please sign in to comment.