Skip to content

Commit

Permalink
FI-1133 fix: remove hook navigateTo
Browse files Browse the repository at this point in the history
fix: add methods `assertPage`, `navigateToPage` and `reloadPage` to base `Page`
feat: add util `reloadDocument`
  • Loading branch information
uid11 committed Jan 28, 2024
1 parent 192ce0c commit 3e64fcb
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 62 deletions.
1 change: 0 additions & 1 deletion autotests/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export {getLogContext} from './getLogContext';
export {getMainTestRunParams} from './getMainTestRunParams';
export {getTestRunHash} from './getTestRunHash';
export {isTestSkipped} from './isTestSkipped';
export {navigateTo} from './navigateTo';
35 changes: 0 additions & 35 deletions autotests/hooks/navigateTo.ts

This file was deleted.

14 changes: 13 additions & 1 deletion autotests/pageObjects/pages/E2edReportExample.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {
setPageCookiesAndNavigateToUrl,
setPageRequestHeadersAndNavigateToUrl,
} from 'autotests/actions';
import {setPageCookies, setPageRequestHeaders} from 'autotests/context';
import {E2edReportExample as E2edReportExampleRoute} from 'autotests/routes/pageRoutes';
import {locatorIdSelector} from 'autotests/selectors';
import {Page} from 'e2ed';
import {setReadonlyProperty} from 'e2ed/utils';

import type {Cookie, Headers, Selector} from 'e2ed/types';
import type {Cookie, Headers, Selector, Url} from 'e2ed/types';

type CustomPageParams = {pageCookies?: readonly Cookie[]; pageRequestHeaders?: Headers} | undefined;

Expand Down Expand Up @@ -69,4 +73,12 @@ export class E2edReportExample extends Page<CustomPageParams> {
setPageRequestHeaders(this.pageRequestHeaders);
}
}

