@@ -2,9 +2,14 @@ const tap = require('tap')
2
2
const sinon = require ( 'sinon' )
3
3
4
4
const _module = require ( '../../src/component/plotly-dash-preview' )
5
+ const constants = require ( '../../src/component/plotly-dash-preview/constants' )
5
6
const remote = require ( '../../src/util/remote' )
6
7
const { createMockWindow } = require ( '../common' )
7
8
9
+ function clone ( obj ) {
10
+ return JSON . parse ( JSON . stringify ( obj ) )
11
+ }
12
+
8
13
tap . test ( 'parse:' , t => {
9
14
const fn = _module . parse
10
15
@@ -30,49 +35,82 @@ tap.test('parse:', t => {
30
35
t . end ( )
31
36
} )
32
37
} )
33
- t . test ( 'should error when pageSize is not given ' , t => {
34
- fn ( {
38
+ t . test ( 'pageSize options: ' , t => {
39
+ const mock = {
35
40
url : 'https://dash-app.com' ,
36
41
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 ( )
56
53
} )
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
+ } )
60
101
} )
61
- t . end ( )
62
102
} )
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
+ } )
75
112
} )
113
+ t . end ( )
76
114
} )
77
115
78
116
t . end ( )
0 commit comments