Skip to content

Commit 2b3a78b

Browse files
committed
Move rgb conversion into assert methods for bar and pie tests [2951]
- Reason: both reading and writing assert statements was more cumbersome before because the conversion of colors into their rgb form had to be done in client code that called the font color assert functions.
1 parent 172e3e9 commit 2b3a78b

File tree

2 files changed

+45
-51
lines changed

2 files changed

+45
-51
lines changed

test/jasmine/tests/bar_test.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,8 @@ describe('Bar.crossTraceCalc (formerly known as setPositions)', function() {
838838
describe('A bar plot', function() {
839839
'use strict';
840840

841-
var DARK = rgb('#444');
842-
var LIGHT = rgb('#fff');
841+
var DARK = '#444';
842+
var LIGHT = '#fff';
843843

844844
var gd;
845845

@@ -908,10 +908,12 @@ describe('A bar plot', function() {
908908
return function() {
909909
var selection = d3.selectAll(BAR_TEXT_SELECTOR);
910910
expect(selection.size()).toBe(expFontColors.length);
911+
911912
selection.each(function(d, i) {
912913
var expFontColor = expFontColors[i];
913914
var isArray = Array.isArray(expFontColor);
914-
expect(this.style.fill).toBe(isArray ? expFontColor[0] : expFontColor,
915+
916+
expect(this.style.fill).toBe(isArray ? rgb(expFontColor[0]) : rgb(expFontColor),
915917
(label || '') + ', fill for element ' + i);
916918
expect(this.style.fillOpacity).toBe(isArray ? expFontColor[1] : '1',
917919
(label || '') + ', fillOpacity for element ' + i);
@@ -1067,7 +1069,7 @@ describe('A bar plot', function() {
10671069
.then(done);
10681070
});
10691071

1070-
var insideTextTestsTraceDef = {
1072+
var insideTextTestsTrace = {
10711073
x: ['giraffes', 'orangutans', 'monkeys', 'elefants', 'spiders', 'snakes'],
10721074
y: [20, 14, 23, 10, 59, 15],
10731075
text: [20, 14, 23, 10, 59, 15],
@@ -1079,7 +1081,7 @@ describe('A bar plot', function() {
10791081
};
10801082

10811083
it('should use inside text colors contrasting to bar colors by default', function(done) {
1082-
Plotly.plot(gd, [insideTextTestsTraceDef])
1084+
Plotly.plot(gd, [insideTextTestsTrace])
10831085
.then(assertTextFontColors([DARK, DARK, LIGHT, LIGHT, DARK, LIGHT]))
10841086
.catch(failTest)
10851087
.then(done);
@@ -1104,37 +1106,37 @@ describe('A bar plot', function() {
11041106
});
11051107

11061108
it('should use defined textfont.color for inside text instead of the contrasting default', function(done) {
1107-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, { textfont: { color: '#09f' } });
1109+
var data = Lib.extendFlat({}, insideTextTestsTrace, { textfont: { color: '#09f' } });
11081110

11091111
Plotly.plot(gd, [data])
1110-
.then(assertTextFontColors(Lib.repeat(rgb('#09f'), 6)))
1112+
.then(assertTextFontColors(Lib.repeat('#09f', 6)))
11111113
.catch(failTest)
11121114
.then(done);
11131115
});
11141116

11151117
it('should use matching color from textfont.color array for inside text, contrasting otherwise', function(done) {
1116-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, { textfont: { color: ['#09f', 'green'] } });
1118+
var data = Lib.extendFlat({}, insideTextTestsTrace, { textfont: { color: ['#09f', 'green'] } });
11171119

11181120
Plotly.plot(gd, [data])
1119-
.then(assertTextFontColors([rgb('#09f'), rgb('green'), LIGHT, LIGHT, DARK, LIGHT]))
1121+
.then(assertTextFontColors(['#09f', 'green', LIGHT, LIGHT, DARK, LIGHT]))
11201122
.catch(failTest)
11211123
.then(done);
11221124
});
11231125

11241126
it('should use defined insidetextfont.color for inside text instead of the contrasting default', function(done) {
1125-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, { insidetextfont: { color: '#09f' } });
1127+
var data = Lib.extendFlat({}, insideTextTestsTrace, { insidetextfont: { color: '#09f' } });
11261128

11271129
Plotly.plot(gd, [data])
1128-
.then(assertTextFontColors(Lib.repeat(rgb('#09f'), 6)))
1130+
.then(assertTextFontColors(Lib.repeat('#09f', 6)))
11291131
.catch(failTest)
11301132
.then(done);
11311133
});
11321134

11331135
it('should use matching color from insidetextfont.color array instead of the contrasting default', function(done) {
1134-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, { insidetextfont: { color: ['yellow', 'green'] } });
1136+
var data = Lib.extendFlat({}, insideTextTestsTrace, { insidetextfont: { color: ['yellow', 'green'] } });
11351137

11361138
Plotly.plot(gd, [data])
1137-
.then(assertTextFontColors([rgb('yellow'), rgb('green'), LIGHT, LIGHT, DARK, LIGHT]))
1139+
.then(assertTextFontColors(['yellow', 'green', LIGHT, LIGHT, DARK, LIGHT]))
11381140
.catch(failTest)
11391141
.then(done);
11401142
});
@@ -1170,7 +1172,7 @@ describe('A bar plot', function() {
11701172
var layout = {barmode: 'stack', font: {family: 'Arial'}};
11711173

11721174
Plotly.plot(gd, [trace1, trace2], layout)
1173-
.then(assertTextFontColors([rgb('blue'), DARK]))
1175+
.then(assertTextFontColors(['blue', DARK]))
11741176
.then(assertTextFontFamilies(['serif', 'Arial']))
11751177
.then(assertTextFontSizes([24, 12]))
11761178
.catch(failTest)
@@ -1179,7 +1181,7 @@ describe('A bar plot', function() {
11791181

11801182
it('should fall back to textfont array values if insidetextfont array values don\'t ' +
11811183
'cover all bars', function(done) {
1182-
var trace = Lib.extendFlat({}, insideTextTestsTraceDef, {
1184+
var trace = Lib.extendFlat({}, insideTextTestsTrace, {
11831185
textfont: {
11841186
color: ['blue', 'blue', 'blue'],
11851187
family: ['Arial', 'serif'],
@@ -1194,7 +1196,7 @@ describe('A bar plot', function() {
11941196
var layout = {font: {family: 'Roboto', size: 12}};
11951197

11961198
Plotly.plot(gd, [trace], layout)
1197-
.then(assertTextFontColors([rgb('yellow'), rgb('green'), rgb('blue'), LIGHT, DARK, LIGHT]))
1199+
.then(assertTextFontColors(['yellow', 'green', 'blue', LIGHT, DARK, LIGHT]))
11981200
.then(assertTextFontFamilies(['Arial', 'serif', 'Roboto', 'Roboto', 'Roboto', 'Roboto']))
11991201
.then(assertTextFontSizes([16, 24, 12, 12, 12, 12]))
12001202
.catch(failTest)
@@ -1248,19 +1250,15 @@ describe('A bar plot', function() {
12481250
.catch(failTest)
12491251
.then(done);
12501252

1251-
var blue = rgb('blue');
1252-
var orange = rgb('orange');
1253-
var red = rgb('red');
1254-
12551253
function assertSelectionModeStyle(label) {
1256-
var unselColor = [rgb('black'), '0.2'];
1257-
assertTextFontColors([unselColor, unselColor, unselColor, red, unselColor, unselColor], label)();
1254+
var unselColor = ['black', '0.2'];
1255+
assertTextFontColors([unselColor, unselColor, unselColor, 'red', unselColor, unselColor], label)();
12581256
assertTextFontFamilies(['Arial', 'serif', 'Roboto', 'Arial', 'serif', 'Roboto'])();
12591257
assertTextFontSizes([16, 24, 12, 8, 24, 12])();
12601258
}
12611259

12621260
function assertNonSelectionModeStyle(label) {
1263-
assertTextFontColors([blue, orange, LIGHT, red, orange, DARK], label)();
1261+
assertTextFontColors(['blue', 'orange', LIGHT, 'red', 'orange', DARK], label)();
12641262
assertTextFontFamilies(['Arial', 'serif', 'Roboto', 'Arial', 'serif', 'Roboto'])();
12651263
assertTextFontSizes([16, 24, 12, 8, 24, 12])();
12661264
}

test/jasmine/tests/pie_test.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ var click = require('../assets/click');
99
var getClientPosition = require('../assets/get_client_position');
1010
var mouseEvent = require('../assets/mouse_event');
1111
var supplyAllDefaults = require('../assets/supply_defaults');
12-
var color = require('../../../src/components/color');
13-
var rgb = color.rgb;
12+
var rgb = require('../../../src/components/color').rgb;
1413

1514
var customAssertions = require('../assets/custom_assertions');
1615
var assertHoverLabelStyle = customAssertions.assertHoverLabelStyle;
@@ -77,8 +76,8 @@ describe('Pie defaults', function() {
7776
describe('Pie traces', function() {
7877
'use strict';
7978

80-
var DARK = color.rgb('#444');
81-
var LIGHT = color.rgb('#fff');
79+
var DARK = '#444';
80+
var LIGHT = '#fff';
8281

8382
var gd;
8483

@@ -168,7 +167,7 @@ describe('Pie traces', function() {
168167
function _checkFontColors(expFontColors) {
169168
return function() {
170169
d3.selectAll(SLICES_TEXT_SELECTOR).each(function(d, i) {
171-
expect(this.style.fill).toBe(expFontColors[i], 'fill color of ' + i);
170+
expect(this.style.fill).toBe(rgb(expFontColors[i]), 'fill color of ' + i);
172171
});
173172
};
174173
}
@@ -527,15 +526,15 @@ describe('Pie traces', function() {
527526
};
528527

529528
Plotly.plot(gd, [data])
530-
.then(_checkFontColors([rgb('red'), rgb('green'), rgb('blue')]))
529+
.then(_checkFontColors(['red', 'green', 'blue']))
531530
.then(_checkFontFamilies(['Arial', 'Gravitas', 'Roboto']))
532531
.then(_checkFontSizes([12, 20, 16]))
533532
.catch(failTest)
534533
.then(done);
535534
});
536535
});
537536

538-
var insideTextTestsTraceDef = {
537+
var insideTextTestsTrace = {
539538
values: [6, 5, 4, 3, 2, 1],
540539
type: 'pie',
541540
marker: {
@@ -544,39 +543,39 @@ describe('Pie traces', function() {
544543
};
545544

546545
it('should use inside text colors contrasting to slice colors by default', function(done) {
547-
Plotly.plot(gd, [insideTextTestsTraceDef])
546+
Plotly.plot(gd, [insideTextTestsTrace])
548547
.then(_checkFontColors([DARK, DARK, LIGHT, LIGHT, DARK, LIGHT]))
549548
.catch(failTest)
550549
.then(done);
551550
});
552551

553552
it('should use textfont.color for inside text instead of the contrasting default', function(done) {
554-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, {textfont: {color: 'red'}});
553+
var data = Lib.extendFlat({}, insideTextTestsTrace, {textfont: {color: 'red'}});
555554
Plotly.plot(gd, [data])
556-
.then(_checkFontColors(Lib.repeat(rgb('red'), 6)))
555+
.then(_checkFontColors(Lib.repeat('red', 6)))
557556
.catch(failTest)
558557
.then(done);
559558
});
560559

561560
it('should use matching color from textfont.color array for inside text, contrasting otherwise', function(done) {
562-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, {textfont: {color: ['red', 'blue']}});
561+
var data = Lib.extendFlat({}, insideTextTestsTrace, {textfont: {color: ['red', 'blue']}});
563562
Plotly.plot(gd, [data])
564-
.then(_checkFontColors([rgb('red'), rgb('blue'), LIGHT, LIGHT, DARK, LIGHT]))
563+
.then(_checkFontColors(['red', 'blue', LIGHT, LIGHT, DARK, LIGHT]))
565564
.catch(failTest)
566565
.then(done);
567566
});
568567

569568
it('should not use layout.font.color for inside text, but a contrasting color instead', function(done) {
570-
Plotly.plot(gd, [insideTextTestsTraceDef], {font: {color: 'green'}})
569+
Plotly.plot(gd, [insideTextTestsTrace], {font: {color: 'green'}})
571570
.then(_checkFontColors([DARK, DARK, LIGHT, LIGHT, DARK, LIGHT]))
572571
.catch(failTest)
573572
.then(done);
574573
});
575574

576575
it('should use matching color from insidetextfont.color array instead of the contrasting default', function(done) {
577-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, {textfont: {color: ['orange', 'purple']}});
576+
var data = Lib.extendFlat({}, insideTextTestsTrace, {textfont: {color: ['orange', 'purple']}});
578577
Plotly.plot(gd, [data])
579-
.then(_checkFontColors([rgb('orange'), rgb('purple'), LIGHT, LIGHT, DARK, LIGHT]))
578+
.then(_checkFontColors(['orange', 'purple', LIGHT, LIGHT, DARK, LIGHT]))
580579
.catch(failTest)
581580
.then(done);
582581
});
@@ -587,15 +586,14 @@ describe('Pie traces', function() {
587586
].forEach(function(spec) {
588587
it('should fall back to textfont scalar values if ' + spec.fontAttr + ' value ' +
589588
'arrays don\'t cover all slices', function(done) {
590-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, {
589+
var data = Lib.extendFlat({}, insideTextTestsTrace, {
591590
textposition: spec.textposition,
592591
textfont: {color: 'orange', family: 'Gravitas', size: 12}
593592
});
594593
data[spec.fontAttr] = {color: ['blue', 'yellow'], family: ['Arial', 'Arial'], size: [24, 34]};
595594

596-
var orange = rgb('orange');
597595
Plotly.plot(gd, [data])
598-
.then(_checkFontColors([rgb('blue'), rgb('yellow'), orange, orange, orange, orange]))
596+
.then(_checkFontColors(['blue', 'yellow', 'orange', 'orange', 'orange', 'orange']))
599597
.then(_checkFontFamilies(['Arial', 'Arial', 'Gravitas', 'Gravitas', 'Gravitas', 'Gravitas']))
600598
.then(_checkFontSizes([24, 34, 12, 12, 12, 12]))
601599
.catch(failTest)
@@ -606,7 +604,7 @@ describe('Pie traces', function() {
606604
it('should fall back to textfont array values and layout.font scalar (except color)' +
607605
' values for inside text', function(done) {
608606
var layout = {font: {color: 'orange', family: 'serif', size: 16}};
609-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, {
607+
var data = Lib.extendFlat({}, insideTextTestsTrace, {
610608
textfont: {
611609
color: ['blue', 'blue'], family: ['Arial', 'Arial'], size: [18, 18]
612610
},
@@ -616,7 +614,7 @@ describe('Pie traces', function() {
616614
});
617615

618616
Plotly.plot(gd, [data], layout)
619-
.then(_checkFontColors([rgb('purple'), rgb('blue'), LIGHT, LIGHT, DARK, LIGHT]))
617+
.then(_checkFontColors(['purple', 'blue', LIGHT, LIGHT, DARK, LIGHT]))
620618
.then(_checkFontFamilies(['Roboto', 'Arial', 'serif', 'serif', 'serif', 'serif']))
621619
.then(_checkFontSizes([24, 18, 16, 16, 16, 16]))
622620
.catch(failTest)
@@ -626,7 +624,7 @@ describe('Pie traces', function() {
626624
it('should fall back to textfont array values and layout.font scalar' +
627625
' values for outside text', function(done) {
628626
var layout = {font: {color: 'orange', family: 'serif', size: 16}};
629-
var data = Lib.extendFlat({}, insideTextTestsTraceDef, {
627+
var data = Lib.extendFlat({}, insideTextTestsTrace, {
630628
textposition: 'outside',
631629
textfont: {
632630
color: ['blue', 'blue'], family: ['Arial', 'Arial'], size: [18, 18]
@@ -636,9 +634,8 @@ describe('Pie traces', function() {
636634
}
637635
});
638636

639-
var orange = rgb('orange');
640637
Plotly.plot(gd, [data], layout)
641-
.then(_checkFontColors([rgb('purple'), rgb('blue'), orange, orange, orange, orange]))
638+
.then(_checkFontColors(['purple', 'blue', 'orange', 'orange', 'orange', 'orange']))
642639
.then(_checkFontFamilies(['Roboto', 'Arial', 'serif', 'serif', 'serif', 'serif']))
643640
.then(_checkFontSizes([24, 18, 16, 16, 16, 16]))
644641
.catch(failTest)
@@ -652,12 +649,12 @@ describe('Pie traces', function() {
652649
it('should fall back to layout.font scalar values for inside text (except color) if ' + spec.fontAttr + ' value ' +
653650
'arrays don\'t cover all slices', function(done) {
654651
var layout = {font: {color: 'orange', family: 'serif', size: 16}};
655-
var data = Lib.extendFlat({}, insideTextTestsTraceDef);
652+
var data = Lib.extendFlat({}, insideTextTestsTrace);
656653
data.textposition = 'inside';
657654
data[spec.fontAttr] = {color: ['blue', 'yellow'], family: ['Arial', 'Arial'], size: [24, 34]};
658655

659656
Plotly.plot(gd, [data], layout)
660-
.then(_checkFontColors([rgb('blue'), rgb('yellow'), LIGHT, LIGHT, DARK, LIGHT]))
657+
.then(_checkFontColors(['blue', 'yellow', LIGHT, LIGHT, DARK, LIGHT]))
661658
.then(_checkFontFamilies(['Arial', 'Arial', 'serif', 'serif', 'serif', 'serif']))
662659
.then(_checkFontSizes([24, 34, 16, 16, 16, 16]))
663660
.catch(failTest)
@@ -672,13 +669,12 @@ describe('Pie traces', function() {
672669
it('should fall back to layout.font scalar values for outside text if ' + spec.fontAttr + ' value ' +
673670
'arrays don\'t cover all slices', function(done) {
674671
var layout = {font: {color: 'orange', family: 'serif', size: 16}};
675-
var data = Lib.extendFlat({}, insideTextTestsTraceDef);
672+
var data = Lib.extendFlat({}, insideTextTestsTrace);
676673
data.textposition = 'outside';
677674
data[spec.fontAttr] = {color: ['blue', 'yellow'], family: ['Arial', 'Arial'], size: [24, 34]};
678675

679-
var orange = rgb('orange');
680676
Plotly.plot(gd, [data], layout)
681-
.then(_checkFontColors([rgb('blue'), rgb('yellow'), orange, orange, orange, orange]))
677+
.then(_checkFontColors(['blue', 'yellow', 'orange', 'orange', 'orange', 'orange']))
682678
.then(_checkFontFamilies(['Arial', 'Arial', 'serif', 'serif', 'serif', 'serif']))
683679
.then(_checkFontSizes([24, 34, 16, 16, 16, 16]))
684680
.catch(failTest)

0 commit comments

Comments
 (0)