Skip to content

Commit 0943a79

Browse files
committed
dash-preview: properly parse (pdf_options.)pageSize and 🔒 with test
1 parent f5a736f commit 0943a79

File tree

2 files changed

+91
-45
lines changed

2 files changed

+91
-45
lines changed

src/component/plotly-dash-preview/parse.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,25 @@ function parse (body, req, opts, sendToRenderer) {
3636
result.timeOut = body.timeout
3737
result.tries = Number(result.timeOut * 1000 / cst.minInterval)
3838

39-
if (cst.sizeMapping[result.pdfOptions.pageSize]) {
40-
result.browserSize = cst.sizeMapping[result.pdfOptions.pageSize]
41-
} else if (body.pageSize && isPositiveNumeric(body.pageSize.width) &&
42-
isPositiveNumeric(body.pageSize.height)) {
39+
var pageSize
40+
if (result.pdfOptions.pageSize) {
41+
pageSize = result.pdfOptions.pageSize
42+
} else if (body.pageSize) {
43+
pageSize = body.pageSize
44+
}
45+
46+
if (cst.sizeMapping[pageSize]) {
47+
result.browserSize = cst.sizeMapping[pageSize]
48+
result.pdfOptions.pageSize = pageSize
49+
} else if (pageSize && isPositiveNumeric(pageSize.width) &&
50+
isPositiveNumeric(pageSize.height)) {
4351
result.browserSize = {
44-
width: body.pageSize.width * cst.pixelsInMicron,
45-
height: body.pageSize.height * cst.pixelsInMicron
52+
width: pageSize.width * cst.pixelsInMicron,
53+
height: pageSize.height * cst.pixelsInMicron
4654
}
4755
result.pdfOptions.pageSize = {
48-
width: Math.ceil(body.pageSize.width),
49-
height: Math.ceil(body.pageSize.height)
56+
width: Math.ceil(pageSize.width),
57+
height: Math.ceil(pageSize.height)
5058
}
5159
} else {
5260
return errorOut(

test/unit/plotly-dash-preview_test.js

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ const tap = require('tap')
22
const sinon = require('sinon')
33

44
const _module = require('../../src/component/plotly-dash-preview')
5+
const constants = require('../../src/component/plotly-dash-preview/constants')
56
const remote = require('../../src/util/remote')
67
const { createMockWindow } = require('../common')
78

9+
function clone (obj) {
10+
return JSON.parse(JSON.stringify(obj))
11+
}
12+
813
tap.test('parse:', t => {
914
const fn = _module.parse
1015

@@ -30,49 +35,82 @@ tap.test('parse:', t => {
3035
t.end()
3136
})
3237
})
33-
t.test('should error when pageSize is not given', t => {
34-
fn({
38+
t.test('pageSize options:', t => {
39+
const mock = {
3540
url: 'https://dash-app.com',
3641
selector: 'dummy'
37-
}, {}, {}, (errorCode, result) => {
38-
t.equal(errorCode, 400)
39-
t.same(result.msg, 'pageSize must either be A3, A4, A5, Legal, Letter, ' +
40-
'Tabloid or an Object containing height and width in microns.')
41-
t.end()
42-
})
43-
})
44-
t.test('should parse properly when pageSize is given', t => {
45-
fn({
46-
url: 'https://dash-app.com',
47-
selector: 'dummy',
48-
pageSize: { height: 1000, width: 1000 }
49-
}, {}, {}, (errorCode, result) => {
50-
t.equal(errorCode, null)
51-
52-
// height/width are converted from microns to pixels:
53-
t.same(result.browserSize, {
54-
height: 4,
55-
width: 4
42+
}
43+
44+
t.test('should error when not given', t => {
45+
fn({
46+
url: 'https://dash-app.com',
47+
selector: 'dummy'
48+
}, {}, {}, (errorCode, result) => {
49+
t.equal(errorCode, 400)
50+
t.same(result.msg, 'pageSize must either be A3, A4, A5, Legal, Letter, ' +
51+
'Tabloid or an Object containing height and width in microns.')
52+
t.end()
5653
})
57-
t.same(result.pdfOptions.pageSize, {
58-
height: 1000,
59-
width: 1000
54+
})
55+
56+
function assertEqualSize (browserSize, pageSize) {
57+
// Browser size is always integer pixels
58+
var bW = browserSize.width
59+
var bH = browserSize.height
60+
t.ok(Number.isInteger(bW), 'browserSize.width is not an integer')
61+
t.ok(Number.isInteger(bH), 'browserSize.height is not an integer')
62+
var pW, pH
63+
if (constants.sizeMapping[pageSize]) {
64+
var equivalentPixelSize = constants.sizeMapping[pageSize]
65+
pW = equivalentPixelSize.width
66+
pH = equivalentPixelSize.height
67+
} else {
68+
pW = pageSize.width * constants.pixelsInMicron
69+
pH = pageSize.height * constants.pixelsInMicron
70+
}
71+
// Round
72+
pW = Math.ceil(pW)
73+
pH = Math.ceil(pH)
74+
t.equal(bW, pW, 'browser and page should have the same width')
75+
t.equal(bH, pH, 'browser and page should have the same height')
76+
}
77+
78+
// Browser size and page size should be the same assuming a DPI of 96
79+
// to make sure plotly.js figures are appropriately sized right away for print
80+
[
81+
[true, { height: 1000, width: 1000 }],
82+
[true, 'Letter'],
83+
[false, { height: 1000, width: 1000 }],
84+
[false, 'Letter']
85+
].forEach(arg => {
86+
var toplevel = arg[0]
87+
var pageSize = arg[1]
88+
t.test(`should size window and page properly when ${toplevel ? '' : 'pdf_options.'}pageSize is given`, t => {
89+
var body = clone(mock)
90+
if (toplevel) {
91+
body.pageSize = pageSize
92+
} else {
93+
body.pdf_options = { pageSize: pageSize }
94+
}
95+
fn(body, {}, {}, (errorCode, result) => {
96+
t.equal(errorCode, null)
97+
t.same(result.pdfOptions.pageSize, pageSize)
98+
assertEqualSize(result.browserSize, result.pdfOptions.pageSize)
99+
t.end()
100+
})
60101
})
61-
t.end()
62102
})
63-
})
64-
t.test('should parse properly when pdf_options are given', t => {
65-
fn({
66-
url: 'https://dash-app.com',
67-
selector: 'dummy',
68-
pdf_options: { pageSize: 'Letter', marginsType: 1 }
69-
}, {}, {}, (errorCode, result) => {
70-
t.equal(errorCode, null)
71-
// height/width are converted to pixels from page-type:
72-
t.same(result.browserSize, { height: 1056, width: 816 })
73-
t.same(result.pdfOptions, { pageSize: 'Letter', marginsType: 1 })
74-
t.end()
103+
104+
t.test('should passthrough pdf_options', t => {
105+
var body = clone(mock)
106+
body.pdf_options = { pageSize: 'Letter', marginsType: 1, crazyOptions: true }
107+
fn(body, {}, {}, (errorCode, result) => {
108+
t.equal(errorCode, null)
109+
t.same(result.pdfOptions, { pageSize: 'Letter', marginsType: 1, crazyOptions: true })
110+
t.end()
111+
})
75112
})
113+
t.end()
76114
})
77115

78116
t.end()

0 commit comments

Comments
 (0)