Skip to content

Commit b477955

Browse files
committed
swap expect().negateIf(c) -> negateIf(c, expect())
N.B. jasmine-core v3 does not allow mutation the Expectation prototyp
1 parent 9ec0602 commit b477955

File tree

9 files changed

+44
-32
lines changed

9 files changed

+44
-32
lines changed

test/jasmine/assets/custom_assertions.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var d3 = require('d3');
4+
var negateIf = require('./negate_if');
45

56
exports.assertDims = function(dims) {
67
var traces = d3.selectAll('.trace');
@@ -122,8 +123,7 @@ exports.assertHoverLabelContent = function(expectation, msg) {
122123
assertLabelContent(nameSel, expectation.name, ptMsg + ' (name)');
123124

124125
if('isRotated' in expectation) {
125-
expect(g.attr('transform').match(reRotate))
126-
.negateIf(expectation.isRotated)
126+
negateIf(expectation.isRotated, expect(g.attr('transform').match(reRotate)))
127127
.toBe(null, ptMsg + ' should be rotated');
128128
}
129129
} else if(ptCnt > 1) {
@@ -162,8 +162,7 @@ exports.assertHoverLabelContent = function(expectation, msg) {
162162
});
163163

164164
if('isRotated' in expectation) {
165-
expect(g.attr('transform').match(reRotate))
166-
.negateIf(expectation.isRotated)
165+
negateIf(expectation.isRotated, expect(g.attr('transform').match(reRotate)))
167166
.toBe(null, ptMsg + ' ' + i + ' should be rotated');
168167
}
169168
});

test/jasmine/assets/custom_matchers.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
/*
22
* custom_matchers - to be included in karma.conf.js, so it can
33
* add these matchers to jasmine globally and all suites have access.
4-
*
5-
* Also adds `.negateIf` which is not a matcher but a conditional `.not`:
6-
*
7-
* expect(x).negateIf(condition).toBe(0);
8-
*
9-
* is equivalent to:
10-
*
11-
* if(condition) expect(x).toBe(0);
12-
* else expect(x).not.toBe(0);
134
*/
145

