|
1 | 1 | var dragElement = require('@src/components/dragelement'); |
2 | 2 |
|
| 3 | +var d3 = require('d3'); |
| 4 | +var createGraphDiv = require('../assets/create_graph_div'); |
| 5 | +var destroyGraphDiv = require('../assets/destroy_graph_div'); |
| 6 | +var mouseEvent = require('../assets/mouse_event'); |
| 7 | + |
| 8 | + |
| 9 | +describe('dragElement', function() { |
| 10 | + 'use strict'; |
| 11 | + |
| 12 | + beforeEach(function() { |
| 13 | + this.gd = createGraphDiv(); |
| 14 | + this.hoverlayer = document.createElement('div'); |
| 15 | + this.element = document.createElement('div'); |
| 16 | + |
| 17 | + this.gd.className = 'js-plotly-plot'; |
| 18 | + this.gd._fullLayout = { |
| 19 | + _hoverlayer: d3.select(this.hoverlayer) |
| 20 | + }; |
| 21 | + this.element.innerHTML = 'drag element'; |
| 22 | + |
| 23 | + this.gd.appendChild(this.element); |
| 24 | + this.element.appendChild(this.hoverlayer); |
| 25 | + |
| 26 | + var clientRect = this.element.getBoundingClientRect(); |
| 27 | + this.x = clientRect.left + clientRect.width / 2; |
| 28 | + this.y = clientRect.top + clientRect.height / 2; |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(destroyGraphDiv); |
| 32 | + |
| 33 | + it('should init drag element', function() { |
| 34 | + var options = { element: this.element }; |
| 35 | + dragElement.init(options); |
| 36 | + |
| 37 | + expect(this.element.style.pointerEvents).toEqual('all', 'with pointer event style'); |
| 38 | + expect(this.element.onmousedown).not.toBe(null, 'with on mousedown handler'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should pass event, startX and startY to prepFn on mousedown', function() { |
| 42 | + var args = []; |
| 43 | + var options = { |
| 44 | + element: this.element, |
| 45 | + prepFn: function(event, startX, startY) { |
| 46 | + args = [event, startX, startY]; |
| 47 | + } |
| 48 | + }; |
| 49 | + dragElement.init(options); |
| 50 | + |
| 51 | + mouseEvent('mousedown', this.x, this.y); |
| 52 | + mouseEvent('mouseup', this.x, this.y); |
| 53 | + |
| 54 | + expect(args[0]).not.toBe(null); |
| 55 | + expect(args[1]).toEqual(Math.floor(this.x)); |
| 56 | + expect(args[2]).toEqual(Math.floor(this.y)); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should pass dragged, dx and dy to moveFn on mousemove', function() { |
| 60 | + var args = []; |
| 61 | + var options = { |
| 62 | + element: this.element, |
| 63 | + moveFn: function(dx, dy, dragged) { |
| 64 | + args = [dx, dy, dragged]; |
| 65 | + } |
| 66 | + }; |
| 67 | + dragElement.init(options); |
| 68 | + |
| 69 | + mouseEvent('mousedown', this.x, this.y); |
| 70 | + mouseEvent('mousemove', this.x + 10, this.y + 10); |
| 71 | + mouseEvent('mouseup', this.x, this.y); |
| 72 | + |
| 73 | + expect(args[0]).toEqual(10); |
| 74 | + expect(args[1]).toEqual(10); |
| 75 | + expect(args[2]).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should pass dragged and numClicks to doneFn on mouseup & mouseout', function() { |
| 79 | + var args = []; |
| 80 | + var options = { |
| 81 | + element: this.element, |
| 82 | + doneFn: function(dragged, numClicks) { |
| 83 | + args = [dragged, numClicks]; |
| 84 | + } |
| 85 | + }; |
| 86 | + dragElement.init(options); |
| 87 | + |
| 88 | + mouseEvent('mousedown', this.x, this.y); |
| 89 | + mouseEvent('mouseup', this.x, this.y); |
| 90 | + |
| 91 | + expect(args[0]).toBe(false); |
| 92 | + expect(args[1]).toEqual(1); |
| 93 | + |
| 94 | + mouseEvent('mousedown', this.x, this.y); |
| 95 | + mouseEvent('mousemove', this.x + 10, this.y + 10); |
| 96 | + mouseEvent('mouseout', this.x, this.y); |
| 97 | + |
| 98 | + expect(args[0]).toBe(true); |
| 99 | + expect(args[1]).toEqual(2); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should add a cover slip div to the DOM', function() { |
| 103 | + function countCoverSlip() { |
| 104 | + return d3.selectAll('.dragcover').size(); |
| 105 | + } |
| 106 | + |
| 107 | + var options = { element: this.element }; |
| 108 | + dragElement.init(options); |
| 109 | + |
| 110 | + expect(countCoverSlip()).toEqual(0); |
| 111 | + |
| 112 | + mouseEvent('mousedown', this.x, this.y); |
| 113 | + expect(countCoverSlip()).toEqual(1); |
| 114 | + |
| 115 | + mouseEvent('mousemove', this.x + 10, this.y + 10); |
| 116 | + expect(countCoverSlip()).toEqual(1); |
| 117 | + |
| 118 | + mouseEvent('mouseout', this.x, this.y); |
| 119 | + expect(countCoverSlip()).toEqual(0); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should fire off click event when down/up without dragging', function() { |
| 123 | + var options = { element: this.element }; |
| 124 | + dragElement.init(options); |
| 125 | + |
| 126 | + var mockObj = { |
| 127 | + handleClick: function() {}, |
| 128 | + dummy: function() {} |
| 129 | + }; |
| 130 | + spyOn(mockObj, 'handleClick'); |
| 131 | + spyOn(mockObj, 'dummy'); |
| 132 | + |
| 133 | + this.element.onclick = mockObj.handleClick; |
| 134 | + |
| 135 | + mouseEvent('mousedown', this.x, this.y); |
| 136 | + mouseEvent('mouseup', this.x, this.y); |
| 137 | + |
| 138 | + expect(mockObj.handleClick).toHaveBeenCalled(); |
| 139 | + |
| 140 | + this.element.onclick = mockObj.dummy; |
| 141 | + |
| 142 | + mouseEvent('mousedown', this.x, this.y); |
| 143 | + mouseEvent('mousemove', this.x + 10, this.y + 10); |
| 144 | + mouseEvent('mouseup', this.x, this.y); |
| 145 | + |
| 146 | + expect(mockObj.dummy).not.toHaveBeenCalled(); |
| 147 | + }); |
| 148 | +}); |
3 | 149 |
|
4 | 150 | describe('dragElement.getCursor', function() { |
5 | 151 | 'use strict'; |
|
0 commit comments