fix: abort more descriptively for beforeunload (#48960)

This commit is contained in:
Shelley Vohr
2025-11-18 18:16:08 +01:00
committed by GitHub
parent 54a617caab
commit 13e84e6868
5 changed files with 46 additions and 3 deletions

View File

@@ -109,6 +109,7 @@ describe('webContents module', () => {
await closeAllWindows();
await cleanupWebContents();
});
it('does not emit if beforeunload returns undefined in a BrowserWindow', async () => {
const w = new BrowserWindow({ show: false });
w.webContents.once('will-prevent-unload', () => {
@@ -162,6 +163,35 @@ describe('webContents module', () => {
w.close();
await wait;
});
it('fails loading a subsequent page after beforeunload is not prevented', async () => {
const w = new BrowserWindow({ show: false });
const didFailLoad = once(w.webContents, 'did-fail-load');
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
await w.webContents.executeJavaScript('console.log(\'gesture\')', true);
w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'a.html'));
const [, code, , validatedURL] = await didFailLoad;
expect(code).to.equal(-3); // ERR_ABORTED
const { href: expectedURL } = url.pathToFileURL(path.join(__dirname, 'fixtures', 'pages', 'a.html'));
expect(validatedURL).to.equal(expectedURL);
});
it('allows loading a subsequent page after beforeunload is prevented', async () => {
const w = new BrowserWindow({ show: false });
w.webContents.once('will-prevent-unload', event => event.preventDefault());
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
await w.webContents.executeJavaScript('console.log(\'gesture\')', true);
await w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'a.html'));
const pageTitle = await w.webContents.executeJavaScript('document.title');
expect(pageTitle).to.equal('test');
const wait = once(w, 'closed');
w.close();
await wait;
});
});
describe('webContents.send(channel, args...)', () => {