Skip to content

Commit

Permalink
add toBeClassed custom matcher
Browse files Browse the repository at this point in the history
- to easily test all items in class list while not
  having to worry about class name ordering.
  • Loading branch information
etpinard committed Mar 14, 2018
1 parent 8e6032d commit b974eab
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
25 changes: 25 additions & 0 deletions test/jasmine/assets/custom_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ var matchers = {
msgExtra
].join(' ');

return {
pass: passed,
message: message
};
}
};
},

toBeClassed: function() {
return {
compare: function(node, _expected, msgExtra) {
var actual = node.classList;
var expected = Array.isArray(_expected) ? _expected : [_expected];

var passed = (
actual.length === expected.length &&
expected.every(function(e) { return actual.contains(e); })
);

var message = [
'Expected classList', '[' + actual + ']',
'to have classes', expected,
msgExtra
].join(' ');

return {
pass: passed,
message: message
Expand Down
15 changes: 5 additions & 10 deletions test/jasmine/tests/cartesian_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ describe('restyle', function() {

// First is tozero, second is tonext:
expect(d3.selectAll('g.trace.scatter .js-fill').size()).toEqual(2);
expect(fills[0].classList.contains('js-tozero')).toBe(true);
expect(fills[0].classList.contains('js-tonext')).toBe(false);
expect(fills[1].classList.contains('js-tozero')).toBe(false);
expect(fills[1].classList.contains('js-tonext')).toBe(true);
expect(fills[0]).toBeClassed(['js-fill', 'js-tozero']);
expect(fills[1]).toBeClassed(['js-fill', 'js-tonext']);

firstToZero = fills[0];
firstToNext = fills[1];
Expand All @@ -40,8 +38,7 @@ describe('restyle', function() {
}).then(function() {
// Trace 1 hidden leaves only trace zero's tozero fill:
expect(d3.selectAll('g.trace.scatter .js-fill').size()).toEqual(1);
expect(fills[0].classList.contains('js-tozero')).toBe(true);
expect(fills[0].classList.contains('js-tonext')).toBe(false);
expect(fills[0]).toBeClassed(['js-fill', 'js-tozero']);
}).then(function() {
return Plotly.restyle(gd, {visible: [true]}, [1]);
}).then(function() {
Expand All @@ -50,10 +47,8 @@ describe('restyle', function() {

// First is tozero, second is tonext:
expect(d3.selectAll('g.trace.scatter .js-fill').size()).toEqual(2);
expect(fills[0].classList.contains('js-tozero')).toBe(true);
expect(fills[0].classList.contains('js-tonext')).toBe(false);
expect(fills[1].classList.contains('js-tozero')).toBe(false);
expect(fills[1].classList.contains('js-tonext')).toBe(true);
expect(fills[0]).toBeClassed(['js-fill', 'js-tozero']);
expect(fills[1]).toBeClassed(['js-fill', 'js-tonext']);

secondToZero = fills[0];
secondToNext = fills[1];
Expand Down
40 changes: 10 additions & 30 deletions test/jasmine/tests/click_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.nwdrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('nwdrag');
expect(node.classList[2]).toBe('cursor-nw-resize');
expect(node).toBeClassed(['drag', 'nwdrag', 'cursor-nw-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand All @@ -442,9 +440,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.nedrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('nedrag');
expect(node.classList[2]).toBe('cursor-ne-resize');
expect(node).toBeClassed(['drag', 'nedrag', 'cursor-ne-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand All @@ -466,9 +462,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.swdrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('swdrag');
expect(node.classList[2]).toBe('cursor-sw-resize');
expect(node).toBeClassed(['drag', 'swdrag', 'cursor-sw-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand All @@ -490,9 +484,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.sedrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('sedrag');
expect(node.classList[2]).toBe('cursor-se-resize');
expect(node).toBeClassed(['drag', 'sedrag', 'cursor-se-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand All @@ -514,9 +506,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.ewdrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('ewdrag');
expect(node.classList[2]).toBe('cursor-ew-resize');
expect(node).toBeClassed(['drag', 'ewdrag', 'cursor-ew-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand All @@ -538,9 +528,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.wdrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('wdrag');
expect(node.classList[2]).toBe('cursor-w-resize');
expect(node).toBeClassed(['drag', 'wdrag', 'cursor-w-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand All @@ -562,9 +550,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.edrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('edrag');
expect(node.classList[2]).toBe('cursor-e-resize');
expect(node).toBeClassed(['drag', 'edrag', 'cursor-e-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand All @@ -586,9 +572,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.nsdrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('nsdrag');
expect(node.classList[2]).toBe('cursor-ns-resize');
expect(node).toBeClassed(['drag', 'nsdrag', 'cursor-ns-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand All @@ -610,9 +594,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.sdrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('sdrag');
expect(node.classList[2]).toBe('cursor-s-resize');
expect(node).toBeClassed(['drag', 'sdrag', 'cursor-s-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand All @@ -634,9 +616,7 @@ describe('Test click interactions:', function() {
var node = document.querySelector('rect.ndrag');
var pos = getRectCenter(node);

expect(node.classList[1]).toBe('drag');
expect(node.classList[0]).toBe('ndrag');
expect(node.classList[2]).toBe('cursor-n-resize');
expect(node).toBeClassed(['drag', 'ndrag', 'cursor-n-resize']);

expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
Expand Down

0 comments on commit b974eab

Please sign in to comment.