Skip to content

Commit a466517

Browse files
committed
add jasmine plot interaction tests!
1 parent 1dc2a01 commit a466517

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
var d3 = require('d3');
2+
3+
var Plotly = require('@src/index');
4+
var Fx = require('@src/plots/cartesian/graph_interact');
5+
6+
7+
describe('Test plot structure', function () {
8+
'use strict';
9+
10+
function createGraphDiv() {
11+
var gd = document.createElement('div');
12+
gd.id = 'graph';
13+
document.body.appendChild(gd);
14+
return gd;
15+
}
16+
17+
function destroyGraphDiv() {
18+
var gd = document.getElementById('graph');
19+
document.body.removeChild(gd);
20+
}
21+
22+
afterEach(destroyGraphDiv);
23+
24+
describe('cartesian plots', function() {
25+
describe('scatter traces', function() {
26+
var mock = require('@mocks/14.json');
27+
28+
beforeEach(function(done) {
29+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
30+
});
31+
32+
it('has one *subplot xy* node', function() {
33+
var nodes = d3.selectAll('g.subplot.xy');
34+
expect(nodes[0].length).toEqual(1);
35+
});
36+
37+
it('has one *scatterlayer* node', function() {
38+
var nodes = d3.selectAll('g.scatterlayer');
39+
expect(nodes[0].length).toEqual(1);
40+
});
41+
42+
it('has as many *trace scatter* nodes as there are traces', function() {
43+
var nodes = d3.selectAll('g.trace.scatter');
44+
expect(nodes[0].length).toEqual(mock.data.length);
45+
});
46+
47+
it('has as many *point* nodes as there are traces', function() {
48+
var nodes = d3.selectAll('path.point');
49+
50+
var Npts = 0;
51+
mock.data.forEach(function(trace) {
52+
Npts += trace.x.length;
53+
});
54+
55+
expect(nodes[0].length).toEqual(Npts);
56+
});
57+
58+
it('responds to hover', function() {
59+
var gd = document.getElementById('graph');
60+
61+
var evt = {
62+
clientX: gd.layout.width/ 2,
63+
clientY: gd.layout.height / 2
64+
};
65+
66+
Fx.hover('graph', evt, 'xy');
67+
68+
var hoverTrace = gd._hoverdata[0];
69+
70+
expect(hoverTrace.curveNumber).toEqual(0);
71+
expect(hoverTrace.pointNumber).toEqual(17);
72+
expect(hoverTrace.x).toEqual(0.388);
73+
expect(hoverTrace.y).toEqual(1);
74+
75+
expect(d3.selectAll('g.axistext')[0].length).toEqual(1);
76+
expect(d3.selectAll('g.hovertext')[0].length).toEqual(1);
77+
});
78+
});
79+
80+
describe('pie traces', function() {
81+
var mock = require('@mocks/pie_simple.json');
82+
83+
beforeEach(function(done) {
84+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
85+
});
86+
87+
it('has as many *slice* nodes as there are pie items', function() {
88+
var nodes = d3.selectAll('g.slice');
89+
90+
var Npts = 0;
91+
mock.data.forEach(function(trace) {
92+
Npts += trace.values.length;
93+
});
94+
95+
expect(nodes[0].length).toEqual(Npts);
96+
});
97+
});
98+
99+
});
100+
101+
describe('gl3d plots', function() {
102+
var mock = require('@mocks/gl3d_marker-arrays.json');
103+
104+
beforeEach(function(done) {
105+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
106+
});
107+
108+
it('has one *canvas* node', function() {
109+
var nodes = d3.selectAll('canvas');
110+
expect(nodes[0].length).toEqual(1);
111+
});
112+
});
113+
114+
describe('gl2d plots', function() {
115+
var mock = require('@mocks/gl2d_10.json');
116+
117+
beforeEach(function(done) {
118+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
119+
});
120+
121+
it('has one *canvas* node', function() {
122+
var nodes = d3.selectAll('canvas');
123+
expect(nodes[0].length).toEqual(1);
124+
});
125+
});
126+
127+
describe('geo plots', function() {
128+
var mock = require('@mocks/geo_first.json');
129+
130+
beforeEach(function(done) {
131+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
132+
});
133+
134+
it('has as many *choroplethlocation* nodes as there are choropleth locations', function() {
135+
var nodes = d3.selectAll('path.choroplethlocation');
136+
137+
var Npts = 0;
138+
mock.data.forEach(function(trace) {
139+
var items = trace.locations;
140+
if(items) Npts += items.length;
141+
});
142+
143+
expect(nodes[0].length).toEqual(Npts);
144+
});
145+
146+
it('has as many *point* nodes as there are marker points', function() {
147+
var nodes = d3.selectAll('path.point');
148+
149+
var Npts = 0;
150+
mock.data.forEach(function(trace) {
151+
var items = trace.lat;
152+
if(items) Npts += items.length;
153+
});
154+
155+
expect(nodes[0].length).toEqual(Npts);
156+
});
157+
});
158+
159+
describe('polar plots', function() {
160+
var mock = require('@mocks/polar_scatter.json');
161+
162+
beforeEach(function(done) {
163+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
164+
});
165+
166+
it('has as many *mark dot* nodes as there are points', function() {
167+
var nodes = d3.selectAll('path.mark.dot');
168+
169+
var Npts = 0;
170+
mock.data.forEach(function(trace) {
171+
Npts += trace.r.length;
172+
});
173+
174+
expect(nodes[0].length).toEqual(Npts);
175+
});
176+
});
177+
});

0 commit comments

Comments
 (0)