Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stacey-gammon committed Apr 25, 2019
1 parent aec421c commit 1484788
Show file tree
Hide file tree
Showing 75 changed files with 6,081 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { QueryLanguageType, ViewMode } from 'plugins/embeddable_api/index';
import { DashboardContainerInput, DashboardPanelState } from '../embeddable';

export function getSampleDashboardInput(
overrides?: Partial<DashboardContainerInput>
): DashboardContainerInput {
return {
id: '123',
filters: [],
useMargins: false,
isFullScreenMode: false,
title: 'My Dashboard',
customization: {},
query: {
language: QueryLanguageType.KUERY,
query: 'hi',
},
timeRange: {
to: 'now',
from: 'now-15m',
},
viewMode: ViewMode.VIEW,
panels: {},
...overrides,
};
}

export function getSampleDashboardPanel(
overrides: Partial<DashboardPanelState> & { embeddableId: string; type: string }
): DashboardPanelState {
return {
gridData: {
h: 15,
w: 15,
x: 0,
y: 0,
i: overrides.embeddableId,
},
embeddableId: overrides.embeddableId,
type: overrides.type,
explicitInput: overrides.explicitInput || {},
...overrides,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { getSampleDashboardInput, getSampleDashboardPanel } from './get_sample_dashboard_input';
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

jest.mock('ui/metadata', () => ({
metadata: {
branch: 'my-metadata-branch',
version: 'my-metadata-version',
},
}));

jest.mock('ui/capabilities', () => ({
uiCapabilities: {
visualize: {
save: true,
},
},
}));

import { EmbeddableFactoryRegistry, isErrorEmbeddable } from 'plugins/embeddable_api/index';
import { ExpandPanelAction } from './expand_panel_action';
import {
HelloWorldEmbeddable,
HELLO_WORLD_EMBEDDABLE,
HelloWorldInput,
HelloWorldEmbeddableFactory,
} from 'plugins/embeddable_api/__test__/index';
import { DashboardContainer } from '../embeddable';
import { getSampleDashboardInput, getSampleDashboardPanel } from '../__test__';

const embeddableFactories = new EmbeddableFactoryRegistry();
embeddableFactories.registerFactory(new HelloWorldEmbeddableFactory());

let container: DashboardContainer;
let embeddable: HelloWorldEmbeddable;

beforeEach(async () => {
container = new DashboardContainer(
getSampleDashboardInput({
panels: {
'123': getSampleDashboardPanel({
embeddableId: '123',
explicitInput: { firstName: 'Sam' },
type: HELLO_WORLD_EMBEDDABLE,
}),
},
}),
embeddableFactories
);

const helloEmbeddable = await container.addNewEmbeddable<HelloWorldInput, HelloWorldEmbeddable>(
HELLO_WORLD_EMBEDDABLE,
{
firstName: 'Kibana',
}
);

if (isErrorEmbeddable(helloEmbeddable)) {
throw new Error('Failed to create embeddable');
} else {
embeddable = helloEmbeddable;
}
});

test('Sets the embeddable expanded panel id on the parent', async () => {
const expandPanelAction = new ExpandPanelAction();

expect(container.getInput().expandedPanelId).toBeUndefined();

expandPanelAction.execute({ embeddable });

expect(container.getInput().expandedPanelId).toBe(embeddable.id);
});

test('Is not compatible when embeddable is not in a dashboard container', async () => {
const action = new ExpandPanelAction();
expect(
await action.isCompatible({
embeddable: new HelloWorldEmbeddable({ firstName: 'sue', id: '123' }),
})
).toBe(false);
});

test('Execute throws an error when called with an embeddable not in a parent', async () => {
const action = new ExpandPanelAction();
async function check() {
await action.execute({ embeddable: container });
}
await expect(check()).rejects.toThrow(Error);
});

test('Returns title', async () => {
const action = new ExpandPanelAction();
expect(action.getTitle({ embeddable })).toBeDefined();
});

test('Returns an icon', async () => {
const action = new ExpandPanelAction();
expect(action.getIcon({ embeddable })).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
jest.mock('ui/metadata', () => ({
metadata: {
branch: 'my-metadata-branch',
version: 'my-metadata-version',
},
}));

import {
HelloWorldEmbeddable,
HelloWorldInput,
HELLO_WORLD_EMBEDDABLE,
HelloWorldEmbeddableFactory,
} from 'plugins/embeddable_api/__test__/embeddables';
import {
EmbeddableFactoryRegistry,
isErrorEmbeddable,
ViewMode,
actionRegistry,
triggerRegistry,
} from 'plugins/embeddable_api/index';
import { DashboardContainer } from './dashboard_container';
import { getSampleDashboardInput, getSampleDashboardPanel } from '../__test__';
import { EditModeAction } from 'plugins/embeddable_api/__test__/actions';
import { mount } from 'enzyme';
import { waitFor, nextTick } from 'test_utils/enzyme_helpers';

// @ts-ignore
import { findTestSubject } from '@elastic/eui/lib/test';
import { EmbeddablePanel } from 'plugins/embeddable_api/panel';
import { I18nProvider } from '@kbn/i18n/react';
import { CONTEXT_MENU_TRIGGER } from 'plugins/embeddable_api/triggers/trigger_registry';

test('DashboardContainer initializes embeddables', async done => {
const embeddableFactories = new EmbeddableFactoryRegistry();
embeddableFactories.registerFactory(new HelloWorldEmbeddableFactory());
const container = new DashboardContainer(
getSampleDashboardInput({
panels: {
'123': getSampleDashboardPanel({
embeddableId: '123',
explicitInput: { firstName: 'Sam' },
type: HELLO_WORLD_EMBEDDABLE,
}),
},
}),
embeddableFactories
);

const subscription = container.getOutput$().subscribe(output => {
if (container.getOutput().embeddableLoaded['123']) {
const embeddable = container.getChild<HelloWorldEmbeddable>('123');
expect(embeddable).toBeDefined();
expect(embeddable.id).toBe('123');
done();
}
});

if (container.getOutput().embeddableLoaded['123']) {
const embeddable = container.getChild<HelloWorldEmbeddable>('123');
expect(embeddable).toBeDefined();
expect(embeddable.id).toBe('123');
subscription.unsubscribe();
done();
}
});

test('DashboardContainer.addNewEmbeddable', async () => {
const embeddableFactories = new EmbeddableFactoryRegistry();
embeddableFactories.registerFactory(new HelloWorldEmbeddableFactory());
const container = new DashboardContainer(getSampleDashboardInput(), embeddableFactories);
const embeddable = await container.addNewEmbeddable<HelloWorldInput>(HELLO_WORLD_EMBEDDABLE, {
firstName: 'Kibana',
});
expect(embeddable).toBeDefined();

if (!isErrorEmbeddable(embeddable)) {
expect(embeddable.getInput().firstName).toBe('Kibana');
} else {
expect(false).toBe(true);
}

const embeddableInContainer = container.getChild<HelloWorldEmbeddable>(embeddable.id);
expect(embeddableInContainer).toBeDefined();
expect(embeddableInContainer.id).toBe(embeddable.id);
});

test('Container view mode change propagates to children', async () => {
const embeddableFactories = new EmbeddableFactoryRegistry();
embeddableFactories.registerFactory(new HelloWorldEmbeddableFactory());
const container = new DashboardContainer(getSampleDashboardInput(), embeddableFactories);
const embeddable = await container.addNewEmbeddable<HelloWorldInput, HelloWorldEmbeddable>(
HELLO_WORLD_EMBEDDABLE,
{
firstName: 'Bob',
}
);

expect(embeddable.getInput().viewMode).toBe(ViewMode.VIEW);

container.updateInput({ viewMode: ViewMode.EDIT });

expect(embeddable.getInput().viewMode).toBe(ViewMode.EDIT);
});

test('Container in edit mode shows edit mode actions', async () => {
const editModeAction = new EditModeAction();
actionRegistry.addAction(editModeAction);
triggerRegistry.attachAction({
triggerId: CONTEXT_MENU_TRIGGER,
actionId: editModeAction.id,
});

const embeddableFactories = new EmbeddableFactoryRegistry();
embeddableFactories.registerFactory(new HelloWorldEmbeddableFactory());
const container = new DashboardContainer(
getSampleDashboardInput({ viewMode: ViewMode.VIEW }),
embeddableFactories
);

const embeddable = await container.addNewEmbeddable<HelloWorldInput, HelloWorldEmbeddable>(
HELLO_WORLD_EMBEDDABLE,
{
firstName: 'Bob',
}
);

const component = mount(
<I18nProvider>
<EmbeddablePanel embeddable={embeddable} container={container} />
</I18nProvider>
);

const button = findTestSubject(component, 'embeddablePanelToggleMenuIcon');

expect(button.length).toBe(1);
findTestSubject(component, 'embeddablePanelToggleMenuIcon').simulate('click');

expect(findTestSubject(component, `embeddablePanelContextMenuOpen`).length).toBe(1);

const editAction = findTestSubject(component, `embeddablePanelAction-${editModeAction.id}`);

expect(editAction.length).toBe(0);

container.updateInput({ viewMode: ViewMode.EDIT });
await nextTick();
component.update();
expect(findTestSubject(component, 'embeddablePanelContextMenuOpen').length).toBe(0);
findTestSubject(component, 'embeddablePanelToggleMenuIcon').simulate('click');
await nextTick();
component.update();
expect(findTestSubject(component, 'embeddablePanelContextMenuOpen').length).toBe(1);

await nextTick();
component.update();

const action = findTestSubject(component, `embeddablePanelAction-${editModeAction.id}`);
expect(action.length).toBe(1);
});
Loading

0 comments on commit 1484788

Please sign in to comment.