Skip to content

Commit

Permalink
plotly-graph: use Promise instead of deprecated callback for printToPDF
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinerg committed Apr 24, 2020
1 parent ae6bf02 commit efd5511
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
18 changes: 7 additions & 11 deletions src/component/plotly-graph/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,15 @@ function toPDF (imgData, imgOpts, bgColor) {
img.onerror = reject
img.src = "${imgData}"
setTimeout(() => reject(new Error('too long to load image')), ${cst.pdfPageLoadImgTimeout})
})`).then(() => {
win.webContents.printToPDF(printOpts, (err, pdfData) => {
if (err) {
reject(err)
} else {
resolve(pdfData)
}
})`)
.then(() => win.webContents.printToPDF(printOpts))
.then(pdfData => {
win.close()
resolve(pdfData)
}).catch((err) => {
win.close()
reject(err)
})
}).catch((err) => {
reject(err)
win.close()
})
})
}

Expand Down
6 changes: 3 additions & 3 deletions test/unit/plotly-graph_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ tap.test('render:', t => {
mockBrowser()
const { win, restore } = mockWindow()
win.webContents.executeJavaScript.returns(new Promise(resolve => resolve()))
win.webContents.printToPDF.yields(null, 'pdf data')
win.webContents.printToPDF.returns(Promise.resolve('pdf data'))

fn({ format: 'pdf' }, {}, (errorCode, result) => {
t.equal(errorCode, null)
Expand Down Expand Up @@ -835,7 +835,7 @@ tap.test('render:', t => {
mockBrowser()
const { win, restore } = mockWindow()
win.webContents.executeJavaScript.returns(new Promise(resolve => resolve()))
win.webContents.printToPDF.yields(null, 'pdf data')
win.webContents.printToPDF.returns(Promise.resolve('pdf data'))

fn({ format: 'pdf' }, {}, (errorCode, result) => {
t.equal(errorCode, null)
Expand Down Expand Up @@ -903,7 +903,7 @@ tap.test('render:', t => {
mockBrowser()
const { win, restore } = mockWindow()
win.webContents.executeJavaScript.returns(new Promise(resolve => resolve()))
win.webContents.printToPDF.yields(new Error('oops'))
win.webContents.printToPDF.returns(Promise.reject(new Error('oops')))

fn({ format: 'pdf' }, {}, (errorCode, result) => {
t.equal(errorCode, 525)
Expand Down

0 comments on commit efd5511

Please sign in to comment.