override navigateToPage(url: Url): Promise<void> {
if (this.pageRequestHeaders) {
return setPageRequestHeadersAndNavigateToUrl(url, this.pageRequestHeaders);
}

return setPageCookiesAndNavigateToUrl(url, this.pageCookies);
}
}
1 change: 0 additions & 1 deletion autotests/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ export type {
GetMainTestRunParams,
GetTestRunHash,
IsTestSkipped,
NavigateTo,
} from './packSpecific';
export type {TestMeta} from './testMeta';
1 change: 0 additions & 1 deletion autotests/types/packSpecific.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ export type MapBackendResponseToLog = PackSpecificTypes['MapBackendResponseToLog
export type MapLogPayloadInConsole = PackSpecificTypes['MapLogPayloadInConsole'];
export type MapLogPayloadInLogFile = PackSpecificTypes['MapLogPayloadInLogFile'];
export type MapLogPayloadInReport = PackSpecificTypes['MapLogPayloadInReport'];
export type NavigateTo = PackSpecificTypes['NavigateTo'];
export type {Pack};
42 changes: 41 additions & 1 deletion src/Page.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// eslint-disable-next-line import/no-internal-modules
import {navigateToUrl} from './actions/navigateToUrl';
// eslint-disable-next-line import/no-internal-modules
import {waitForAllRequestsComplete, waitForInterfaceStabilization} from './actions/waitFor';
import {CREATE_PAGE_TOKEN} from './constants/internal';
import {assertValueIsTrue} from './utils/asserts';
import {getFullPackConfig} from './utils/config';
import {reloadDocument} from './utils/document';

import type {PageRoute} from './PageRoute';
import type {AsyncVoid, PageClassTypeArgs, ParamsKeyType} from './types/internal';
import type {AsyncVoid, PageClassTypeArgs, ParamsKeyType, Url} from './types/internal';

/**
* Inner key for parameters type.
Expand Down Expand Up @@ -79,6 +82,21 @@ export abstract class Page<PageParams = undefined> {
*/
afterNavigateToPage?(): AsyncVoid;

/**
* Optional hook that runs after reload to the page.
*/
afterReloadPage?(): AsyncVoid;

/**
* Asserts that we are on the expected page by `isMatch` flage.
* `isMatch` equals `true`, if url matches the page with given parameters, and `false` otherwise.
*/
assertPage(isMatch: boolean): AsyncVoid {
assertValueIsTrue(isMatch, `the document url matches the page "${this.constructor.name}"`, {
page: this,
});
}

/**
* Optional hook that runs before asserts the page.
*/
Expand All @@ -89,11 +107,33 @@ export abstract class Page<PageParams = undefined> {
*/
beforeNavigateToPage?(): AsyncVoid;

/**
* Optional hook that runs before reload to the page.
*/
beforeReloadPage?(): AsyncVoid;

/**
* Get page route (for navigation to the page).
*/
abstract getRoute(): PageRoute<unknown>;

/**
* Navigates to the page by url.
*/
navigateToPage(url: Url): Promise<void> {
return navigateToUrl(url, {skipLogs: true});
}

/**
* Reloads the page.
*/
reloadPage(): Promise<void> {
return reloadDocument();
}

/**
* Waits for page loaded.
*/
async waitForPageLoaded(): Promise<void> {
await waitForAllRequestsComplete(() => true, {
maxIntervalBetweenRequestsInMs: this.maxIntervalBetweenRequestsInMs,
Expand Down
18 changes: 11 additions & 7 deletions src/actions/navigateToUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ import {createClientFunction} from '../createClientFunction';
import {getFullPackConfig} from '../utils/config';
import {log} from '../utils/log';

import type {Url, Void} from '../types/internal';
import type {ClientFunction, Url, Void} from '../types/internal';

const clientNavigateToUrl = createClientFunction<[url: Url], Void>(
(url) => {
window.location.href = url;
},
{name: 'navigateToUrl'},
);
let clientNavigateToUrl: ClientFunction<[url: Url], Void> | undefined;

type Options = Readonly<{
skipLogs?: boolean;
Expand All @@ -21,6 +16,15 @@ type Options = Readonly<{
* Navigate to the `url` (without waiting of interface stabilization).
*/
export const navigateToUrl = async (url: Url, options: Options = {}): Promise<void> => {
if (clientNavigateToUrl === undefined) {
clientNavigateToUrl = createClientFunction<[url: Url], Void>(
(clientUrl) => {
window.location.href = clientUrl;
},
{name: 'navigateToUrl'},
);
}

const {skipLogs = false} = options;

if (skipLogs !== true) {
Expand Down
5 changes: 2 additions & 3 deletions src/actions/pages/assertPage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {LogEventStatus, LogEventType} from '../../constants/internal';
import {assertValueIsTrue} from '../../utils/asserts';
import {getDocumentUrl} from '../../utils/document';
import {log} from '../../utils/log';

Expand Down Expand Up @@ -27,13 +26,13 @@ export const assertPage = async <SomePageClass extends AnyPageClassType>(
const isMatch = route.isMatchUrl(documentUrl);

const logEventStatus = isMatch ? LogEventStatus.Passed : LogEventStatus.Failed;
const message = `the document url matches the specified page "${PageClass.name}"`;
const message = `the document url matches the page "${PageClass.name}"`;
const {routeParams} = route;
const payload = {documentUrl, isMatch, pageParams, routeParams};

log(`Asserts that ${message}`, {...payload, logEventStatus}, LogEventType.InternalAction);

assertValueIsTrue(isMatch, message, payload);
await page.assertPage(isMatch);

await page.afterAssertPage?.();

Expand Down
5 changes: 1 addition & 4 deletions src/actions/pages/navigateToPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {LogEventType} from '../../constants/internal';
import {getDurationWithUnits} from '../../utils/getDurationWithUnits';
import {log} from '../../utils/log';
import {getUserlandHooks} from '../../utils/userland';

import {createPageInstance} from './createPageInstance';

Expand Down Expand Up @@ -33,9 +32,7 @@ export const navigateToPage = async <SomePageClass extends AnyPageClassType>(

await page.beforeNavigateToPage?.();

const {navigateTo} = getUserlandHooks();

await navigateTo(url);
await page.navigateToPage(url);

log(
`Navigation to the page "${PageClass.name}" completed`,
Expand Down
9 changes: 5 additions & 4 deletions src/actions/pages/reloadPage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {LogEventType} from '../../constants/internal';
import {createClientFunction} from '../../createClientFunction';
import {log} from '../../utils/log';

import type {AnyPageClassType} from '../../types/internal';

const clientReloadPage = createClientFunction(() => window.location.reload(), {name: 'reloadPage'});

/**
* Reloads the page, taking into account its stabilization interval.
*/
export const reloadPage = async (page: InstanceType<AnyPageClassType>): Promise<void> => {
log(`Reload page "${page.constructor.name}"`, LogEventType.InternalAction);

await clientReloadPage();
await page.beforeReloadPage?.();

await page.reloadPage();

await page.waitForPageLoaded();

await page.afterReloadPage?.();
};
1 change: 0 additions & 1 deletion src/types/userland/createPackSpecificTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ export type CreatePackSpecificTypes<
MapLogPayloadInConsole: MapLogPayload;
MapLogPayloadInLogFile: MapLogPayload;
MapLogPayloadInReport: MapLogPayloadInReport;
NavigateTo: Hooks['navigateTo'];
}>;
2 changes: 0 additions & 2 deletions src/types/userland/userlandHooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {LogEventType} from '../../constants/internal';

import type {Url} from '../http';
import type {LogContext, LogPayload} from '../log';
import type {IsTestSkippedResult} from '../skipTest';
import type {RunHash, TestRun, TestStaticOptions} from '../testRun';
Expand All @@ -20,5 +19,4 @@ export type UserlandHooks<TestMeta = TestMetaPlaceholder> = {
getMainTestRunParams(this: void, testRun: TestRun<TestMeta>): string;
getTestRunHash(this: void, testRun: TestRun<TestMeta>): RunHash;
isTestSkipped(this: void, testStaticOptions: TestStaticOptions<TestMeta>): IsTestSkippedResult;
navigateTo(this: void, url: Url): Promise<void>;
};
1 change: 1 addition & 0 deletions src/utils/document/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {getDocumentCookie} from './getDocumentCookie';
export {getDocumentTitle} from './getDocumentTitle';
export {getDocumentUrl} from './getDocumentUrl';
export {reloadDocument} from './reloadDocument';
18 changes: 18 additions & 0 deletions src/utils/document/reloadDocument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {createClientFunction} from '../../createClientFunction';

import type {ClientFunction} from '../../types/internal';

let clientReloadDocument: ClientFunction | undefined;

/**
* Reloads current document.
*/
export const reloadDocument = (): Promise<void> => {
if (clientReloadDocument === undefined) {
clientReloadDocument = createClientFunction(() => window.location.reload(), {
name: 'reloadPage',
});
}

return clientReloadDocument();
};
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export {
getHorizontalDistanceBetweenSelectors,
getVerticalDistanceBetweenSelectors,
} from './distanceBetweenSelectors';
export {getDocumentCookie, getDocumentTitle, getDocumentUrl} from './document';
export {getDocumentCookie, getDocumentTitle, getDocumentUrl, reloadDocument} from './document';
export {E2edError, getStackTrace} from './error';
export {getFunctionPresentationForLogs, setCustomInspectOnFunction} from './fn';
export {writeFile} from './fs';
Expand Down

0 comments on commit 3e64fcb

Please sign in to comment.