Skip to content

Commit 94847b1

Browse files
committed
Fix the fact that expect(new Array(1)).toEqual([undefined]) fails
1 parent c4efa80 commit 94847b1

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

test/jasmine/tests/lib_test.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@ var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
99
var Plots = PlotlyInternal.Plots;
1010

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+
1138

1239
describe('Test lib.js:', function() {
1340
'use strict';
@@ -485,37 +512,37 @@ describe('Test lib.js:', function() {
485512
it('unpacks top-level paths', function() {
486513
var input = {'marker.color': 'red', 'marker.size': [1, 2, 3]};
487514
var expected = {marker: {color: 'red', size: [1, 2, 3]}};
488-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
515+
expectLooseDeepEqual(Lib.expandObjectPaths(input), expected);
489516
});
490517

491518
it('unpacks recursively', function() {
492519
var input = {'marker.color': {'red.certainty': 'definitely'}};
493520
var expected = {marker: {color: {red: {certainty: 'definitely'}}}};
494-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
521+
expectLooseDeepEqual(Lib.expandObjectPaths(input), expected);
495522
});
496523

497524
it('unpacks deep paths', function() {
498525
var input = {'foo.bar.baz': 'red'};
499526
var expected = {foo: {bar: {baz: 'red'}}};
500-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
527+
expectLooseDeepEqual(Lib.expandObjectPaths(input), expected);
501528
});
502529

503530
it('unpacks non-top-level deep paths', function() {
504531
var input = {color: {'foo.bar.baz': 'red'}};
505532
var expected = {color: {foo: {bar: {baz: 'red'}}}};
506-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
533+
expectLooseDeepEqual(Lib.expandObjectPaths(input), expected);
507534
});
508535

509536
it('merges dotted properties into objects', function() {
510537
var input = {marker: {color: 'red'}, 'marker.size': 8};
511538
var expected = {marker: {color: 'red', size: 8}};
512-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
539+
expectLooseDeepEqual(Lib.expandObjectPaths(input), expected);
513540
});
514541

515542
it('merges objects into dotted properties', function() {
516543
var input = {'marker.size': 8, marker: {color: 'red'}};
517544
var expected = {marker: {color: 'red', size: 8}};
518-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
545+
expectLooseDeepEqual(Lib.expandObjectPaths(input), expected);
519546
});
520547

521548
it('retains the identity of nested objects', function() {
@@ -541,49 +568,49 @@ describe('Test lib.js:', function() {
541568
it('expands bracketed array notation', function() {
542569
var input = {'marker[1]': {color: 'red'}};
543570
var expected = {marker: [undefined, {color: 'red'}]};
544-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
571+
expectLooseDeepEqual(Lib.expandObjectPaths(input), expected);
545572
});
546573

547574
it('expands nested arrays', function() {
548575
var input = {'marker[1].range[1]': 5};
549576
var expected = {marker: [undefined, {range: [undefined, 5]}]};
550577
var computed = Lib.expandObjectPaths(input);
551-
expect(computed).toEqual(expected);
578+
expectLooseDeepEqual(computed, expected);
552579
});
553580

554581
it('expands bracketed array with more nested attributes', function() {
555582
var input = {'marker[1]': {'color.alpha': 2}};
556583
var expected = {marker: [undefined, {color: {alpha: 2}}]};
557584
var computed = Lib.expandObjectPaths(input);
558-
expect(computed).toEqual(expected);
585+
expectLooseDeepEqual(computed, expected);
559586
});
560587

561588
it('expands bracketed array notation without further nesting', function() {
562589
var input = {'marker[1]': 8};
563590
var expected = {marker: [undefined, 8]};
564591
var computed = Lib.expandObjectPaths(input);
565-
expect(computed).toEqual(expected);
592+
expectLooseDeepEqual(computed, expected);
566593
});
567594

568595
it('expands bracketed array notation with further nesting', function() {
569596
var input = {'marker[1].size': 8};
570597
var expected = {marker: [undefined, {size: 8}]};
571598
var computed = Lib.expandObjectPaths(input);
572-
expect(computed).toEqual(expected);
599+
expectLooseDeepEqual(computed, expected);
573600
});
574601

575602
it('expands bracketed array notation with further nesting', function() {
576603
var input = {'marker[1].size.magnitude': 8};
577604
var expected = {marker: [undefined, {size: {magnitude: 8}}]};
578605
var computed = Lib.expandObjectPaths(input);
579-
expect(computed).toEqual(expected);
606+
expectLooseDeepEqual(computed, expected);
580607
});
581608

582609
it('combines changes with single array nesting', function() {
583610
var input = {'marker[1].foo': 5, 'marker[0].foo': 4};
584611
var expected = {marker: [{foo: 4}, {foo: 5}]};
585612
var computed = Lib.expandObjectPaths(input);
586-
expect(computed).toEqual(expected);
613+
expectLooseDeepEqual(computed, expected);
587614
});
588615

589616
// TODO: This test is unimplemented since it's a currently-unused corner case.

0 commit comments

Comments
 (0)