Make executeJavaScript return a Promise so that caught errors can be sent to the caller
This commit is contained in:
parent
75b010ce63
commit
857e1da6a3
5 changed files with 59 additions and 7 deletions
|
@ -1462,8 +1462,10 @@ describe('browser-window module', function () {
|
|||
|
||||
describe('window.webContents.executeJavaScript', function () {
|
||||
var expected = 'hello, world!'
|
||||
var expectedErrorMsg = 'woops!'
|
||||
var code = '(() => "' + expected + '")()'
|
||||
var asyncCode = '(() => new Promise(r => setTimeout(() => r("' + expected + '"), 500)))()'
|
||||
var badAsyncCode = '(() => new Promise((r, e) => setTimeout(() => e("' + expectedErrorMsg + '"), 500)))()'
|
||||
|
||||
it('doesnt throw when no calback is provided', function () {
|
||||
const result = ipcRenderer.sendSync('executeJavaScript', code, false)
|
||||
|
@ -1486,6 +1488,30 @@ describe('browser-window module', function () {
|
|||
})
|
||||
})
|
||||
|
||||
it('resolves the returned promise with the result', function (done) {
|
||||
ipcRenderer.send('executeJavaScript', code, true)
|
||||
ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {
|
||||
assert.equal(result, expected)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('resolves the returned promise with the result if the code returns an asyncronous promise', function (done) {
|
||||
ipcRenderer.send('executeJavaScript', asyncCode, true)
|
||||
ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {
|
||||
assert.equal(result, expected)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects the returned promise if an async error is thrown', function (done) {
|
||||
ipcRenderer.send('executeJavaScript', badAsyncCode, true)
|
||||
ipcRenderer.once('executeJavaScript-promise-error', function (event, error) {
|
||||
assert.equal(error, expectedErrorMsg)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('works after page load and during subframe load', function (done) {
|
||||
w.webContents.once('did-finish-load', function () {
|
||||
// initiate a sub-frame load, then try and execute script during it
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue