From c6ca530c6f8ec02a5fedf3ff857519fa18413d5c Mon Sep 17 00:00:00 2001 From: David Ortner Date: Wed, 10 Jan 2024 13:16:19 +0100 Subject: [PATCH] #466@trivial: Adds unit tests for waitForNavigation(). --- .../test/browser/BrowserFrame.test.ts | 36 +++++++++++++++++++ .../test/browser/BrowserPage.test.ts | 17 +++++++++ .../DetachedBrowserFrame.test.ts | 36 +++++++++++++++++++ .../DetachedBrowserPage.test.ts | 17 +++++++++ 4 files changed, 106 insertions(+) diff --git a/packages/happy-dom/test/browser/BrowserFrame.test.ts b/packages/happy-dom/test/browser/BrowserFrame.test.ts index 47de4b452..58984f7f5 100644 --- a/packages/happy-dom/test/browser/BrowserFrame.test.ts +++ b/packages/happy-dom/test/browser/BrowserFrame.test.ts @@ -12,6 +12,7 @@ import BrowserNavigationCrossOriginPolicyEnum from '../../src/browser/enums/Brow import BrowserFrameFactory from '../../src/browser/utilities/BrowserFrameFactory'; import BrowserErrorCaptureEnum from '../../src/browser/enums/BrowserErrorCaptureEnum'; import Headers from '../../src/fetch/Headers'; +import IHTMLAnchorElement from '../../src/nodes/html-anchor-element/IHTMLAnchorElement'; describe('BrowserFrame', () => { afterEach(() => { @@ -135,6 +136,41 @@ describe('BrowserFrame', () => { }); }); + describe('waitForNavigation()', () => { + it('Waits page to have been navigated.', async () => { + let count = 0; + vi.spyOn(Fetch.prototype, 'send').mockImplementation(function (): Promise { + count++; + return Promise.resolve({ + text: () => + new Promise((resolve) => + setTimeout( + () => + resolve( + count === 1 ? '' : 'Navigated' + ), + 1 + ) + ) + }); + }); + + const browser = new Browser(); + const page = browser.newPage(); + + await page.mainFrame.goto('http://localhost:3000', { + referrer: 'http://localhost:3000/referrer', + referrerPolicy: 'no-referrer-when-downgrade' + }); + + (page.mainFrame.document.querySelector('a')).click(); + + await page.mainFrame.waitForNavigation(); + + expect(page.mainFrame.document.querySelector('b')?.textContent).toBe('Navigated'); + }); + }); + describe('abort()', () => { it('Aborts all ongoing operations.', async () => { const browser = new Browser(); diff --git a/packages/happy-dom/test/browser/BrowserPage.test.ts b/packages/happy-dom/test/browser/BrowserPage.test.ts index d99036981..1310aba28 100644 --- a/packages/happy-dom/test/browser/BrowserPage.test.ts +++ b/packages/happy-dom/test/browser/BrowserPage.test.ts @@ -137,6 +137,23 @@ describe('BrowserPage', () => { }); }); + describe('waitForNavigation()', () => { + it('Waits page to have been navigated.', async () => { + const browser = new Browser(); + const page = browser.newPage(); + let isCalled = false; + + vi.spyOn(page.mainFrame, 'waitForNavigation').mockImplementation((): Promise => { + isCalled = true; + return Promise.resolve(); + }); + + await page.waitForNavigation(); + + expect(isCalled).toBe(true); + }); + }); + describe('abort()', () => { it('Aborts all ongoing operations.', async () => { const browser = new Browser(); 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 147d2f59d..c433b9b0c 100644 --- a/packages/happy-dom/test/browser/detached-browser/DetachedBrowserFrame.test.ts +++ b/packages/happy-dom/test/browser/detached-browser/DetachedBrowserFrame.test.ts @@ -13,6 +13,7 @@ import BrowserNavigationCrossOriginPolicyEnum from '../../../src/browser/enums/B import BrowserFrameFactory from '../../../src/browser/utilities/BrowserFrameFactory'; import BrowserErrorCaptureEnum from '../../../src/browser/enums/BrowserErrorCaptureEnum'; import Headers from '../../../src/fetch/Headers'; +import IHTMLAnchorElement from '../../../src/nodes/html-anchor-element/IHTMLAnchorElement'; describe('DetachedBrowserFrame', () => { afterEach(() => { @@ -145,6 +146,41 @@ describe('DetachedBrowserFrame', () => { }); }); + describe('waitForNavigation()', () => { + it('Waits page to have been navigated.', async () => { + let count = 0; + vi.spyOn(Fetch.prototype, 'send').mockImplementation(function (): Promise { + count++; + return Promise.resolve({ + text: () => + new Promise((resolve) => + setTimeout( + () => + resolve( + count === 1 ? '' : 'Navigated' + ), + 1 + ) + ) + }); + }); + + const browser = new DetachedBrowser(BrowserWindow); + const page = browser.newPage(); + + await page.mainFrame.goto('http://localhost:3000', { + referrer: 'http://localhost:3000/referrer', + referrerPolicy: 'no-referrer-when-downgrade' + }); + + (page.mainFrame.document.querySelector('a')).click(); + + await page.mainFrame.waitForNavigation(); + + expect(page.mainFrame.document.querySelector('b')?.textContent).toBe('Navigated'); + }); + }); + describe('abort()', () => { it('Aborts all ongoing operations.', async () => { const browser = new DetachedBrowser(BrowserWindow); diff --git a/packages/happy-dom/test/browser/detached-browser/DetachedBrowserPage.test.ts b/packages/happy-dom/test/browser/detached-browser/DetachedBrowserPage.test.ts index 43077d8b3..934681d4b 100644 --- a/packages/happy-dom/test/browser/detached-browser/DetachedBrowserPage.test.ts +++ b/packages/happy-dom/test/browser/detached-browser/DetachedBrowserPage.test.ts @@ -147,6 +147,23 @@ describe('DetachedBrowserPage', () => { }); }); + describe('waitForNavigation()', () => { + it('Waits page to have been navigated.', async () => { + const browser = new DetachedBrowser(BrowserWindow); + const page = browser.newPage(); + let isCalled = false; + + vi.spyOn(page.mainFrame, 'waitForNavigation').mockImplementation((): Promise => { + isCalled = true; + return Promise.resolve(); + }); + + await page.waitForNavigation(); + + expect(isCalled).toBe(true); + }); + }); + describe('abort()', () => { it('Aborts all ongoing operations.', async () => { const browser = new DetachedBrowser(BrowserWindow);