Skip to content

Commit f011525

Browse files
committed
test title hover effects
1 parent e526042 commit f011525

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

src/components/titles/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var Lib = require('../../lib');
1818
var Drawing = require('../drawing');
1919
var Color = require('../color');
2020
var svgTextUtils = require('../../lib/svg_text_utils');
21+
var interactConstants = require('../../constants/interactions');
2122

2223

2324
var Titles = module.exports = {};
@@ -196,11 +197,11 @@ Titles.draw = function(gd, titleClass, options) {
196197
.text(txt)
197198
.on('mouseover.opacity', function() {
198199
d3.select(this).transition()
199-
.duration(100).style('opacity', 1);
200+
.duration(interactConstants.SHOW_PLACEHOLDER).style('opacity', 1);
200201
})
201202
.on('mouseout.opacity', function() {
202203
d3.select(this).transition()
203-
.duration(1000).style('opacity', 0);
204+
.duration(interactConstants.HIDE_PLACEHOLDER).style('opacity', 0);
204205
});
205206
}
206207

src/constants/interactions.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
12+
module.exports = {
13+
/**
14+
* Timing information for interactive elements
15+
*/
16+
SHOW_PLACEHOLDER: 100,
17+
HIDE_PLACEHOLDER: 1000
18+
};

test/jasmine/tests/titles_test.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
var d3 = require('d3');
2+
3+
var Plotly = require('@lib/index');
4+
var interactConstants = require('@src/constants/interactions');
5+
6+
var createGraphDiv = require('../assets/create_graph_div');
7+
var destroyGraphDiv = require('../assets/destroy_graph_div');
8+
var mouseEvent = require('../assets/mouse_event');
9+
10+
describe('editable titles', function() {
11+
'use strict';
12+
13+
var data = [{x: [1, 2, 3], y: [1, 2, 3]}];
14+
15+
var gd;
16+
17+
afterEach(destroyGraphDiv);
18+
19+
beforeEach(function() {
20+
gd = createGraphDiv();
21+
});
22+
23+
function checkTitle(letter, text, opacityOut, opacityIn) {
24+
var titleEl = d3.select('.' + letter + 'title');
25+
expect(titleEl.text()).toBe(text);
26+
expect(+titleEl.style('opacity')).toBe(opacityOut);
27+
28+
var bb = titleEl.node().getBoundingClientRect(),
29+
xCenter = (bb.left + bb.right) / 2,
30+
yCenter = (bb.top + bb.bottom) / 2,
31+
done,
32+
promise = new Promise(function(resolve) { done = resolve; });
33+
34+
mouseEvent('mouseover', xCenter, yCenter);
35+
setTimeout(function() {
36+
expect(+titleEl.style('opacity')).toBe(opacityIn);
37+
38+
mouseEvent('mouseout', xCenter, yCenter);
39+
setTimeout(function() {
40+
expect(+titleEl.style('opacity')).toBe(opacityOut);
41+
done();
42+
}, interactConstants.HIDE_PLACEHOLDER + 50);
43+
}, interactConstants.SHOW_PLACEHOLDER + 50);
44+
45+
return promise;
46+
}
47+
48+
it('shows default titles semi-opaque with no hover effects', function(done) {
49+
Plotly.plot(gd, data, {}, {editable: true})
50+
.then(function() {
51+
return Promise.all([
52+
// Check all three titles in parallel. This only works because
53+
// we're using synthetic events, not a real mouse. It's a big
54+
// win though because the test takes 1.2 seconds with the
55+
// animations...
56+
checkTitle('x', 'Click to enter X axis title', 0.2, 0.2),
57+
checkTitle('y', 'Click to enter Y axis title', 0.2, 0.2),
58+
checkTitle('g', 'Click to enter Plot title', 0.2, 0.2)
59+
]);
60+
})
61+
.then(done);
62+
});
63+
64+
it('has hover effects for blank titles', function(done) {
65+
Plotly.plot(gd, data, {
66+
xaxis: {title: ''},
67+
yaxis: {title: ''},
68+
title: ''
69+
}, {editable: true})
70+
.then(function() {
71+
return Promise.all([
72+
checkTitle('x', 'Click to enter X axis title', 0, 1),
73+
checkTitle('y', 'Click to enter Y axis title', 0, 1),
74+
checkTitle('g', 'Click to enter Plot title', 0, 1)
75+
]);
76+
})
77+
.then(done);
78+
});
79+
80+
it('has no hover effects for titles that used to be blank', function(done) {
81+
Plotly.plot(gd, data, {
82+
xaxis: {title: ''},
83+
yaxis: {title: ''},
84+
title: ''
85+
}, {editable: true})
86+
.then(function() {
87+
return Plotly.relayout(gd, {
88+
'xaxis.title': 'XXX',
89+
'yaxis.title': 'YYY',
90+
'title': 'TTT'
91+
});
92+
})
93+
.then(function() {
94+
return Promise.all([
95+
checkTitle('x', 'XXX', 1, 1),
96+
checkTitle('y', 'YYY', 1, 1),
97+
checkTitle('g', 'TTT', 1, 1)
98+
]);
99+
})
100+
.then(done);
101+
});
102+
103+
});

0 commit comments

Comments
 (0)