import type { TestFixture, Page } from '@playwright/test'; import * as textAdMock from 'mocks/ad/textAd'; export type MockTextAdFixture = () => Promise; const SEVIO_LOADER_URL = 'https://cdn.adx.ws/scripts/loader.js'; const fixture: TestFixture = async({ page }, use) => { await use(async() => { const { thumbnail, sponsored, title, clickURL, ctatext } = textAdMock.duck; const adHtml = [ `Ads: `, // eslint-disable-next-line max-len ``, `${ sponsored } \u2013 ${ title } `, `${ ctatext }`, ].join(''); // Route the Sevio loader script so that onLoad fires (not onError), which starts // the ad-served timeout in the component. await page.route(SEVIO_LOADER_URL, (route) => { return route.fulfill({ status: 200, contentType: 'application/javascript', body: '' }); }); await page.evaluate((html) => { const queue: Array>> = []; queue.push = () => { // Defer injection so the MutationObserver (set up in the next useEffect) is // already observing the div when the HTML arrives. setTimeout(() => { const container = document.querySelector('.sevioads'); if (container) { container.innerHTML = html; } }, 0); return queue.length; }; Object.defineProperty(window, 'sevioads', { value: queue, writable: true, configurable: true, }); }, adHtml); await page.route(thumbnail, (route) => { return route.fulfill({ status: 200, path: './playwright/mocks/image_s.jpg', }); }); }); }; export default fixture;