Skip to content

Commit

Permalink
Extract common logic to service
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Grubic committed Feb 28, 2023
1 parent 4314b87 commit a74490f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { HttpStart } from '@kbn/core/public';
import { isErrorEmbeddable, openAddPanelFlyout } from '@kbn/embeddable-plugin/public';
import { getSavedObjectFinder } from '@kbn/saved-objects-finder-plugin/public';

import { SavedObjectsManagementPluginStart } from '@kbn/saved-objects-management-plugin/public';
import { pluginServices } from '../../../services/plugin_services';
import { DashboardContainer } from '../dashboard_container';

Expand All @@ -31,7 +30,7 @@ export function addFromLibrary(this: DashboardContainer) {
SavedObjectFinder: getSavedObjectFinder(
uiSettings,
http as HttpStart,
savedObjectsManagement as unknown as SavedObjectsManagementPluginStart
savedObjectsManagement
),
reportUiCounter: usageCollection.reportUiCounter,
getAllFactories: getEmbeddableFactories,
Expand Down
51 changes: 8 additions & 43 deletions test/functional/services/dashboard/add_panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export class DashboardAddPanelService extends FtrService {
private readonly log = this.ctx.getService('log');
private readonly retry = this.ctx.getService('retry');
private readonly testSubjects = this.ctx.getService('testSubjects');
private readonly find = this.ctx.getService('find');
private readonly flyout = this.ctx.getService('flyout');
private readonly common = this.ctx.getPageObject('common');
private readonly header = this.ctx.getPageObject('header');
private readonly savedObjectsFinder = this.ctx.getService('savedObjectsFinder');

async clickOpenAddPanel() {
this.log.debug('DashboardAddPanel.clickOpenAddPanel');
Expand Down Expand Up @@ -79,30 +79,6 @@ export class DashboardAddPanelService extends FtrService {
await this.testSubjects.click(`createNew-${type}`);
}

async toggleFilterPopover() {
this.log.debug('DashboardAddPanel.toggleFilter');
const filtersHolder = await this.find.byClassName('euiSearchBar__filtersHolder');
const filtersButton = await filtersHolder.findByCssSelector('button');
await filtersButton.click();
}

async toggleFilter(type: string) {
this.log.debug(`DashboardAddPanel.addToFilter(${type})`);
await this.waitForListLoading();
await this.toggleFilterPopover();
const list = await this.testSubjects.find('euiSelectableList');
const listItems = await list.findAllByCssSelector('li');
for (let i = 0; i < listItems.length; i++) {
const listItem = await listItems[i].findByClassName('euiSelectableListItem__text');
const text = await listItem.getVisibleText();
if (text.includes(type)) {
await listItem.click();
await this.toggleFilterPopover();
break;
}
}
}

async addEveryEmbeddableOnCurrentPage() {
this.log.debug('addEveryEmbeddableOnCurrentPage');
const itemList = await this.testSubjects.find('savedObjectsFinderTable');
Expand Down Expand Up @@ -171,10 +147,6 @@ export class DashboardAddPanelService extends FtrService {
}
}

async waitForListLoading() {
await this.testSubjects.waitForDeleted('savedObjectFinderLoadingIndicator');
}

async closeAddPanel() {
await this.flyout.ensureClosed('dashboardAddPanel');
}
Expand All @@ -190,9 +162,9 @@ export class DashboardAddPanelService extends FtrService {
this.log.debug('DashboardAddPanel.addEveryVisualization');
await this.ensureAddPanelIsShowing();
if (filter) {
await this.filterEmbeddableNames(filter.replace('-', ' '));
await this.savedObjectsFinder.filterEmbeddableNames(filter.replace('-', ' '));
}
await this.toggleFilter('Visualization');
await this.savedObjectsFinder.toggleFilter('Visualization');
const itemList = await this.testSubjects.find('savedObjectsFinderTable');
await this.retry.try(async () => {
const embeddableListBody = await itemList.findByTagName('tbody');
Expand All @@ -215,9 +187,9 @@ export class DashboardAddPanelService extends FtrService {
await this.ensureAddPanelIsShowing();
const searchList = [];
if (filter) {
await this.filterEmbeddableNames(filter.replace('-', ' '));
await this.savedObjectsFinder.filterEmbeddableNames(filter.replace('-', ' '));
}
await this.toggleFilter('Saved search');
await this.savedObjectsFinder.toggleFilter('Saved search');
let morePages = true;
while (morePages) {
searchList.push(await this.addEveryEmbeddableOnCurrentPage());
Expand Down Expand Up @@ -257,25 +229,18 @@ export class DashboardAddPanelService extends FtrService {
`DashboardAddPanel.addEmbeddable, name: ${embeddableName}, type: ${embeddableType}`
);
await this.ensureAddPanelIsShowing();
await this.toggleFilter(embeddableType);
await this.filterEmbeddableNames(`"${embeddableName.replace('-', ' ')}"`);
await this.savedObjectsFinder.toggleFilter(embeddableType);
await this.savedObjectsFinder.filterEmbeddableNames(`"${embeddableName.replace('-', ' ')}"`);
await this.testSubjects.click(`savedObjectTitle${embeddableName.split(' ').join('-')}`);
await this.testSubjects.exists('addObjectToDashboardSuccess');
await this.closeAddPanel();
return embeddableName;
}

async filterEmbeddableNames(name: string) {
// The search input field may be disabled while the table is loading so wait for it
await this.waitForListLoading();
await this.testSubjects.setValue('savedObjectFinderSearchInput', name);
await this.waitForListLoading();
}

async panelAddLinkExists(name: string) {
this.log.debug(`DashboardAddPanel.panelAddLinkExists(${name})`);
await this.ensureAddPanelIsShowing();
await this.filterEmbeddableNames(`"${name}"`);
await this.savedObjectsFinder.filterEmbeddableNames(`"${name}"`);
return await this.testSubjects.exists(`savedObjectTitle${name.split(' ').join('-')}`);
}
}
28 changes: 2 additions & 26 deletions test/functional/services/dashboard/replace_panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,9 @@ import { FtrService } from '../../ftr_provider_context';

export class DashboardReplacePanelService extends FtrService {
private readonly log = this.ctx.getService('log');
private readonly find = this.ctx.getService('find');
private readonly testSubjects = this.ctx.getService('testSubjects');
private readonly flyout = this.ctx.getService('flyout');

async toggleFilterPopover() {
this.log.debug('DashboardReplacePanel.toggleFilter');
const filtersHolder = await this.find.byClassName('euiSearchBar__filtersHolder');
const filtersButton = await filtersHolder.findByCssSelector('button');
await filtersButton.click();
}

async toggleFilter(type: string) {
this.log.debug(`DashboardReplacePanel.replaceToFilter(${type})`);
await this.waitForListLoading();
await this.toggleFilterPopover();
const list = await this.testSubjects.find('euiSelectableList');
const listItems = await list.findAllByCssSelector('li');
for (let i = 0; i < listItems.length; i++) {
const listItem = await listItems[i].findByClassName('euiSelectableListItem__text');
const text = await listItem.getVisibleText();
if (text.includes(type)) {
await listItem.click();
await this.toggleFilterPopover();
break;
}
}
}
private readonly savedObjectsFinder = this.ctx.getService('savedObjectsFinder');

async isReplacePanelOpen() {
this.log.debug('DashboardReplacePanel.isReplacePanelOpen');
Expand Down Expand Up @@ -80,7 +56,7 @@ export class DashboardReplacePanelService extends FtrService {
await this.ensureReplacePanelIsShowing();
await this.filterEmbeddableNames(`"${embeddableName.replace('-', ' ')}"`);
if (embeddableType) {
await this.toggleFilter(embeddableType);
await this.savedObjectsFinder.toggleFilter(embeddableType);
}
await this.testSubjects.click(`savedObjectTitle${embeddableName.split(' ').join('-')}`);
await this.testSubjects.exists('addObjectToDashboardSuccess');
Expand Down
2 changes: 2 additions & 0 deletions test/functional/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { KibanaSupertestProvider } from './supertest';
import { MenuToggleService } from './menu_toggle';
import { MonacoEditorService } from './monaco_editor';
import { UsageCollectionService } from './usage_collection';
import { SavedObjectsFinderService } from './saved_objects_finder';

export const services = {
...commonServiceProviders,
Expand Down Expand Up @@ -100,4 +101,5 @@ export const services = {
menuToggle: MenuToggleService,
retryOnStale: RetryOnStaleProvider,
usageCollection: UsageCollectionService,
savedObjectsFinder: SavedObjectsFinderService,
};
50 changes: 50 additions & 0 deletions test/functional/services/saved_objects_finder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { FtrService } from '../ftr_provider_context';

export class SavedObjectsFinderService extends FtrService {
private readonly testSubjects = this.ctx.getService('testSubjects');
private readonly find = this.ctx.getService('find');
private readonly log = this.ctx.getService('log');

public async toggleFilterPopover() {
this.log.debug('SavedObjectsFinder.toggleFilter');
const filtersHolder = await this.find.byClassName('euiSearchBar__filtersHolder');
const filtersButton = await filtersHolder.findByCssSelector('button');
await filtersButton.click();
}

public async toggleFilter(type: string) {
this.log.debug(`SavedObjectsFinder.addToFilter(${type})`);
await this.waitForListLoading();
await this.toggleFilterPopover();
const list = await this.testSubjects.find('euiSelectableList');
const listItems = await list.findAllByCssSelector('li');
for (let i = 0; i < listItems.length; i++) {
const listItem = await listItems[i].findByClassName('euiSelectableListItem__text');
const text = await listItem.getVisibleText();
if (text.includes(type)) {
await listItem.click();
await this.toggleFilterPopover();
break;
}
}
}

public async filterEmbeddableNames(name: string) {
// The search input field may be disabled while the table is loading so wait for it
await this.waitForListLoading();
await this.testSubjects.setValue('savedObjectFinderSearchInput', name);
await this.waitForListLoading();
}

private async waitForListLoading() {
await this.testSubjects.waitForDeleted('savedObjectFinderLoadingIndicator');
}
}

0 comments on commit a74490f

Please sign in to comment.