@@ -14,6 +14,12 @@ describe('serializeCanvas', () => {
1414 height="150px"
1515 style="border: 5px solid black;"
1616 ></canvas>
17+ <canvas
18+ id="canvas-with-maxwidth"
19+ width="200px"
20+ height="100px"
21+ style="border: 2px solid red; max-width: 150px;"
22+ ></canvas>
1723 <canvas
1824 id="empty"
1925 width="0px"
@@ -37,6 +43,14 @@ describe('serializeCanvas', () => {
3743 ctx . stroke ( ) ;
3844
3945 cache [ plat ] . dataURL = canvas . toDataURL ( ) ;
46+
47+ // Draw on canvas with maxWidth
48+ let canvasWithMaxWidth = dom . getElementById ( 'canvas-with-maxwidth' ) ;
49+ let ctxWithMaxWidth = canvasWithMaxWidth . getContext ( '2d' ) ;
50+ ctxWithMaxWidth . fillStyle = 'blue' ;
51+ ctxWithMaxWidth . fillRect ( 10 , 10 , 50 , 30 ) ;
52+
53+ cache [ plat ] . dataURLWithMaxWidth = canvasWithMaxWidth . toDataURL ( ) ;
4054 } ) ;
4155
4256 serialized = serializeDOM ( ) ;
@@ -54,7 +68,7 @@ describe('serializeCanvas', () => {
5468 expect ( $canvas [ 0 ] . getAttribute ( 'width' ) ) . toBe ( '150px' ) ;
5569 expect ( $canvas [ 0 ] . getAttribute ( 'height' ) ) . toBe ( '150px' ) ;
5670 expect ( $canvas [ 0 ] . getAttribute ( 'src' ) ) . toMatch ( '/__serialized__/\\w+\\.png' ) ;
57- expect ( $canvas [ 0 ] . getAttribute ( 'style' ) ) . toBe ( 'border: 5px solid black; max-width: 100%; ' ) ;
71+ expect ( $canvas [ 0 ] . getAttribute ( 'style' ) ) . toBe ( 'border: 5px solid black;' ) ;
5872 expect ( $canvas [ 0 ] . matches ( '[data-percy-canvas-serialized]' ) ) . toBe ( true ) ;
5973
6074 expect ( serialized . resources ) . toContain ( jasmine . objectContaining ( {
@@ -64,6 +78,22 @@ describe('serializeCanvas', () => {
6478 } ) ) ;
6579 } ) ;
6680
81+ it ( `${ platform } : preserves maxWidth from canvas to image element` , ( ) => {
82+ let $canvas = $ ( '#canvas-with-maxwidth' ) ;
83+ expect ( $canvas [ 0 ] . tagName ) . toBe ( 'IMG' ) ;
84+ expect ( $canvas [ 0 ] . getAttribute ( 'width' ) ) . toBe ( '200px' ) ;
85+ expect ( $canvas [ 0 ] . getAttribute ( 'height' ) ) . toBe ( '100px' ) ;
86+ expect ( $canvas [ 0 ] . getAttribute ( 'style' ) ) . toBe ( 'border: 2px solid red; max-width: 150px;' ) ;
87+ expect ( $canvas [ 0 ] . style . maxWidth ) . toBe ( '150px' ) ;
88+ expect ( $canvas [ 0 ] . matches ( '[data-percy-canvas-serialized]' ) ) . toBe ( true ) ;
89+
90+ expect ( serialized . resources ) . toContain ( jasmine . objectContaining ( {
91+ url : $canvas [ 0 ] . getAttribute ( 'src' ) ,
92+ content : cache [ platform ] . dataURLWithMaxWidth . split ( ',' ) [ 1 ] ,
93+ mimetype : 'image/png'
94+ } ) ) ;
95+ } ) ;
96+
6797 it ( `${ platform } : does not serialize canvas elements when JS is enabled` , ( ) => {
6898 serialized = serializeDOM ( { enableJavaScript : true } ) ;
6999 $ = parseDOM ( serialized . html , platform ) ;
0 commit comments