@@ -8,6 +8,33 @@ var createGraphDiv = require('../assets/create_graph_div');
8
8
var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
9
9
var Plots = PlotlyInternal . Plots ;
10
10
11
+ /* This is a one-off function to fully populate sparse arrays. This arises
12
+ * because:
13
+ *
14
+ * var x = new Array(2)
15
+ * expect(x).toEqual([undefined, undefined])
16
+ *
17
+ * will fail assertion even though x[0] === undefined and x[1] === undefined.
18
+ * This is because the array elements don't exist until assigned.
19
+ */
20
+ function populateUndefinedArrayEls ( x ) {
21
+ var i ;
22
+ if ( Array . isArray ( x ) ) {
23
+ for ( i = 0 ; i < x . length ; i ++ ) {
24
+ x [ i ] = x [ i ] ;
25
+ }
26
+ } else if ( Lib . isPlainObject ( x ) ) {
27
+ var keys = Object . keys ( x ) ;
28
+ for ( i = 0 ; i < keys . length ; i ++ ) {
29
+ populateUndefinedArrayEls ( x [ keys [ i ] ] ) ;
30
+ }
31
+ }
32
+ }
33
+
34
+ function expectLooseDeepEqual ( a , b ) {
35
+ expect ( populateUndefinedArrayEls ( a ) ) . toEqual ( populateUndefinedArrayEls ( b ) ) ;
36
+ }
37
+
11
38
12
39
describe ( 'Test lib.js:' , function ( ) {
13
40
'use strict' ;
@@ -485,37 +512,37 @@ describe('Test lib.js:', function() {
485
512
it ( 'unpacks top-level paths' , function ( ) {
486
513
var input = { 'marker.color' : 'red' , 'marker.size' : [ 1 , 2 , 3 ] } ;
487
514
var expected = { marker : { color : 'red' , size : [ 1 , 2 , 3 ] } } ;
488
- expect ( Lib . expandObjectPaths ( input ) ) . toEqual ( expected ) ;
515
+ expectLooseDeepEqual ( Lib . expandObjectPaths ( input ) , expected ) ;
489
516
} ) ;
490
517
491
518
it ( 'unpacks recursively' , function ( ) {
492
519
var input = { 'marker.color' : { 'red.certainty' : 'definitely' } } ;
493
520
var expected = { marker : { color : { red : { certainty : 'definitely' } } } } ;
494
- expect ( Lib . expandObjectPaths ( input ) ) . toEqual ( expected ) ;
521
+ expectLooseDeepEqual ( Lib . expandObjectPaths ( input ) , expected ) ;
495
522
} ) ;
496
523
497
524
it ( 'unpacks deep paths' , function ( ) {
498
525
var input = { 'foo.bar.baz' : 'red' } ;
499
526
var expected = { foo : { bar : { baz : 'red' } } } ;
500
- expect ( Lib . expandObjectPaths ( input ) ) . toEqual ( expected ) ;
527
+ expectLooseDeepEqual ( Lib . expandObjectPaths ( input ) , expected ) ;
501
528
} ) ;
502
529
503
530
it ( 'unpacks non-top-level deep paths' , function ( ) {
504
531
var input = { color : { 'foo.bar.baz' : 'red' } } ;
505
532
var expected = { color : { foo : { bar : { baz : 'red' } } } } ;
506
- expect ( Lib . expandObjectPaths ( input ) ) . toEqual ( expected ) ;
533
+ expectLooseDeepEqual ( Lib . expandObjectPaths ( input ) , expected ) ;
507
534
} ) ;
508
535
509
536
it ( 'merges dotted properties into objects' , function ( ) {
510
537
var input = { marker : { color : 'red' } , 'marker.size' : 8 } ;
511
538
var expected = { marker : { color : 'red' , size : 8 } } ;
512
- expect ( Lib . expandObjectPaths ( input ) ) . toEqual ( expected ) ;
539
+ expectLooseDeepEqual ( Lib . expandObjectPaths ( input ) , expected ) ;
513
540
} ) ;
514
541
515
542
it ( 'merges objects into dotted properties' , function ( ) {
516
543
var input = { 'marker.size' : 8 , marker : { color : 'red' } } ;
517
544
var expected = { marker : { color : 'red' , size : 8 } } ;
518
- expect ( Lib . expandObjectPaths ( input ) ) . toEqual ( expected ) ;
545
+ expectLooseDeepEqual ( Lib . expandObjectPaths ( input ) , expected ) ;
519
546
} ) ;
520
547
521
548
it ( 'retains the identity of nested objects' , function ( ) {
@@ -541,49 +568,49 @@ describe('Test lib.js:', function() {
541
568
it ( 'expands bracketed array notation' , function ( ) {
542
569
var input = { 'marker[1]' : { color : 'red' } } ;
543
570
var expected = { marker : [ undefined , { color : 'red' } ] } ;
544
- expect ( Lib . expandObjectPaths ( input ) ) . toEqual ( expected ) ;
571
+ expectLooseDeepEqual ( Lib . expandObjectPaths ( input ) , expected ) ;
545
572
} ) ;
546
573
547
574
it ( 'expands nested arrays' , function ( ) {
548
575
var input = { 'marker[1].range[1]' : 5 } ;
549
576
var expected = { marker : [ undefined , { range : [ undefined , 5 ] } ] } ;
550
577
var computed = Lib . expandObjectPaths ( input ) ;
551
- expect ( computed ) . toEqual ( expected ) ;
578
+ expectLooseDeepEqual ( computed , expected ) ;
552
579
} ) ;
553
580
554
581
it ( 'expands bracketed array with more nested attributes' , function ( ) {
555
582
var input = { 'marker[1]' : { 'color.alpha' : 2 } } ;
556
583
var expected = { marker : [ undefined , { color : { alpha : 2 } } ] } ;
557
584
var computed = Lib . expandObjectPaths ( input ) ;
558
- expect ( computed ) . toEqual ( expected ) ;
585
+ expectLooseDeepEqual ( computed , expected ) ;
559
586
} ) ;
560
587
561
588
it ( 'expands bracketed array notation without further nesting' , function ( ) {
562
589
var input = { 'marker[1]' : 8 } ;
563
590
var expected = { marker : [ undefined , 8 ] } ;
564
591
var computed = Lib . expandObjectPaths ( input ) ;
565
- expect ( computed ) . toEqual ( expected ) ;
592
+ expectLooseDeepEqual ( computed , expected ) ;
566
593
} ) ;
567
594
568
595
it ( 'expands bracketed array notation with further nesting' , function ( ) {
569
596
var input = { 'marker[1].size' : 8 } ;
570
597
var expected = { marker : [ undefined , { size : 8 } ] } ;
571
598
var computed = Lib . expandObjectPaths ( input ) ;
572
- expect ( computed ) . toEqual ( expected ) ;
599
+ expectLooseDeepEqual ( computed , expected ) ;
573
600
} ) ;
574
601
575
602
it ( 'expands bracketed array notation with further nesting' , function ( ) {
576
603
var input = { 'marker[1].size.magnitude' : 8 } ;
577
604
var expected = { marker : [ undefined , { size : { magnitude : 8 } } ] } ;
578
605
var computed = Lib . expandObjectPaths ( input ) ;
579
- expect ( computed ) . toEqual ( expected ) ;
606
+ expectLooseDeepEqual ( computed , expected ) ;
580
607
} ) ;
581
608
582
609
it ( 'combines changes with single array nesting' , function ( ) {
583
610
var input = { 'marker[1].foo' : 5 , 'marker[0].foo' : 4 } ;
584
611
var expected = { marker : [ { foo : 4 } , { foo : 5 } ] } ;
585
612
var computed = Lib . expandObjectPaths ( input ) ;
586
- expect ( computed ) . toEqual ( expected ) ;
613
+ expectLooseDeepEqual ( computed , expected ) ;
587
614
} ) ;
588
615
589
616
// TODO: This test is unimplemented since it's a currently-unused corner case.
0 commit comments