diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index f19ff06b111..9f45492dcd1 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1012,6 +1012,25 @@ describe('BrowserWindow module', function () { }) }) }) + + it('supports calling preventDefault on new-window events', (done) => { + w.destroy() + w = new BrowserWindow({ + show: false, + webPreferences: { + sandbox: true, + } + }) + const initialWebContents = webContents.getAllWebContents() + ipcRenderer.send('prevent-next-new-window', w.webContents.id) + w.webContents.once('new-window', () => { + process.nextTick(() => { + assert.deepEqual(webContents.getAllWebContents().length, initialWebContents.length) + done() + }) + }) + w.loadURL('file://' + path.join(fixtures, 'pages', 'window-open.html')) + }) }) }) diff --git a/spec/static/main.js b/spec/static/main.js index b9d7d9473c3..2b625fd0952 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -245,3 +245,8 @@ ipcMain.on('create-window-with-options-cycle', (event) => { const window = new BrowserWindow({show: false, foo: foo}) event.returnValue = window.id }) + + +ipcMain.on('prevent-next-new-window', (event, id) => { + webContents.fromId(id).once('new-window', event => event.preventDefault()) +})