156
'use strict';
@@ -241,9 +232,4 @@ function arrayToStr(array) {
241232

242233
beforeAll(function() {
243234
jasmine.addMatchers(matchers);
244-
245-
jasmine.Expectation.prototype.negateIf = function(negate) {
246-
if(negate) return this.not;
247-
return this;
248-
};
249235
});

test/jasmine/assets/negate_if.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
/**
4+
* Helpers that can negate an expectation given a condition
5+
*
6+
* @param {boolean OR function} condition
7+
* @param {jasmine expect return} expectation
8+
* @returns {jasmine expect return}
9+
*
10+
* Example:
11+
*
12+
* negateIf(myCondition, expect(actual)).toBe(expected);
13+
*
14+
*/
15+
function negateIf(condition, expectation) {
16+
return (typeof condition === 'function' ? condition() : condition) ?
17+
expectation.not :
18+
expectation;
19+
}
20+
21+
module.exports = negateIf;

test/jasmine/tests/bar_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var DBLCLICKDELAY = require('../../../src/constants/interactions').DBLCLICKDELAY
1212
var createGraphDiv = require('../assets/create_graph_div');
1313
var destroyGraphDiv = require('../assets/destroy_graph_div');
1414
var failTest = require('../assets/fail_test');
15+
var negateIf = require('../assets/negate_if');
1516
var checkTicks = require('../assets/custom_assertions').checkTicks;
1617
var supplyAllDefaults = require('../assets/supply_defaults');
1718
var color = require('../../../src/components/color');
@@ -1759,7 +1760,7 @@ describe('A bar plot', function() {
17591760
if(!i) return;
17601761
var bbox = this.getBoundingClientRect();
17611762
['left', 'right', 'top', 'bottom', 'width', 'height'].forEach(function(dim) {
1762-
expect(bbox[dim]).negateIf(dims.indexOf(dim) === -1)
1763+
negateIf(dims.indexOf(dim) === -1, expect(bbox[dim]))
17631764
.toBeWithin(bbox1[dim], 0.1, msg + ' (' + i + '): ' + dim);
17641765
});
17651766
});

test/jasmine/tests/colorbar_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var subroutines = require('@src/plot_api/subroutines');
88
var createGraphDiv = require('../assets/create_graph_div');
99
var destroyGraphDiv = require('../assets/destroy_graph_div');
1010
var failTest = require('../assets/fail_test');
11+
var negateIf = require('../assets/negate_if');
1112
var supplyAllDefaults = require('../assets/supply_defaults');
1213
var assertPlotSize = require('../assets/custom_assertions').assertPlotSize;
1314
var drag = require('../assets/drag');
@@ -119,7 +120,7 @@ describe('Test colorbar:', function() {
119120
var cbbg = colorbars.selectAll('.cbbg');
120121
var cbfills = colorbars.selectAll('.cbfill');
121122

122-
expect(cbfills.size()).negateIf(multiFill).toBe(1);
123+
negateIf(multiFill, expect(cbfills.size())).toBe(1);
123124

124125
if(!cbHeight) cbHeight = 400;
125126
var bgHeight = +cbbg.attr('height');

test/jasmine/tests/geo_test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var d3 = require('d3');
1111
var createGraphDiv = require('../assets/create_graph_div');
1212
var destroyGraphDiv = require('../assets/destroy_graph_div');
1313
var failTest = require('../assets/fail_test');
14+
var negateIf = require('../assets/negate_if');
1415
var getClientPosition = require('../assets/get_client_position');
1516
var mouseEvent = require('../assets/mouse_event');
1617
var click = require('../assets/click');
@@ -1571,8 +1572,8 @@ describe('Test geo base layers', function() {
15711572
var cd0 = gd.calcdata[0];
15721573
var subplot = gd._fullLayout.geo._subplot;
15731574

1574-
expect(cd0[0].geojson).negateIf(geojson[0]).toBe(null);
1575-
expect(cd0[1].geojson).negateIf(geojson[1]).toBe(null);
1575+
negateIf(geojson[0], expect(cd0[0].geojson)).toBe(null);
1576+
negateIf(geojson[1], expect(cd0[1].geojson)).toBe(null);
15761577

15771578
expect(Object.keys(subplot.layers).length).toEqual(layers.length, '# of layers');
15781579

test/jasmine/tests/plot_api_test.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var d3 = require('d3');
1717
var createGraphDiv = require('../assets/create_graph_div');
1818
var destroyGraphDiv = require('../assets/destroy_graph_div');
1919
var failTest = require('../assets/fail_test');
20+
var negateIf = require('../assets/negate_if');
2021
var checkTicks = require('../assets/custom_assertions').checkTicks;
2122
var supplyAllDefaults = require('../assets/supply_defaults');
2223

@@ -1110,9 +1111,9 @@ describe('Test plot api', function() {
11101111
var zmax1 = 10;
11111112

11121113
function check(auto, msg) {
1113-
expect(gd._fullData[0].zmin).negateIf(auto).toBe(zmin0, msg);
1114+
negateIf(auto, expect(gd._fullData[0].zmin)).toBe(zmin0, msg);
11141115
expect(gd._fullData[0].zauto).toBe(auto, msg);
1115-
expect(gd._fullData[1].zmax).negateIf(auto).toBe(zmax1, msg);
1116+
negateIf(auto, expect(gd._fullData[1].zmax)).toBe(zmax1, msg);
11161117
expect(gd._fullData[1].zauto).toBe(auto, msg);
11171118
}
11181119

@@ -1153,12 +1154,12 @@ describe('Test plot api', function() {
11531154

11541155
function check(auto, autocolorscale, msg) {
11551156
expect(gd._fullData[0].marker.cauto).toBe(auto, msg);
1156-
expect(gd._fullData[0].marker.cmin).negateIf(auto).toBe(mcmin0);
1157+
negateIf(auto, expect(gd._fullData[0].marker.cmin)).toBe(mcmin0);
11571158
expect(gd._fullData[0].marker.autocolorscale).toBe(autocolorscale, msg);
11581159
expect(gd._fullData[0].marker.colorscale).toEqual(auto ? autocscale : scales[mcscl0]);
11591160

11601161
expect(gd._fullData[1].marker.line.cauto).toBe(auto, msg);
1161-
expect(gd._fullData[1].marker.line.cmax).negateIf(auto).toBe(mlcmax1);
1162+
negateIf(auto, expect(gd._fullData[1].marker.line.cmax)).toBe(mlcmax1);
11621163
expect(gd._fullData[1].marker.line.autocolorscale).toBe(autocolorscale, msg);
11631164
expect(gd._fullData[1].marker.line.colorscale).toEqual(auto ? autocscale : scales[mlcscl1]);
11641165
}
@@ -1323,8 +1324,8 @@ describe('Test plot api', function() {
13231324
function check(auto, msg) {
13241325
expect(gd.data[0].autocontour).toBe(auto, msg);
13251326
expect(gd.data[1].autocontour).toBe(auto, msg);
1326-
expect(gd.data[0].contours.start).negateIf(auto).toBe(start0, msg);
1327-
expect(gd.data[1].contours.size).negateIf(auto).toBe(size1, msg);
1327+
negateIf(auto, expect(gd.data[0].contours.start)).toBe(start0, msg);
1328+
negateIf(auto, expect(gd.data[1].contours.size)).toBe(size1, msg);
13281329
}
13291330

13301331
Plotly.plot(gd, [
@@ -1411,9 +1412,9 @@ describe('Test plot api', function() {
14111412
var dtick1 = 0.8;
14121413

14131414
function check(auto, msg) {
1414-
expect(gd._fullData[0].colorbar.tick0).negateIf(auto).toBe(tick00, msg);
1415+
negateIf(auto, expect(gd._fullData[0].colorbar.tick0)).toBe(tick00, msg);
14151416
expect(gd._fullData[0].colorbar.tickmode).toBe(auto ? 'auto' : 'linear', msg);
1416-
expect(gd._fullData[1].colorbar.dtick).negateIf(auto).toBe(dtick1, msg);
1417+
negateIf(auto, expect(gd._fullData[1].colorbar.dtick)).toBe(dtick1, msg);
14171418
expect(gd._fullData[1].colorbar.tickmode).toBe(auto ? 'auto' : 'linear', msg);
14181419
}
14191420

test/jasmine/tests/polar_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var d3 = require('d3');
77
var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
99
var failTest = require('../assets/fail_test');
10+
var negateIf = require('../assets/negate_if');
1011
var mouseEvent = require('../assets/mouse_event');
1112
var click = require('../assets/click');
1213
var doubleClick = require('../assets/double_click');
@@ -460,7 +461,7 @@ describe('Test relayout on polar subplots:', function() {
460461
expect(txt.text()).toBe(content, 'radial axis title');
461462
}
462463

463-
expect(newBBox).negateIf(didBBoxChanged).toEqual(lastBBox, 'did bbox change');
464+
negateIf(didBBoxChanged, expect(newBBox)).toEqual(lastBBox, 'did bbox change');
464465
lastBBox = newBBox;
465466
}
466467

test/jasmine/tests/scatter_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Plotly = require('@lib/index');
88
var createGraphDiv = require('../assets/create_graph_div');
99
var destroyGraphDiv = require('../assets/destroy_graph_div');
1010
var customAssertions = require('../assets/custom_assertions');
11+
var negateIf = require('../assets/negate_if');
1112
var failTest = require('../assets/fail_test');
1213
var transitions = require('../assets/transitions');
1314

@@ -952,7 +953,7 @@ describe('end-to-end scatter tests', function() {
952953
function checkFill(visible, msg) {
953954
var fillSelection = d3.select(gd).selectAll('.scatterlayer .js-fill');
954955
expect(fillSelection.size()).toBe(1, msg);
955-
expect(fillSelection.attr('d')).negateIf(visible).toBe('M0,0Z', msg);
956+
negateIf(visible, expect(fillSelection.attr('d'))).toBe('M0,0Z', msg);
956957
}
957958

958959
Plotly.newPlot(gd, [trace0, trace1, trace2], {}, {scrollZoom: true})

0 commit comments

Comments
 (0)