Skip to content

Commit 7c9f28c

Browse files
committed
pull drag interaction out of shapes_test - and speed it up
1 parent 5406829 commit 7c9f28c

File tree

3 files changed

+83
-117
lines changed

3 files changed

+83
-117
lines changed

test/jasmine/assets/drag.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var mouseEvent = require('../assets/mouse_event');
2+
3+
/*
4+
* drag: grab a node and drag it (dx, dy) pixels
5+
* optionally specify an edge ('n', 'se', 'w' etc)
6+
* to grab it by an edge or corner (otherwise the middle is used)
7+
*/
8+
module.exports = function(node, dx, dy, edge) {
9+
10+
edge = edge || '';
11+
var bbox = node.getBoundingClientRect(),
12+
fromX, fromY;
13+
14+
if(edge.indexOf('n') !== -1) fromY = bbox.top;
15+
else if(edge.indexOf('s') !== -1) fromY = bbox.bottom;
16+
else fromY = (bbox.bottom + bbox.top) / 2;
17+
18+
if(edge.indexOf('w') !== -1) fromX = bbox.left;
19+
else if(edge.indexOf('e') !== -1) fromX = bbox.right;
20+
else fromX = (bbox.left + bbox.right) / 2;
21+
22+
23+
var toX = fromX + dx,
24+
toY = fromY + dy;
25+
26+
mouseEvent('mousemove', fromX, fromY, {element: node});
27+
mouseEvent('mousedown', fromX, fromY, {element: node});
28+
29+
var promise = waitForDragCover().then(function(dragCoverNode) {
30+
mouseEvent('mousemove', toX, toY, {element: dragCoverNode});
31+
mouseEvent('mouseup', toX, toY, {element: dragCoverNode});
32+
return waitForDragCoverRemoval();
33+
});
34+
35+
return promise;
36+
};
37+
38+
function waitForDragCover() {
39+
return new Promise(function(resolve) {
40+
var interval = 5,
41+
timeout = 5000;
42+
43+
var id = setInterval(function() {
44+
var dragCoverNode = document.querySelector('.dragcover');
45+
if(dragCoverNode) {
46+
clearInterval(id);
47+
resolve(dragCoverNode);
48+
}
49+
50+
timeout -= interval;
51+
if(timeout < 0) {
52+
clearInterval(id);
53+
throw new Error('waitForDragCover: timeout');
54+
}
55+
}, interval);
56+
});
57+
}
58+
59+
function waitForDragCoverRemoval() {
60+
return new Promise(function(resolve) {
61+
var interval = 5,
62+
timeout = 5000;
63+
64+
var id = setInterval(function() {
65+
var dragCoverNode = document.querySelector('.dragcover');
66+
if(!dragCoverNode) {
67+
clearInterval(id);
68+
resolve(dragCoverNode);
69+
}
70+
71+
timeout -= interval;
72+
if(timeout < 0) {
73+
clearInterval(id);
74+
throw new Error('waitForDragCoverRemoval: timeout');
75+
}
76+
}, interval);
77+
});
78+
}

test/jasmine/assets/mouse_event.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function(type, x, y, opts) {
1010
fullOpts.buttons = opts.buttons;
1111
}
1212

13-
var el = document.elementFromPoint(x, y),
13+
var el = opts.element || document.elementFromPoint(x, y),
1414
ev;
1515

1616
if(type === 'scroll') {
@@ -20,4 +20,6 @@ module.exports = function(type, x, y, opts) {
2020
}
2121

2222
el.dispatchEvent(ev);
23+
24+
return el;
2325
};

test/jasmine/tests/shapes_test.js

Lines changed: 2 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var customMatchers = require('../assets/custom_matchers');
1414
var createGraphDiv = require('../assets/create_graph_div');
1515
var destroyGraphDiv = require('../assets/destroy_graph_div');
1616
var failTest = require('../assets/fail_test');
17+
var drag = require('../assets/drag');
1718

1819

