mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-18 07:51:55 +03:00
8d6fb51908
refs https://github.com/TryGhost/Team/issues/3504 Not complete yet, but contains the basic structure and a few tests that work and should run in CI.
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import {E2E_PORT} from '../../playwright.config';
|
|
import {Page} from '@playwright/test';
|
|
|
|
const MOCKED_SITE_URL = 'https://localhost:1234';
|
|
|
|
type LastApiRequest = {
|
|
body: null | any
|
|
};
|
|
|
|
export async function initialize({page, path, apiStatus, embeddedOnUrl, ...options}: {page: Page, embeddedOnUrl?: string, path?: string; title?: string, description?: string, icon?: string, backgroundColor?: string, buttonColor?: string, site?: string, 'label-1'?: string, 'label-2'?: string, apiStatus?: number}) {
|
|
const sitePath = `${embeddedOnUrl ?? MOCKED_SITE_URL}${path ?? ''}`;
|
|
await page.route(sitePath, async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
body: '<html><head><meta charset="UTF-8" /></head><body></body></html>'
|
|
});
|
|
});
|
|
|
|
const url = `http://localhost:${E2E_PORT}/signup-form.min.js`;
|
|
await page.setViewportSize({width: 1000, height: 1000});
|
|
|
|
await page.goto(sitePath);
|
|
const lastApiRequest = await mockApi({page, status: apiStatus});
|
|
|
|
if (!options.site) {
|
|
options.site = MOCKED_SITE_URL;
|
|
}
|
|
|
|
await page.evaluate((data) => {
|
|
const scriptTag = document.createElement('script');
|
|
scriptTag.src = data.url;
|
|
|
|
for (const option of Object.keys(data.options)) {
|
|
scriptTag.dataset[option] = data.options[option];
|
|
}
|
|
document.body.appendChild(scriptTag);
|
|
}, {url, options});
|
|
|
|
await page.waitForSelector('iframe');
|
|
|
|
return {
|
|
frame: page.frameLocator('iframe'),
|
|
lastApiRequest
|
|
};
|
|
}
|
|
|
|
export async function mockApi({page, status = 200}: {page: any, status?: number}) {
|
|
const lastApiRequest: LastApiRequest = {
|
|
body: null
|
|
};
|
|
|
|
await page.route(`${MOCKED_SITE_URL}/members/api/send-magic-link/`, async (route) => {
|
|
const requestBody = JSON.parse(route.request().postData());
|
|
lastApiRequest.body = requestBody;
|
|
|
|
await route.fulfill({
|
|
status,
|
|
body: 'ok'
|
|
});
|
|
});
|
|
|
|
await page.route(`${MOCKED_SITE_URL}/invalid/members/api/send-magic-link/`, async (route) => {
|
|
await route.abort('addressunreachable');
|
|
});
|
|
|
|
return lastApiRequest;
|
|
}
|