From cd6b48c201cfb2ef02a9fc00653bbe3f0f84ef74 Mon Sep 17 00:00:00 2001 From: yandeu <20306025+yandeu@users.noreply.github.com> Date: Fri, 21 Jul 2023 16:53:28 +0200 Subject: [PATCH] dev --- scripts/browserTest/index.mjs | 32 ++++++++++++++++++++++++++++++++ test/browser/link.test.html | 22 ++++++++++++++++++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/scripts/browserTest/index.mjs b/scripts/browserTest/index.mjs index a54297f..5c1f550 100644 --- a/scripts/browserTest/index.mjs +++ b/scripts/browserTest/index.mjs @@ -1,3 +1,5 @@ +// @ts-check + import crypto from 'crypto' import puppeteer from 'puppeteer' import { createServer } from 'http' @@ -21,14 +23,44 @@ let hasError = false const server = createServer(requestListener({ serve, collectCoverage })) server.closeAsync = () => new Promise(resolve => server.close(() => resolve())) +/** + * @param {{fileName:string, browser:puppeteer.Browser}} param0 + */ const main = async ({ fileName, browser }) => { console.log(`> ${fileName}`) const page = await browser.newPage() + await page.setRequestInterception(true); + + /** @type {Array} */ + const requests = []; // Enable both JavaScript and CSS coverage if (collectCoverage) await page.coverage.startJSCoverage() + // Blocks all window.location.href requests + page.on('request', async request => { + let isNavRequest = request.isNavigationRequest() && request.frame() === page.mainFrame(); + if (!isNavRequest) { + request.continue(); + return; + } + + requests.push(request); + if (requests.length == 1) { + request.continue(); + return; + } + + request.abort('aborted'); + + const url = requests[requests.length - 1].url() + await page.evaluate((url) => { + window.__gotohref__ = url; + }, url); + + }); + // Navigate to page let url = `http://localhost:8080/${fileName.replace(/^\+/, '')}` await page.goto(url, { waitUntil: 'networkidle2' }) diff --git a/test/browser/link.test.html b/test/browser/link.test.html index e4fe5e9..d9dd850 100644 --- a/test/browser/link.test.html +++ b/test/browser/link.test.html @@ -1,4 +1,4 @@ - + @@ -70,12 +70,26 @@ headLastChild().remove() }) - describe('Back/Delay (TODO)', async () => { + describe('Delay', async () => { + const App = () => h(Link, { delay: 1000, href: 'https://nanojsx.github.io/' }, 'nanojsx.github.io') + + render(App, root()) + + await Test.wait() + + /** @type {HTMLLinkElement} */ + const link = root()?.firstChild + link.click() + + await Test.wait(2000) + + expect(window.__gotohref__).toBe('https://nanojsx.github.io/') + }) + + describe('Back (TODO)', async () => { const back = (Link, { back: true, href: 'https://nanojsx.github.io/' }, 'nanojsx.github.io') - const delay = (Link, { delay: 150, href: 'https://nanojsx.github.io/' }, 'nanojsx.github.io') expect('skip', null).toBe(null, 'Should test "back"') - expect('skip', null).toBe(null, 'Should test "delay"') }) Test.start()