1
1
describe ( 'Chart.Controller' , function ( ) {
2
2
3
- function processResizeEvent ( chart , callback ) {
4
- setTimeout ( callback , 100 ) ;
3
+ function waitForResize ( chart , callback ) {
4
+ var resizer = chart . chart . canvas . parentNode . _chartjs . resizer ;
5
+ var content = resizer . contentWindow || resizer ;
6
+ var state = content . document . readyState || 'complete' ;
7
+ var handler = function ( ) {
8
+ Chart . helpers . removeEvent ( content , 'load' , handler ) ;
9
+ Chart . helpers . removeEvent ( content , 'resize' , handler ) ;
10
+ setTimeout ( callback , 50 ) ;
11
+ } ;
12
+
13
+ Chart . helpers . addEvent ( content , state !== 'complete' ? 'load' : 'resize' , handler ) ;
5
14
}
6
15
7
16
describe ( 'context acquisition' , function ( ) {
@@ -358,14 +367,14 @@ describe('Chart.Controller', function() {
358
367
359
368
var wrapper = chart . chart . canvas . parentNode ;
360
369
wrapper . style . width = '455px' ;
361
- processResizeEvent ( chart , function ( ) {
370
+ waitForResize ( chart , function ( ) {
362
371
expect ( chart ) . toBeChartOfSize ( {
363
372
dw : 455 , dh : 350 ,
364
373
rw : 455 , rh : 350 ,
365
374
} ) ;
366
375
367
376
wrapper . style . width = '150px' ;
368
- processResizeEvent ( chart , function ( ) {
377
+ waitForResize ( chart , function ( ) {
369
378
expect ( chart ) . toBeChartOfSize ( {
370
379
dw : 150 , dh : 350 ,
371
380
rw : 150 , rh : 350 ,
@@ -398,14 +407,14 @@ describe('Chart.Controller', function() {
398
407
399
408
var wrapper = chart . chart . canvas . parentNode ;
400
409
wrapper . style . height = '455px' ;
401
- processResizeEvent ( chart , function ( ) {
410
+ waitForResize ( chart , function ( ) {
402
411
expect ( chart ) . toBeChartOfSize ( {
403
412
dw : 300 , dh : 455 ,
404
413
rw : 300 , rh : 455 ,
405
414
} ) ;
406
415
407
416
wrapper . style . height = '150px' ;
408
- processResizeEvent ( chart , function ( ) {
417
+ waitForResize ( chart , function ( ) {
409
418
expect ( chart ) . toBeChartOfSize ( {
410
419
dw : 300 , dh : 150 ,
411
420
rw : 300 , rh : 150 ,
@@ -440,7 +449,7 @@ describe('Chart.Controller', function() {
440
449
var wrapper = chart . chart . canvas . parentNode ;
441
450
wrapper . style . height = '355px' ;
442
451
wrapper . style . width = '455px' ;
443
- processResizeEvent ( chart , function ( ) {
452
+ waitForResize ( chart , function ( ) {
444
453
expect ( chart ) . toBeChartOfSize ( {
445
454
dw : 455 , dh : 355 ,
446
455
rw : 455 , rh : 355 ,
@@ -467,7 +476,7 @@ describe('Chart.Controller', function() {
467
476
468
477
var canvas = chart . chart . canvas ;
469
478
canvas . style . display = 'block' ;
470
- processResizeEvent ( chart , function ( ) {
479
+ waitForResize ( chart , function ( ) {
471
480
expect ( chart ) . toBeChartOfSize ( {
472
481
dw : 320 , dh : 350 ,
473
482
rw : 320 , rh : 350 ,
@@ -494,7 +503,7 @@ describe('Chart.Controller', function() {
494
503
495
504
var wrapper = chart . chart . canvas . parentNode ;
496
505
wrapper . style . display = 'block' ;
497
- processResizeEvent ( chart , function ( ) {
506
+ waitForResize ( chart , function ( ) {
498
507
expect ( chart ) . toBeChartOfSize ( {
499
508
dw : 460 , dh : 380 ,
500
509
rw : 460 , rh : 380 ,
@@ -503,6 +512,54 @@ describe('Chart.Controller', function() {
503
512
done ( ) ;
504
513
} ) ;
505
514
} ) ;
515
+
516
+ // https://github.com/chartjs/Chart.js/issues/3521
517
+ it ( 'should resize the canvas after the wrapper has been re-attached to the DOM' , function ( done ) {
518
+ var chart = acquireChart ( {
519
+ options : {
520
+ responsive : true ,
521
+ maintainAspectRatio : false
522
+ }
523
+ } , {
524
+ canvas : {
525
+ style : ''
526
+ } ,
527
+ wrapper : {
528
+ style : 'width: 320px; height: 350px'
529
+ }
530
+ } ) ;
531
+
532
+ expect ( chart ) . toBeChartOfSize ( {
533
+ dw : 320 , dh : 350 ,
534
+ rw : 320 , rh : 350 ,
535
+ } ) ;
536
+
537
+ var wrapper = chart . chart . canvas . parentNode ;
538
+ var parent = wrapper . parentNode ;
539
+ parent . removeChild ( wrapper ) ;
540
+ parent . appendChild ( wrapper ) ;
541
+ wrapper . style . height = '355px' ;
542
+
543
+ waitForResize ( chart , function ( ) {
544
+ expect ( chart ) . toBeChartOfSize ( {
545
+ dw : 320 , dh : 355 ,
546
+ rw : 320 , rh : 355 ,
547
+ } ) ;
548
+
549
+ parent . removeChild ( wrapper ) ;
550
+ wrapper . style . width = '455px' ;
551
+ parent . appendChild ( wrapper ) ;
552
+
553
+ waitForResize ( chart , function ( ) {
554
+ expect ( chart ) . toBeChartOfSize ( {
555
+ dw : 455 , dh : 355 ,
556
+ rw : 455 , rh : 355 ,
557
+ } ) ;
558
+
559
+ done ( ) ;
560
+ } ) ;
561
+ } ) ;
562
+ } ) ;
506
563
} ) ;
507
564
508
565
describe ( 'config.options.responsive: true (maintainAspectRatio: true)' , function ( ) {
@@ -550,14 +607,14 @@ describe('Chart.Controller', function() {
550
607
551
608
var wrapper = chart . chart . canvas . parentNode ;
552
609
wrapper . style . width = '450px' ;
553
- processResizeEvent ( chart , function ( ) {
610
+ waitForResize ( chart , function ( ) {
554
611
expect ( chart ) . toBeChartOfSize ( {
555
612
dw : 450 , dh : 225 ,
556
613
rw : 450 , rh : 225 ,
557
614
} ) ;
558
615
559
616
wrapper . style . width = '150px' ;
560
- processResizeEvent ( chart , function ( ) {
617
+ waitForResize ( chart , function ( ) {
561
618
expect ( chart ) . toBeChartOfSize ( {
562
619
dw : 150 , dh : 75 ,
563
620
rw : 150 , rh : 75 ,
@@ -590,14 +647,14 @@ describe('Chart.Controller', function() {
590
647
591
648
var wrapper = chart . chart . canvas . parentNode ;
592
649
wrapper . style . height = '455px' ;
593
- processResizeEvent ( chart , function ( ) {
650
+ waitForResize ( chart , function ( ) {
594
651
expect ( chart ) . toBeChartOfSize ( {
595
652
dw : 320 , dh : 160 ,
596
653
rw : 320 , rh : 160 ,
597
654
} ) ;
598
655
599
656
wrapper . style . height = '150px' ;
600
- processResizeEvent ( chart , function ( ) {
657
+ waitForResize ( chart , function ( ) {
601
658
expect ( chart ) . toBeChartOfSize ( {
602
659
dw : 320 , dh : 160 ,
603
660
rw : 320 , rh : 160 ,
@@ -629,7 +686,7 @@ describe('Chart.Controller', function() {
629
686
var canvas = chart . chart . canvas ;
630
687
var wrapper = canvas . parentNode ;
631
688
wrapper . style . width = '475px' ;
632
- processResizeEvent ( chart , function ( ) {
689
+ waitForResize ( chart , function ( ) {
633
690
expect ( chart ) . toBeChartOfSize ( {
634
691
dw : 475 , dh : 450 ,
635
692
rw : 475 , rh : 450 ,
0 commit comments