Skip to content

Commit

Permalink
Refactors tests, helpers and plugin to take delayed rendering into ac…
Browse files Browse the repository at this point in the history
…count. The delays stem from multiple background tasks that run simultaneously and cannot be turned off
  • Loading branch information
TinaHeiligers committed Oct 18, 2023
1 parent fac7c79 commit 1e08998
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
1 change: 1 addition & 0 deletions test/functional/services/apps_menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class AppsMenuService extends FtrService {
if (!(await this.testSubjects.exists('collapsibleNav'))) {
await this.testSubjects.click('toggleNavButton');
}
await this.testSubjects.exists('collapsibleNav');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { Plugin, CoreSetup } from '@kbn/core/public';
import { Plugin, CoreSetup, DEFAULT_APP_CATEGORIES } from '@kbn/core/public';

export class CoreAppLeavePlugin
implements Plugin<CoreAppLeavePluginSetup, CoreAppLeavePluginStart>
Expand All @@ -15,6 +15,8 @@ export class CoreAppLeavePlugin
core.application.register({
id: 'appleave1',
title: 'AppLeave 1',
appRoute: '/app/appleave1',
category: DEFAULT_APP_CATEGORIES.kibana,
async mount(params) {
const { renderApp } = await import('./application');
params.onAppLeave((actions) => actions.confirm('confirm-message', 'confirm-title'));
Expand All @@ -24,6 +26,8 @@ export class CoreAppLeavePlugin
core.application.register({
id: 'appleave2',
title: 'AppLeave 2',
appRoute: '/app/appleave2',
category: DEFAULT_APP_CATEGORIES.kibana,
async mount(params) {
const { renderApp } = await import('./application');
params.onAppLeave((actions) => actions.confirm('confirm-message', 'confirm-title'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Side Public License, v 1.
*/

import url from 'url';
import expect from '@kbn/expect';
import url from 'url';
import { PluginFunctionalProviderContext } from '../../services';

const getKibanaUrl = (pathname?: string, search?: string) =>
Expand All @@ -25,45 +25,81 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide
const appsMenu = getService('appsMenu');
const log = getService('log');
const retry = getService('retry');
const testSubjects = getService('testSubjects');
const config = getService('config');

const waitForUrlToBe = (pathname?: string, search?: string) => {
const waitForUrlToBe = async (pathname?: string, search?: string) => {
const expectedUrl = getKibanaUrl(pathname, search);
return retry.waitFor(`Url to be ${expectedUrl}`, async () => {
return await retry.waitFor(`Url to be ${expectedUrl}`, async () => {
const currentUrl = await browser.getCurrentUrl();
log.debug(`waiting for currentUrl ${currentUrl} to be expectedUrl ${expectedUrl}`);
return currentUrl === expectedUrl;
});
};

const ensureModalOpen = async (
defaultTryTimeout: number,
attempts: number,
timeMultiplier: number,
action: 'cancel' | 'confirm'
): Promise<void> => {
let isConfirmCancelModalOpenState = false;
await retry.tryForTime(defaultTryTimeout * timeMultiplier, async () => {
isConfirmCancelModalOpenState = await testSubjects.exists('confirmModalCancelButton');
});
if (isConfirmCancelModalOpenState) {
log.debug(`defaultTryTimeout * ${timeMultiplier} is long enough`);
return action === 'cancel'
? await PageObjects.common.clickCancelOnModal(true, false)
: await PageObjects.common.clickConfirmOnModal();
} else {
log.debug(`defaultTryTimeout * ${timeMultiplier} is not long enough`);
return await ensureModalOpen(
defaultTryTimeout,
(attempts = attempts > 0 ? attempts - 1 : 0),
(timeMultiplier = timeMultiplier < 10 ? timeMultiplier + 1 : 10),
action
);
}
};

describe('application using leave confirmation', () => {
const defaultTryTimeout = config.get('timeouts.try');
const attempts = 5;
describe('when navigating to another app', () => {
it('allows navigation if user click confirm on the confirmation dialog', async () => {
const timeMultiplier = 2;
it('prevents navigation if user click cancel on the confirmation dialog', async () => {
await PageObjects.common.navigateToApp('appleave1');
await waitForUrlToBe('/app/appleave1');

await appsMenu.clickLink('AppLeave 2');

await PageObjects.common.expectConfirmModalOpenState(true);
await PageObjects.common.clickConfirmOnModal();

await ensureModalOpen(defaultTryTimeout, attempts, timeMultiplier, 'cancel');
await PageObjects.header.waitUntilLoadingHasFinished();
await retry.waitFor('navigate to appleave1', async () => {
const currentUrl = await browser.getCurrentUrl();
log.debug(`currentUrl ${currentUrl}`);
return currentUrl.includes('appleave1');
});
const currentUrl = await browser.getCurrentUrl();
const kibanaUrl = getKibanaUrl('/app/appleave2');
log.debug(`currentUrl ${currentUrl} kibanaUrl ${kibanaUrl}`);
expect(currentUrl).to.eql(kibanaUrl);
expect(currentUrl).to.contain('appleave1');
await PageObjects.common.navigateToApp('home');
});
it('prevents navigation if user click cancel on the confirmation dialog', async () => {

it('allows navigation if user click confirm on the confirmation dialog', async () => {
await PageObjects.common.navigateToApp('appleave1');
await waitForUrlToBe('/app/appleave1');

await appsMenu.clickLink('AppLeave 2');

await PageObjects.common.expectConfirmModalOpenState(true);
await PageObjects.common.clickCancelOnModal(false);

await ensureModalOpen(defaultTryTimeout, attempts, timeMultiplier, 'confirm');
await PageObjects.header.waitUntilLoadingHasFinished();
await retry.waitFor('navigate to appleave1', async () => {
const currentUrl = await browser.getCurrentUrl();
log.debug(`currentUrl ${currentUrl}`);
return currentUrl.includes('appleave2');
});
const currentUrl = await browser.getCurrentUrl();
const kibanaUrl = getKibanaUrl('/app/appleave1');
log.debug(`currentUrl ${currentUrl} kibanaUrl ${kibanaUrl}`);
expect(currentUrl).to.eql(kibanaUrl);
expect(currentUrl).to.contain('appleave2');
await PageObjects.common.navigateToApp('home');
});
});
});
Expand Down

0 comments on commit 1e08998

Please sign in to comment.