From 4732136ea5b18252499c641a66a3b493bd803baa Mon Sep 17 00:00:00 2001 From: David Ortner Date: Wed, 8 Jan 2025 00:43:01 +0100 Subject: [PATCH] fix: [#1664] Support using URL as an object for URL:s when using the Browser API (#1672) --- .../src/browser/utilities/BrowserFrameURL.ts | 2 +- packages/happy-dom/test/browser/BrowserFrame.test.ts | 8 +++++++- .../detached-browser/DetachedBrowserFrame.test.ts | 11 ++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/happy-dom/src/browser/utilities/BrowserFrameURL.ts b/packages/happy-dom/src/browser/utilities/BrowserFrameURL.ts index 4c5f4e2b..ca78717b 100644 --- a/packages/happy-dom/src/browser/utilities/BrowserFrameURL.ts +++ b/packages/happy-dom/src/browser/utilities/BrowserFrameURL.ts @@ -13,7 +13,7 @@ export default class BrowserFrameURL { * @returns Relative URL. */ public static getRelativeURL(frame: IBrowserFrame, url: string | URL): URL { - url = (url instanceof URL ? url.toString() : url) || 'about:blank'; + url = url ? String(url) : 'about:blank'; if (url.startsWith('about:') || url.startsWith('javascript:')) { return new URL(url); diff --git a/packages/happy-dom/test/browser/BrowserFrame.test.ts b/packages/happy-dom/test/browser/BrowserFrame.test.ts index d9a84590..060d84c4 100644 --- a/packages/happy-dom/test/browser/BrowserFrame.test.ts +++ b/packages/happy-dom/test/browser/BrowserFrame.test.ts @@ -113,10 +113,16 @@ describe('BrowserFrame', () => { it('Sets the document URL.', () => { const browser = new Browser(); const page = browser.defaultContext.newPage(); - const location = page.mainFrame.window.location; page.mainFrame.url = 'http://localhost:3000'; expect(page.mainFrame.window.location.href).toBe('http://localhost:3000/'); }); + + it(`Supports URL as an object.`, async () => { + const browser = new Browser(); + const page = browser.defaultContext.newPage(); + page.mainFrame.url = ({ toString: () => 'http://localhost:3000' }); + expect(page.mainFrame.window.location.href).toBe('http://localhost:3000/'); + }); }); describe('waitUntilComplete()', () => { diff --git a/packages/happy-dom/test/browser/detached-browser/DetachedBrowserFrame.test.ts b/packages/happy-dom/test/browser/detached-browser/DetachedBrowserFrame.test.ts index ec613271..3e52c3fe 100644 --- a/packages/happy-dom/test/browser/detached-browser/DetachedBrowserFrame.test.ts +++ b/packages/happy-dom/test/browser/detached-browser/DetachedBrowserFrame.test.ts @@ -137,10 +137,19 @@ describe('DetachedBrowserFrame', () => { browser.defaultContext.pages[0].mainFrame ); const page = browser.defaultContext.pages[0]; - const location = page.mainFrame.window.location; page.mainFrame.url = 'http://localhost:3000'; expect(page.mainFrame.window.location.href).toBe('http://localhost:3000/'); }); + + it(`Supports URL as an object.`, async () => { + const browser = new DetachedBrowser(BrowserWindow); + browser.defaultContext.pages[0].mainFrame.window = new BrowserWindow( + browser.defaultContext.pages[0].mainFrame + ); + const page = browser.defaultContext.pages[0]; + page.mainFrame.url = ({ toString: () => 'http://localhost:3000' }); + expect(page.mainFrame.window.location.href).toBe('http://localhost:3000/'); + }); }); describe('waitUntilComplete()', () => {