|
| 1 | +var Plotly = require('@lib/index'); |
| 2 | +var Lib = require('@src/lib'); |
| 3 | +var DBLCLICKDELAY = require('@src/plots/cartesian/constants').DBLCLICKDELAY; |
| 4 | + |
| 5 | +var createGraphDiv = require('../assets/create_graph_div'); |
| 6 | +var destroyGraphDiv = require('../assets/destroy_graph_div'); |
| 7 | +var mouseEvent = require('../assets/mouse_event'); |
| 8 | + |
| 9 | + |
| 10 | +describe('select box and lasso', function() { |
| 11 | + var mock = require('@mocks/14.json'); |
| 12 | + |
| 13 | + afterEach(destroyGraphDiv); |
| 14 | + |
| 15 | + function drag(path) { |
| 16 | + var len = path.length; |
| 17 | + |
| 18 | + mouseEvent('mousemove', path[0][0], path[0][1]); |
| 19 | + mouseEvent('mousedown', path[0][0], path[0][1]); |
| 20 | + |
| 21 | + path.slice(1, len).forEach(function(pt) { |
| 22 | + mouseEvent('mousemove', pt[0], pt[1]); |
| 23 | + }); |
| 24 | + |
| 25 | + mouseEvent('mouseup', path[len - 1][0], path[len - 1][1]); |
| 26 | + } |
| 27 | + |
| 28 | + function click(x, y) { |
| 29 | + mouseEvent('mousemove', x, y); |
| 30 | + mouseEvent('mousedown', x, y); |
| 31 | + mouseEvent('mouseup', x, y); |
| 32 | + } |
| 33 | + |
| 34 | + function doubleClick(x, y, cb) { |
| 35 | + click(x, y); |
| 36 | + setTimeout(function() { |
| 37 | + click(x, y); |
| 38 | + cb(); |
| 39 | + }, DBLCLICKDELAY / 2); |
| 40 | + } |
| 41 | + |
| 42 | + describe('select events', function() { |
| 43 | + var mockCopy = Lib.extendDeep({}, mock); |
| 44 | + mockCopy.layout.dragmode = 'select'; |
| 45 | + |
| 46 | + var gd; |
| 47 | + beforeEach(function(done) { |
| 48 | + gd = createGraphDiv(); |
| 49 | + |
| 50 | + Plotly.plot(gd, mockCopy.data, mockCopy.layout) |
| 51 | + .then(done); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should trigger events', function(done) { |
| 55 | + var selectingCnt = 0, |
| 56 | + selectingData; |
| 57 | + gd.on('plotly_selecting', function(data) { |
| 58 | + selectingCnt++; |
| 59 | + selectingData = data; |
| 60 | + }); |
| 61 | + |
| 62 | + var selectedCnt = 0, |
| 63 | + selectedData; |
| 64 | + gd.on('plotly_selected', function(data) { |
| 65 | + selectedCnt++; |
| 66 | + selectedData = data; |
| 67 | + }); |
| 68 | + |
| 69 | + var doubleClickData; |
| 70 | + gd.on('plotly_doubleclick', function(data) { |
| 71 | + doubleClickData = data; |
| 72 | + }); |
| 73 | + |
| 74 | + drag([[100, 200], [150, 200]]); |
| 75 | + |
| 76 | + expect(selectingCnt).toEqual(1); |
| 77 | + expect(selectingData.points).toEqual([{ |
| 78 | + curveNumber: 0, |
| 79 | + pointNumber: 0, |
| 80 | + x: 0.002, |
| 81 | + y: 16.25 |
| 82 | + }, { |
| 83 | + curveNumber: 0, |
| 84 | + pointNumber: 1, |
| 85 | + x: 0.004, |
| 86 | + y: 12.5 |
| 87 | + }]); |
| 88 | + expect(selectingData.range).toEqual({ |
| 89 | + x: [0.0019667582669138295, 0.004546754982054625], |
| 90 | + y: [0.10209191961595454, 24.512223978291406] |
| 91 | + }); |
| 92 | + |
| 93 | + expect(selectedCnt).toEqual(1); |
| 94 | + expect(selectedData.points).toEqual([{ |
| 95 | + curveNumber: 0, |
| 96 | + pointNumber: 0, |
| 97 | + x: 0.002, |
| 98 | + y: 16.25 |
| 99 | + }, { |
| 100 | + curveNumber: 0, |
| 101 | + pointNumber: 1, |
| 102 | + x: 0.004, |
| 103 | + y: 12.5 |
| 104 | + }]); |
| 105 | + expect(selectedData.range).toEqual({ |
| 106 | + x: [0.0019667582669138295, 0.004546754982054625], |
| 107 | + y: [0.10209191961595454, 24.512223978291406] |
| 108 | + }); |
| 109 | + |
| 110 | + doubleClick(250, 200, function() { |
| 111 | + expect(doubleClickData).toBe(null); |
| 112 | + done(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + }); |
| 117 | + |
| 118 | + describe('lasso events', function() { |
| 119 | + var mockCopy = Lib.extendDeep({}, mock); |
| 120 | + mockCopy.layout.dragmode = 'lasso'; |
| 121 | + |
| 122 | + var gd; |
| 123 | + beforeEach(function(done) { |
| 124 | + gd = createGraphDiv(); |
| 125 | + |
| 126 | + Plotly.plot(gd, mockCopy.data, mockCopy.layout) |
| 127 | + .then(done); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should trigger events', function(done) { |
| 131 | + var selectingCnt = 0, |
| 132 | + selectingData; |
| 133 | + gd.on('plotly_selecting', function(data) { |
| 134 | + selectingCnt++; |
| 135 | + selectingData = data; |
| 136 | + }); |
| 137 | + |
| 138 | + var selectedCnt = 0, |
| 139 | + selectedData; |
| 140 | + gd.on('plotly_selected', function(data) { |
| 141 | + selectedCnt++; |
| 142 | + selectedData = data; |
| 143 | + }); |
| 144 | + |
| 145 | + var doubleClickData; |
| 146 | + gd.on('plotly_doubleclick', function(data) { |
| 147 | + doubleClickData = data; |
| 148 | + }); |
| 149 | + |
| 150 | + drag([[331, 178], [333, 246], [350, 250], [343, 176]]); |
| 151 | + |
| 152 | + expect(selectingCnt).toEqual(3); |
| 153 | + expect(selectingData.points).toEqual([{ |
| 154 | + curveNumber: 0, |
| 155 | + pointNumber: 10, |
| 156 | + x: 0.099, |
| 157 | + y: 2.75 |
| 158 | + }]); |
| 159 | + |
| 160 | + expect(selectedCnt).toEqual(1); |
| 161 | + expect(selectedData.points).toEqual([{ |
| 162 | + curveNumber: 0, |
| 163 | + pointNumber: 10, |
| 164 | + x: 0.099, |
| 165 | + y: 2.75 |
| 166 | + }]); |
| 167 | + |
| 168 | + doubleClick(250, 200, function() { |
| 169 | + expect(doubleClickData).toBe(null); |
| 170 | + done(); |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments