feat: optional typically sync callback for WebFrame#executeJavaScript* (#21423)

This commit is contained in:
bughit 2020-03-02 19:11:40 -05:00 committed by GitHub
parent 748a917ffd
commit 84126a4f23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 197 additions and 23 deletions

View file

@ -25,4 +25,116 @@ describe('webFrame module', function () {
it('findFrameByRoutingId() does not crash when not found', () => {
expect(webFrame.findFrameByRoutingId(-1)).to.be.null()
})
describe('executeJavaScript', () => {
let childFrameElement, childFrame
before(() => {
childFrameElement = document.createElement('iframe')
document.body.appendChild(childFrameElement)
childFrame = webFrame.firstChild
})
after(() => {
childFrameElement.remove()
})
it('executeJavaScript() yields results via a promise and a sync callback', done => {
let callbackResult, callbackError
childFrame
.executeJavaScript('1 + 1', (result, error) => {
callbackResult = result
callbackError = error
})
.then(
promiseResult => {
expect(promiseResult).to.equal(2)
done()
},
promiseError => {
done(promiseError)
}
)
expect(callbackResult).to.equal(2)
expect(callbackError).to.be.undefined()
})
it('executeJavaScriptInIsolatedWorld() yields results via a promise and a sync callback', done => {
let callbackResult, callbackError
childFrame
.executeJavaScriptInIsolatedWorld(999, [{ code: '1 + 1' }], (result, error) => {
callbackResult = result
callbackError = error
})
.then(
promiseResult => {
expect(promiseResult).to.equal(2)
done()
},
promiseError => {
done(promiseError)
}
)
expect(callbackResult).to.equal(2)
expect(callbackError).to.be.undefined()
})
it('executeJavaScript() yields errors via a promise and a sync callback', done => {
let callbackResult, callbackError
childFrame
.executeJavaScript('thisShouldProduceAnError()', (result, error) => {
callbackResult = result
callbackError = error
})
.then(
promiseResult => {
done(new Error('error is expected'))
},
promiseError => {
expect(promiseError).to.be.an('error')
done()
}
)
expect(callbackResult).to.be.undefined()
expect(callbackError).to.be.an('error')
})
// executeJavaScriptInIsolatedWorld is failing to detect exec errors and is neither
// rejecting nor passing the error to the callback. This predates the reintroduction
// of the callback so will not be fixed as part of the callback PR
// if/when this is fixed the test can be uncommented.
//
// it('executeJavaScriptInIsolatedWorld() yields errors via a promise and a sync callback', done => {
// let callbackResult, callbackError
//
// childFrame
// .executeJavaScriptInIsolatedWorld(999, [{ code: 'thisShouldProduceAnError()' }], (result, error) => {
// callbackResult = result
// callbackError = error
// })
// .then(
// promiseResult => {
// done(new Error('error is expected'))
// },
// promiseError => {
// expect(promiseError).to.be.an('error')
// done()
// }
// )
//
// expect(callbackResult).to.be.undefined()
// expect(callbackError).to.be.an('error')
// })
it('executeJavaScript(InIsolatedWorld) can be used without a callback', async () => {
expect(await webFrame.executeJavaScript('1 + 1')).to.equal(2)
expect(await webFrame.executeJavaScriptInIsolatedWorld(999, [{ code: '1 + 1' }])).to.equal(2)
})
})
})