@augment-vir/test
Version:
A universal testing suite that works with Mocha style test runners _and_ Node.js's built-in test runner.
54 lines (53 loc) • 1.67 kB
JavaScript
import { check } from '@augment-vir/assert';
import { omitObjectKeys } from '@augment-vir/common';
import { buildUrl } from 'url-vir';
import { assertTestContext, assertWrapTestContext, extractTestNameAsDir, TestEnv, } from '../augments/universal-testing-suite/universal-test-context.js';
/**
* Converts {@link NavOptions} into an actionable URL string.
*
* @category Internal
*/
export function extractNavUrl(testContext, options) {
return buildUrl(options.baseFrontendUrl ||
assertWrapTestContext(testContext, TestEnv.Playwright).page.url(), {
...omitObjectKeys(options, ['paths']),
...(options.paths
? check.isArray(options.paths)
? {
paths: options.paths,
}
: {
paths: options.paths.fullPaths,
}
: {}),
}).href;
}
/**
* The test name appended to the frontend when `testPlaywright.nav` is used.
*
* @category Internal
*/
export const playwrightTeatNameUrlParam = 'test-name';
/**
* Navigate to a URL in Playwright via given paths.
*
* @category Internal
*/
export async function navigateTo(testContext, options) {
assertTestContext(testContext, TestEnv.Playwright);
const page = testContext.page;
const testName = extractTestNameAsDir(testContext);
const finalPath = buildUrl(extractNavUrl(testContext, options), {
search: {
[playwrightTeatNameUrlParam]: [testName],
},
}).href;
if (page.url() === finalPath) {
return;
}
else {
await page.goto(finalPath, {
waitUntil: 'domcontentloaded',
});
}
}