1920
describe('Test shapes defaults:', function() {
@@ -748,7 +749,7 @@ describe('Test shapes', function() {
748749

749750
var initialCoordinates = getShapeCoordinates(layoutShape, x2p, y2p);
750751

751-
return resize(direction, node, dx, dy).then(function() {
752+
return drag(node, dx, dy, direction).then(function() {
752753
var finalCoordinates = getShapeCoordinates(layoutShape, x2p, y2p);
753754

754755
var keyN, keyS, keyW, keyE;
@@ -815,118 +816,3 @@ describe('Test shapes', function() {
815816
return coordinates;
816817
}
817818
});
818-
819-
var DBLCLICKDELAY = require('@src/plots/cartesian/constants').DBLCLICKDELAY;
820-
821-
function mouseDown(node, x, y) {
822-
node.dispatchEvent(new MouseEvent('mousedown', {
823-
bubbles: true,
824-
clientX: x,
825-
clientY: y
826-
}));
827-
}
828-
829-
function mouseMove(node, x, y) {
830-
node.dispatchEvent(new MouseEvent('mousemove', {
831-
bubbles: true,
832-
clientX: x,
833-
clientY: y
834-
}));
835-
}
836-
837-
function mouseUp(node, x, y) {
838-
node.dispatchEvent(new MouseEvent('mouseup', {
839-
bubbles: true,
840-
clientX: x,
841-
clientY: y
842-
}));
843-
}
844-
845-
function drag(node, dx, dy) {
846-
var bbox = node.getBoundingClientRect(),
847-
fromX = (bbox.left + bbox.right) / 2,
848-
fromY = (bbox.bottom + bbox.top) / 2,
849-
toX = fromX + dx,
850-
toY = fromY + dy;
851-
852-
mouseMove(node, fromX, fromY);
853-
mouseDown(node, fromX, fromY);
854-
855-
var promise = waitForDragCover().then(function(dragCoverNode) {
856-
mouseMove(dragCoverNode, toX, toY);
857-
mouseUp(dragCoverNode, toX, toY);
858-
return waitForDragCoverRemoval();
859-
});
860-
861-
return promise;
862-
}
863-
864-
function resize(direction, node, dx, dy) {
865-
var bbox = node.getBoundingClientRect();
866-
867-
var fromX, fromY, toX, toY;
868-
869-
if(~direction.indexOf('n')) fromY = bbox.top;
870-
else if(~direction.indexOf('s')) fromY = bbox.bottom;
871-
else fromY = (bbox.bottom + bbox.top) / 2;
872-
873-
if(~direction.indexOf('w')) fromX = bbox.left;
874-
else if(~direction.indexOf('e')) fromX = bbox.right;
875-
else fromX = (bbox.left + bbox.right) / 2;
876-
877-
toX = fromX + dx;
878-
toY = fromY + dy;
879-
880-
mouseMove(node, fromX, fromY);
881-
mouseDown(node, fromX, fromY);
882-
883-
var promise = waitForDragCover().then(function(dragCoverNode) {
884-
mouseMove(dragCoverNode, toX, toY);
885-
mouseUp(dragCoverNode, toX, toY);
886-
return waitForDragCoverRemoval();
887-
});
888-
889-
return promise;
890-
}
891-
892-
function waitForDragCover() {
893-
return new Promise(function(resolve) {
894-
var interval = DBLCLICKDELAY / 4,
895-
timeout = 5000;
896-
897-
var id = setInterval(function() {
898-
var dragCoverNode = d3.selectAll('.dragcover').node();
899-
if(dragCoverNode) {
900-
clearInterval(id);
901-
resolve(dragCoverNode);
902-
}
903-
904-
timeout -= interval;
905-
if(timeout < 0) {
906-
clearInterval(id);
907-
throw new Error('waitForDragCover: timeout');
908-
}
909-
}, interval);
910-
});
911-
}
912-
913-
function waitForDragCoverRemoval() {
914-
return new Promise(function(resolve) {
915-
var interval = DBLCLICKDELAY / 4,
916-
timeout = 5000;
917-
918-
var id = setInterval(function() {
919-
var dragCoverNode = d3.selectAll('.dragcover').node();
920-
if(!dragCoverNode) {
921-
clearInterval(id);
922-
resolve(dragCoverNode);
923-
}
924-
925-
timeout -= interval;
926-
if(timeout < 0) {
927-
clearInterval(id);
928-
throw new Error('waitForDragCoverRemoval: timeout');
929-
}
930-
}, interval);
931-
});
932-
}

0 commit comments

Comments
 (0)