Skip to content

Commit

Permalink
spec: add tests for the will-redirect event
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Jul 30, 2018
1 parent f483586 commit 97d4dc6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
63 changes: 63 additions & 0 deletions spec/api-browser-window-spec.js
Expand Up @@ -82,6 +82,12 @@ describe('BrowserWindow module', () => {
}
})
})
} else if (req.url === '/302') {
res.setHeader('Location', '/200')
res.statusCode = 302
res.end()
} else if (req.url === '/navigate-302') {
res.end(`<html><body><script>window.location='${server.url}/302'</script></body></html>`)
} else {
res.end()
}
Expand Down Expand Up @@ -337,6 +343,63 @@ describe('BrowserWindow module', () => {
})
})

describe('will-redirect event', () => {
it('is emitted on redirects', (done) => {
w.webContents.on('will-redirect', (event, url) => {
done()
})
w.loadURL(`${server.url}/302`)
})

it('is emitted after will-navigate on redirects', (done) => {
let navigateCalled = false
w.loadURL(`${server.url}/navigate-302`)
w.webContents.on('will-navigate', () => {
navigateCalled = true
})
w.webContents.on('will-redirect', (event, url) => {
expect(navigateCalled).to.equal(true, 'should have called will-navigate first')
done()
})
})

it('is emitted before did-stop-loading on redirects', (done) => {
let stopCalled = false
w.webContents.on('did-stop-loading', () => {
stopCalled = true
})
w.webContents.on('will-redirect', (event, url) => {
expect(stopCalled).to.equal(false, 'should not have called did-stop-loading first')
done()
})
w.loadURL(`${server.url}/302`)
})

it('allows the window to be closed from the event listener', (done) => {
ipcRenderer.send('close-on-will-redirect', w.id)
ipcRenderer.once('closed-on-will-redirect', () => { done() })
w.loadURL(`${server.url}/302`)
})

it('can be prevented', (done) => {
ipcRenderer.send('prevent-will-redirect', w.id)
w.webContents.on('will-navigate', (e, url) => {
expect(url).to.equal(`${server.url}/302`)
})
w.webContents.on('did-stop-loading', () => {
expect(w.webContents.getURL()).to.equal(
`${server.url}/navigate-302`,
'url should not have changed after navigation event'
)
done()
})
w.webContents.on('will-redirect', (e, url) => {
expect(url).to.equal(`${server.url}/200`)
})
w.loadURL(`${server.url}/navigate-302`)
})
})

describe('BrowserWindow.show()', () => {
before(function () {
if (isCI) {
Expand Down
16 changes: 16 additions & 0 deletions spec/static/main.js
Expand Up @@ -263,6 +263,22 @@ ipcMain.on('close-on-will-navigate', (event, id) => {
})
})

ipcMain.on('close-on-will-redirect', (event, id) => {
const contents = event.sender
const window = BrowserWindow.fromId(id)
window.webContents.once('will-redirect', (event, input) => {
window.close()
contents.send('closed-on-will-redirect')
})
})

ipcMain.on('prevent-will-redirect', (event, id) => {
const window = BrowserWindow.fromId(id)
window.webContents.once('will-redirect', (event) => {
event.preventDefault()
})
})

ipcMain.on('create-window-with-options-cycle', (event) => {
// This can't be done over remote since cycles are already
// nulled out at the IPC layer
Expand Down

0 comments on commit 97d4dc6

Please sign in to comment.