page-with
Version:
A library for usage example-driven in-browser testing of your own libraries.
95 lines (94 loc) • 3.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.pageWith = void 0;
const fs = require("fs");
const path = require("path");
const createBrowser_1 = require("./createBrowser");
const createLogger_1 = require("./internal/createLogger");
const request_1 = require("./utils/request");
const debug_1 = require("./utils/debug");
const spyOnConsole_1 = require("./utils/spyOnConsole");
const log = (0, createLogger_1.createLogger)('page');
/**
* Open a new page with the given usage scenario.
*/
async function pageWith(options) {
const { example, markup, contentBase, title } = options;
/**
* @todo Pass that `pageId` to the server to establish a unique route
* for this page!
*/
log(`loading example at "${example}"`);
if (example) {
const fullExamplePath = path.isAbsolute(example)
? example
: path.resolve(process.cwd(), example);
if (!fs.existsSync(fullExamplePath)) {
throw new Error(`Failed to load a scenario at "${fullExamplePath}": given file does not exist.`);
}
}
if (markup) {
log('using a custom markup', markup);
}
if (contentBase) {
log('using a custom content base', contentBase);
}
createBrowser_1.server.use((app) => {
if (title) {
app.set('title', title);
}
if (markup) {
app.set('markup', markup);
}
if (contentBase) {
app.set('contentBase', contentBase);
}
});
const cleanupRoutes = options.routes ? createBrowser_1.server.use(options.routes) : null;
if (options.serverOptions) {
Object.entries(options.serverOptions).forEach(([name, value]) => {
createBrowser_1.server.setOption(name, value);
});
}
const [context] = await Promise.all([
createBrowser_1.browser.newContext(),
createBrowser_1.server.compile(example),
]);
const serverContext = createBrowser_1.server.createContext(example, {
title: options.title,
markup: options.markup,
});
log('compiled example running at', serverContext.previewUrl);
const page = await context.newPage();
const consoleSpy = (0, spyOnConsole_1.spyOnConsole)(page);
log('navigating to the compiled example...', serverContext.previewUrl);
await page.goto(serverContext.previewUrl, { waitUntil: 'networkidle' });
if (options.env) {
await page.evaluate((variables) => {
variables.forEach(([variableName, value]) => {
// @ts-expect-error Adding a custom "window" property.
window[variableName] = value;
});
}, Object.entries(options.env));
}
page.on('close', () => {
log('closing the page...');
cleanupRoutes === null || cleanupRoutes === void 0 ? void 0 : cleanupRoutes();
});
return {
page,
origin: serverContext.previewUrl,
context,
makeUrl(chunk) {
var _a;
return new URL(chunk, (_a = createBrowser_1.server.connectionInfo) === null || _a === void 0 ? void 0 : _a.url).toString();
},
debug(customPage) {
return (0, debug_1.debug)(customPage || page);
},
request: (0, request_1.createRequestUtil)(page, createBrowser_1.server),
consoleSpy,
server: createBrowser_1.server,
};
}
exports.pageWith = pageWith;