|
| 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 | + function editTitle(letter, attr, text) { |
| 49 | + return new Promise(function(resolve) { |
| 50 | + gd.once('plotly_relayout', function(eventData) { |
| 51 | + expect(eventData[attr]).toEqual(text, [letter, attr, eventData]); |
| 52 | + setTimeout(resolve, 10); |
| 53 | + }); |
| 54 | + |
| 55 | + var textNode = document.querySelector('.' + letter + 'title'); |
| 56 | + textNode.dispatchEvent(new window.MouseEvent('click')); |
| 57 | + |
| 58 | + var editNode = document.querySelector('.plugin-editable.editable'); |
| 59 | + editNode.dispatchEvent(new window.FocusEvent('focus')); |
| 60 | + editNode.textContent = text; |
| 61 | + editNode.dispatchEvent(new window.FocusEvent('focus')); |
| 62 | + editNode.dispatchEvent(new window.FocusEvent('blur')); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + it('shows default titles semi-opaque with no hover effects', function(done) { |
| 67 | + Plotly.plot(gd, data, {}, {editable: true}) |
| 68 | + .then(function() { |
| 69 | + return Promise.all([ |
| 70 | + // Check all three titles in parallel. This only works because |
| 71 | + // we're using synthetic events, not a real mouse. It's a big |
| 72 | + // win though because the test takes 1.2 seconds with the |
| 73 | + // animations... |
| 74 | + checkTitle('x', 'Click to enter X axis title', 0.2, 0.2), |
| 75 | + checkTitle('y', 'Click to enter Y axis title', 0.2, 0.2), |
| 76 | + checkTitle('g', 'Click to enter Plot title', 0.2, 0.2) |
| 77 | + ]); |
| 78 | + }) |
| 79 | + .then(done); |
| 80 | + }); |
| 81 | + |
| 82 | + it('has hover effects for blank titles', function(done) { |
| 83 | + Plotly.plot(gd, data, { |
| 84 | + xaxis: {title: ''}, |
| 85 | + yaxis: {title: ''}, |
| 86 | + title: '' |
| 87 | + }, {editable: true}) |
| 88 | + .then(function() { |
| 89 | + return Promise.all([ |
| 90 | + checkTitle('x', 'Click to enter X axis title', 0, 1), |
| 91 | + checkTitle('y', 'Click to enter Y axis title', 0, 1), |
| 92 | + checkTitle('g', 'Click to enter Plot title', 0, 1) |
| 93 | + ]); |
| 94 | + }) |
| 95 | + .then(done); |
| 96 | + }); |
| 97 | + |
| 98 | + it('has no hover effects for titles that used to be blank', function(done) { |
| 99 | + Plotly.plot(gd, data, { |
| 100 | + xaxis: {title: ''}, |
| 101 | + yaxis: {title: ''}, |
| 102 | + title: '' |
| 103 | + }, {editable: true}) |
| 104 | + .then(function() { |
| 105 | + return editTitle('x', 'xaxis.title', 'XXX'); |
| 106 | + }) |
| 107 | + .then(function() { |
| 108 | + return editTitle('y', 'yaxis.title', 'YYY'); |
| 109 | + }) |
| 110 | + .then(function() { |
| 111 | + return editTitle('g', 'title', 'TTT'); |
| 112 | + }) |
| 113 | + .then(function() { |
| 114 | + return Promise.all([ |
| 115 | + checkTitle('x', 'XXX', 1, 1), |
| 116 | + checkTitle('y', 'YYY', 1, 1), |
| 117 | + checkTitle('g', 'TTT', 1, 1) |
| 118 | + ]); |
| 119 | + }) |
| 120 | + .then(done); |
| 121 | + }); |
| 122 | + |
| 123 | +}); |
0 commit comments