|
| 1 | +var Plotly = require('@lib/index'); |
| 2 | +var Images = require('@src/components/images'); |
| 3 | +var createGraphDiv = require('../assets/create_graph_div'); |
| 4 | +var destroyGraphDiv = require('../assets/destroy_graph_div'); |
| 5 | +var mouseEvent = require('../assets/mouse_event'); |
| 6 | + |
| 7 | +describe('Layout images', function() { |
| 8 | + |
| 9 | + describe('supplyLayoutDefaults', function() { |
| 10 | + |
| 11 | + var layoutIn, |
| 12 | + layoutOut; |
| 13 | + |
| 14 | + beforeEach(function() { |
| 15 | + layoutIn = { images: [] }; |
| 16 | + layoutOut = {}; |
| 17 | + }); |
| 18 | + |
| 19 | + it('should reject when there is no `source`', function() { |
| 20 | + layoutIn.images[0] = { opacity: 0.5, width: 0.2, height: 0.2 }; |
| 21 | + |
| 22 | + Images.supplyLayoutDefaults(layoutIn, layoutOut); |
| 23 | + |
| 24 | + expect(layoutOut.images.length).toEqual(0); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should coerce the correct defaults', function() { |
| 28 | + layoutIn.images[0] = { source: 'http://www.someimagesource.com' }; |
| 29 | + |
| 30 | + var expected = { |
| 31 | + source: 'http://www.someimagesource.com', |
| 32 | + layer: 'above', |
| 33 | + x: 0, |
| 34 | + y: 0, |
| 35 | + xanchor: 'left', |
| 36 | + yanchor: 'top', |
| 37 | + width: 0, |
| 38 | + height: 0, |
| 39 | + sizing: 'contain', |
| 40 | + opacity: 1, |
| 41 | + xref: 'paper', |
| 42 | + yref: 'paper' |
| 43 | + }; |
| 44 | + |
| 45 | + Images.supplyLayoutDefaults(layoutIn, layoutOut); |
| 46 | + |
| 47 | + expect(layoutOut.images[0]).toEqual(expected); |
| 48 | + }); |
| 49 | + |
| 50 | + }); |
| 51 | + |
| 52 | + describe('drawing', function() { |
| 53 | + |
| 54 | + var gd, |
| 55 | + data = [{ x: [1,2,3], y: [1,2,3] }]; |
| 56 | + |
| 57 | + beforeEach(function() { |
| 58 | + gd = createGraphDiv(); |
| 59 | + }); |
| 60 | + |
| 61 | + afterEach(destroyGraphDiv); |
| 62 | + |
| 63 | + it('should draw images on the right layers', function() { |
| 64 | + |
| 65 | + var layer; |
| 66 | + |
| 67 | + Plotly.plot(gd, data, { images: [{ |
| 68 | + source: 'imageabove', |
| 69 | + layer: 'above' |
| 70 | + }]}); |
| 71 | + |
| 72 | + layer = gd._fullLayout._imageUpperLayer; |
| 73 | + expect(layer.length).toBe(1); |
| 74 | + |
| 75 | + destroyGraphDiv(); |
| 76 | + gd = createGraphDiv(); |
| 77 | + Plotly.plot(gd, data, { images: [{ |
| 78 | + source: 'imagebelow', |
| 79 | + layer: 'below' |
| 80 | + }]}); |
| 81 | + |
| 82 | + layer = gd._fullLayout._imageLowerLayer; |
| 83 | + expect(layer.length).toBe(1); |
| 84 | + |
| 85 | + destroyGraphDiv(); |
| 86 | + gd = createGraphDiv(); |
| 87 | + Plotly.plot(gd, data, { images: [{ |
| 88 | + source: 'imagesubplot', |
| 89 | + layer: 'below', |
| 90 | + xref: 'x', |
| 91 | + yref: 'y' |
| 92 | + }]}); |
| 93 | + |
| 94 | + layer = gd._fullLayout._imageSubplotLayer; |
| 95 | + expect(layer.length).toBe(1); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('with anchors and sizing', function() { |
| 99 | + |
| 100 | + function testAspectRatio(xAnchor, yAnchor, sizing, expected) { |
| 101 | + var anchorName = xAnchor + yAnchor; |
| 102 | + Plotly.plot(gd, data, { images: [{ |
| 103 | + source: anchorName, |
| 104 | + xanchor: xAnchor, |
| 105 | + yanchor: yAnchor, |
| 106 | + sizing: sizing |
| 107 | + }]}); |
| 108 | + |
| 109 | + var image = Plotly.d3.select('[href="' + anchorName + '"]'), |
| 110 | + parValue = image.attr('preserveAspectRatio'); |
| 111 | + |
| 112 | + expect(parValue).toBe(expected); |
| 113 | + } |
| 114 | + |
| 115 | + it('should work for center middle', function() { |
| 116 | + testAspectRatio('center', 'middle', undefined, 'xMidYMid'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should work for left top', function() { |
| 120 | + testAspectRatio('left', 'top', undefined, 'xMinYMin'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should work for right bottom', function() { |
| 124 | + testAspectRatio('right', 'bottom', undefined, 'xMaxYMax'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should work for stretch sizing', function() { |
| 128 | + testAspectRatio('middle', 'center', 'stretch', 'none'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should work for fill sizing', function() { |
| 132 | + testAspectRatio('invalid', 'invalid', 'fill', 'xMinYMin slice'); |
| 133 | + }); |
| 134 | + |
| 135 | + }); |
| 136 | + |
| 137 | + }); |
| 138 | + |
| 139 | + describe('when the plot is dragged', function() { |
| 140 | + var gd, |
| 141 | + data = [{ x: [1,2,3], y: [1,2,3] }]; |
| 142 | + |
| 143 | + beforeEach(function() { |
| 144 | + gd = createGraphDiv(); |
| 145 | + }); |
| 146 | + |
| 147 | + afterEach(destroyGraphDiv); |
| 148 | + |
| 149 | + it('should not move when referencing the paper', function(done) { |
| 150 | + var source = 'http://www.placekitten.com/200', |
| 151 | + image = { |
| 152 | + source: source, |
| 153 | + xref: 'paper', |
| 154 | + yref: 'paper', |
| 155 | + x: 0, |
| 156 | + y: 0, |
| 157 | + width: 0.1, |
| 158 | + height: 0.1 |
| 159 | + }; |
| 160 | + |
| 161 | + Plotly.plot(gd, data, { |
| 162 | + images: [image], |
| 163 | + dragmode: 'pan', |
| 164 | + width: 600, |
| 165 | + height: 400 |
| 166 | + }).then(function() { |
| 167 | + var img = Plotly.d3.select('[href="' + source + '"]').node(), |
| 168 | + oldPos = img.getBoundingClientRect(); |
| 169 | + |
| 170 | + mouseEvent('mousedown', 250, 200); |
| 171 | + mouseEvent('mousemove', 300, 250); |
| 172 | + |
| 173 | + var newPos = img.getBoundingClientRect(); |
| 174 | + |
| 175 | + expect(newPos.left).toBe(oldPos.left); |
| 176 | + expect(newPos.top).toBe(oldPos.top); |
| 177 | + |
| 178 | + mouseEvent('mouseup', 300, 250); |
| 179 | + }).then(done); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should move when referencing axes', function(done) { |
| 183 | + var source = 'http://www.placekitten.com/200', |
| 184 | + image = { |
| 185 | + source: source, |
| 186 | + xref: 'x', |
| 187 | + yref: 'y', |
| 188 | + x: 2, |
| 189 | + y: 2, |
| 190 | + width: 1, |
| 191 | + height: 1 |
| 192 | + }; |
| 193 | + |
| 194 | + Plotly.plot(gd, data, { |
| 195 | + images: [image], |
| 196 | + dragmode: 'pan', |
| 197 | + width: 600, |
| 198 | + height: 400 |
| 199 | + }).then(function() { |
| 200 | + var img = Plotly.d3.select('[href="' + source + '"]').node(), |
| 201 | + oldPos = img.getBoundingClientRect(); |
| 202 | + |
| 203 | + mouseEvent('mousedown', 250, 200); |
| 204 | + mouseEvent('mousemove', 300, 250); |
| 205 | + |
| 206 | + var newPos = img.getBoundingClientRect(); |
| 207 | + |
| 208 | + expect(newPos.left).toBe(oldPos.left + 50); |
| 209 | + expect(newPos.top).toBe(oldPos.top + 50); |
| 210 | + |
| 211 | + mouseEvent('mouseup', 300, 250); |
| 212 | + }).then(done); |
| 213 | + }); |
| 214 | + |
| 215 | + }); |
| 216 | + |
| 217 | +}); |
0 commit comments