Skip to content

Commit 0729921

Browse files
committed
load custom_matchers globally, and refactor negateIf as a method
1 parent e4227aa commit 0729921

37 files changed

+45
-264
lines changed

test/jasmine/assets/custom_matchers.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
/*
2+
* custom_matchers - to be included in karma.conf.js, so it can
3+
* 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);
13+
*/
14+
115
'use strict';
216

317
var isNumeric = require('fast-isnumeric');
418
var Lib = require('../../../src/lib');
519
var deepEqual = require('deep-equal');
620

7-
module.exports = {
21+
var matchers = {
822
// toEqual except with sparse arrays populated. This arises because:
923
//
1024
// var x = new Array(2)
@@ -165,3 +179,12 @@ function coercePosition(precision) {
165179
function arrayToStr(array) {
166180
return '[ ' + array.join(', ') + ' ]';
167181
}
182+
183+
beforeAll(function() {
184+
jasmine.addMatchers(matchers);
185+
186+
jasmine.Expectation.prototype.negateIf = function(negate) {
187+
if(negate) return this.not;
188+
return this;
189+
};
190+
});

test/jasmine/assets/negate_if.js

-16
This file was deleted.

test/jasmine/karma.conf.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ var pathToStrictD3 = path.join(__dirname, '..', '..', 'tasks', 'util', 'strict_d
9999
var pathToMain = path.join(__dirname, '..', '..', 'lib', 'index.js');
100100
var pathToJQuery = path.join(__dirname, 'assets', 'jquery-1.8.3.min.js');
101101
var pathToIE9mock = path.join(__dirname, 'assets', 'ie9_mock.js');
102+
var pathToCustomMatchers = path.join(__dirname, 'assets', 'custom_matchers.js');
102103

103104

104105
function func(config) {
@@ -132,8 +133,8 @@ func.defaultConfig = {
132133

133134
// list of files / patterns to load in the browser
134135
//
135-
// N.B. this field is filled below
136-
files: [],
136+
// N.B. the rest of this field is filled below
137+
files: [pathToCustomMatchers],
137138

138139
// list of files / pattern to exclude
139140
exclude: [],
@@ -220,16 +221,18 @@ func.defaultConfig = {
220221
}
221222
};
222223

224+
func.defaultConfig.preprocessors[pathToCustomMatchers] = ['browserify'];
225+
223226
if(isFullSuite) {
224227
func.defaultConfig.files.push(pathToJQuery);
225228
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
226229
} else if(isBundleTest) {
227230
switch(basename(testFileGlob)) {
228231
case 'requirejs':
229-
func.defaultConfig.files = [
232+
func.defaultConfig.files.push(
230233
constants.pathToRequireJS,
231234
constants.pathToRequireJSFixture
232-
];
235+
);
233236
break;
234237
case 'ie9':
235238
// load ie9_mock.js before plotly.js+test bundle

test/jasmine/tests/annotations_test.js

-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var HOVERMINTIME = require('@src/components/fx').constants.HOVERMINTIME;
99
var DBLCLICKDELAY = require('@src/constants/interactions').DBLCLICKDELAY;
1010

1111
var d3 = require('d3');
12-
var customMatchers = require('../assets/custom_matchers');
1312
var createGraphDiv = require('../assets/create_graph_div');
1413
var destroyGraphDiv = require('../assets/destroy_graph_div');
1514
var failTest = require('../assets/fail_test');
@@ -561,10 +560,6 @@ describe('annotations autorange', function() {
561560
var mock;
562561
var gd;
563562

564-
beforeAll(function() {
565-
jasmine.addMatchers(customMatchers);
566-
});
567-
568563
beforeEach(function() {
569564
gd = createGraphDiv();
570565
mock = Lib.extendDeep({}, require('@mocks/annotations-autorange.json'));
@@ -908,10 +903,6 @@ describe('annotation effects', function() {
908903
function arrowDrag() { return gd.querySelector('.annotation-arrow-g>.anndrag'); }
909904
function textBox() { return gd.querySelector('.annotation-text-g'); }
910905

911-
beforeAll(function() {
912-
jasmine.addMatchers(customMatchers);
913-
});
914-
915906
function makePlot(annotations, config) {
916907
gd = createGraphDiv();
917908

test/jasmine/tests/axes_test.js

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ var Axes = require('@src/plots/cartesian/axes');
1010

1111
var createGraphDiv = require('../assets/create_graph_div');
1212
var destroyGraphDiv = require('../assets/destroy_graph_div');
13-
var customMatchers = require('../assets/custom_matchers');
1413
var failTest = require('../assets/fail_test');
1514

1615

@@ -577,7 +576,6 @@ describe('Test axes', function() {
577576

578577
beforeEach(function() {
579578
gd = createGraphDiv();
580-
jasmine.addMatchers(customMatchers);
581579
});
582580

583581
afterEach(destroyGraphDiv);

test/jasmine/tests/bar_test.js

+1-29
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ var Axes = require('@src/plots/cartesian/axes');
1010
var createGraphDiv = require('../assets/create_graph_div');
1111
var destroyGraphDiv = require('../assets/destroy_graph_div');
1212
var fail = require('../assets/fail_test');
13-
var customMatchers = require('../assets/custom_matchers');
1413
var checkTicks = require('../assets/custom_assertions').checkTicks;
15-
var negateIf = require('../assets/negate_if');
1614

1715
var d3 = require('d3');
1816

@@ -154,10 +152,6 @@ describe('Bar.supplyDefaults', function() {
154152
describe('bar calc / setPositions', function() {
155153
'use strict';
156154

157-
beforeAll(function() {
158-
jasmine.addMatchers(customMatchers);
159-
});
160-
161155
it('should fill in calc pt fields (stack case)', function() {
162156
var gd = mockBarPlot([{
163157
y: [2, 1, 2]
@@ -269,10 +263,6 @@ describe('bar calc / setPositions', function() {
269263
describe('Bar.calc', function() {
270264
'use strict';
271265

272-
beforeAll(function() {
273-
jasmine.addMatchers(customMatchers);
274-
});
275-
276266
it('should guard against invalid base items', function() {
277267
var gd = mockBarPlot([{
278268
base: [null, 1, 2],
@@ -328,10 +318,6 @@ describe('Bar.calc', function() {
328318
describe('Bar.setPositions', function() {
329319
'use strict';
330320

331-
beforeAll(function() {
332-
jasmine.addMatchers(customMatchers);
333-
});
334-
335321
it('should guard against invalid offset items', function() {
336322
var gd = mockBarPlot([{
337323
offset: [null, 0, 1],
@@ -768,10 +754,6 @@ describe('A bar plot', function() {
768754

769755
var gd;
770756

771-
beforeAll(function() {
772-
jasmine.addMatchers(customMatchers);
773-
});
774-
775757
beforeEach(function() {
776758
gd = createGraphDiv();
777759
});
@@ -1205,7 +1187,7 @@ describe('A bar plot', function() {
12051187
if(!i) return;
12061188
var bbox = this.getBoundingClientRect();
12071189
['left', 'right', 'top', 'bottom', 'width', 'height'].forEach(function(dim) {
1208-
negateIf(expect(bbox[dim]), dims.indexOf(dim) === -1)
1190+
expect(bbox[dim]).negateIf(dims.indexOf(dim) === -1)
12091191
.toBeWithin(bbox1[dim], 0.1, msg + ' (' + i + '): ' + dim);
12101192
});
12111193
});
@@ -1269,10 +1251,6 @@ describe('bar hover', function() {
12691251

12701252
var gd;
12711253

1272-
beforeAll(function() {
1273-
jasmine.addMatchers(customMatchers);
1274-
});
1275-
12761254
afterEach(destroyGraphDiv);
12771255

12781256
function getPointData(gd) {
@@ -1532,17 +1510,13 @@ function mockBarPlot(dataWithoutTraceType, layout) {
15321510
}
15331511

15341512
function assertArrayField(calcData, prop, expectation) {
1535-
// Note that this functions requires to add `customMatchers` to jasmine
1536-
// matchers; i.e: `jasmine.addMatchers(customMatchers);`.
15371513
var values = Lib.nestedProperty(calcData, prop).get();
15381514
if(!Array.isArray(values)) values = [values];
15391515

15401516
expect(values).toBeCloseToArray(expectation, undefined, '(field ' + prop + ')');
15411517
}
15421518

15431519
function assertPointField(calcData, prop, expectation) {
1544-
// Note that this functions requires to add `customMatchers` to jasmine
1545-
// matchers; i.e: `jasmine.addMatchers(customMatchers);`.
15461520
var values = [];
15471521

15481522
calcData.forEach(function(calcTrace) {
@@ -1557,8 +1531,6 @@ function assertPointField(calcData, prop, expectation) {
15571531
}
15581532

15591533
function assertTraceField(calcData, prop, expectation) {
1560-
// Note that this functions requires to add `customMatchers` to jasmine
1561-
// matchers; i.e: `jasmine.addMatchers(customMatchers);`.
15621534
var values = calcData.map(function(calcTrace) {
15631535
return Lib.nestedProperty(calcTrace[0], prop).get();
15641536
});

test/jasmine/tests/carpet_test.js

-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var smoothFill2D = require('@src/traces/carpet/smooth_fill_2d_array');
77
var smoothFill = require('@src/traces/carpet/smooth_fill_array');
88

99
var d3 = require('d3');
10-
var customMatchers = require('../assets/custom_matchers');
1110
var createGraphDiv = require('../assets/create_graph_div');
1211
var destroyGraphDiv = require('../assets/destroy_graph_div');
1312
var fail = require('../assets/fail_test');
@@ -215,8 +214,6 @@ describe('supplyDefaults visibility check', function() {
215214
describe('carpet smooth_fill_2d_array', function() {
216215
var _;
217216

218-
beforeAll(function() { jasmine.addMatchers(customMatchers); });
219-
220217
it('fills in all points trivially', function() {
221218
// Given only corners, should just propagate the constant throughout:
222219
expect(smoothFill2D(
@@ -381,8 +378,6 @@ describe('carpet smooth_fill_2d_array', function() {
381378
describe('smooth_fill_array', function() {
382379
var _;
383380

384-
beforeAll(function() { jasmine.addMatchers(customMatchers); });
385-
386381
it('fills in via linear interplation', function() {
387382
expect(smoothFill([_, _, 2, 3, _, _, 6, 7, _, _, 10, 11, _]))
388383
.toBeCloseToArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);

test/jasmine/tests/cartesian_interact_test.js

-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
99
var mouseEvent = require('../assets/mouse_event');
1010
var failTest = require('../assets/fail_test');
11-
var customMatchers = require('../assets/custom_matchers');
1211
var selectButton = require('../assets/modebar_button');
1312
var drag = require('../assets/drag');
1413
var doubleClick = require('../assets/double_click');
@@ -85,8 +84,6 @@ describe('main plot pan', function() {
8584

8685
it('should respond to pan interactions', function(done) {
8786

88-
jasmine.addMatchers(customMatchers);
89-
9087
var precision = 5;
9188

9289
var buttonPan = selectButton(modeBar, 'pan2d');
@@ -182,10 +179,6 @@ describe('main plot pan', function() {
182179
describe('axis zoom/pan and main plot zoom', function() {
183180
var gd;
184181

185-
beforeAll(function() {
186-
jasmine.addMatchers(customMatchers);
187-
});
188-
189182
beforeEach(function() {
190183
gd = createGraphDiv();
191184
});

test/jasmine/tests/click_test.js

-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
99
var mouseEvent = require('../assets/mouse_event');
1010
var getRectCenter = require('../assets/get_rect_center');
11-
var customMatchers = require('../assets/custom_matchers');
1211

1312
// cartesian click events events use the hover data
1413
// from the mousemove events and then simulate
@@ -52,10 +51,6 @@ describe('Test click interactions:', function() {
5251
var autoRangeX = [-3.011967491973726, 2.1561305597186564],
5352
autoRangeY = [-0.9910086301469277, 1.389382716298284];
5453

55-
beforeAll(function() {
56-
jasmine.addMatchers(customMatchers);
57-
});
58-
5954
beforeEach(function() {
6055
gd = createGraphDiv();
6156
mockCopy = Lib.extendDeep({}, mock);

test/jasmine/tests/contour_test.js

-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var colorScales = require('@src/components/colorscale/scales');
99
var fail = require('../assets/fail_test');
1010
var createGraphDiv = require('../assets/create_graph_div');
1111
var destroyGraphDiv = require('../assets/destroy_graph_div');
12-
var customMatchers = require('../assets/custom_matchers');
1312
var checkTicks = require('../assets/custom_assertions').checkTicks;
1413

1514

@@ -178,10 +177,6 @@ describe('contour makeColorMap', function() {
178177
describe('contour calc', function() {
179178
'use strict';
180179

181-
beforeAll(function() {
182-
jasmine.addMatchers(customMatchers);
183-
});
184-
185180
function _calc(opts) {
186181
var base = { type: 'contour' },
187182
trace = Lib.extendFlat({}, base, opts),

test/jasmine/tests/drawing_test.js

-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@ var svgTextUtils = require('@src/lib/svg_text_utils');
55
var createGraphDiv = require('../assets/create_graph_div');
66
var destroyGraphDiv = require('../assets/destroy_graph_div');
77
var fail = require('../assets/fail_test');
8-
var customMatchers = require('../assets/custom_matchers');
98

109
describe('Drawing', function() {
1110
'use strict';
1211

13-
beforeAll(function() {
14-
jasmine.addMatchers(customMatchers);
15-
});
16-
1712
describe('setClipUrl', function() {
1813

1914
beforeEach(function() {

test/jasmine/tests/errorbars_test.js

-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ var d3 = require('d3');
44
var createGraphDiv = require('../assets/create_graph_div');
55
var destroyGraphDiv = require('../assets/destroy_graph_div');
66
var fail = require('../assets/fail_test');
7-
var customMatchers = require('../assets/custom_matchers');
87

98

109
describe('errorbar plotting', function() {
1110
var gd;
1211

1312
beforeEach(function() {
1413
gd = createGraphDiv();
15-
jasmine.addMatchers(customMatchers);
1614
});
1715

1816
afterEach(destroyGraphDiv);

test/jasmine/tests/geo_test.js

-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var topojsonUtils = require('@src/lib/topojson_utils');
1111
var d3 = require('d3');
1212
var createGraphDiv = require('../assets/create_graph_div');
1313
var destroyGraphDiv = require('../assets/destroy_graph_div');
14-
var customMatchers = require('../assets/custom_matchers');
1514
var getClientPosition = require('../assets/get_client_position');
1615
var mouseEvent = require('../assets/mouse_event');
1716
var click = require('../assets/click');
@@ -1060,8 +1059,6 @@ describe('Test event property of interactions on a geo plot:', function() {
10601059
nearPos;
10611060

10621061
beforeAll(function(done) {
1063-
jasmine.addMatchers(customMatchers);
1064-
10651062
gd = createGraphDiv();
10661063
mockCopy = Lib.extendDeep({}, mock);
10671064
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {

0 commit comments

Comments
 (0)