|
| 1 | +var Plotly = require('@lib/index'); |
| 2 | +var createGraph = require('../assets/create_graph_div'); |
| 3 | +var destroyGraph = require('../assets/destroy_graph_div'); |
| 4 | +var mock = require('../../image/mocks/legend_scroll.json'); |
| 5 | + |
| 6 | +describe('The legend', function() { |
| 7 | + var gd, |
| 8 | + legend; |
| 9 | + |
| 10 | + describe('when plotted with many traces', function() { |
| 11 | + beforeEach(function() { |
| 12 | + gd = createGraph(); |
| 13 | + Plotly.plot(gd, mock.data, mock.layout); |
| 14 | + legend = document.getElementsByClassName('legend')[0]; |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(destroyGraph); |
| 18 | + |
| 19 | + it('should not exceed plot height', function() { |
| 20 | + var legendHeight = legend.getAttribute('height'), |
| 21 | + plotHeight = gd._fullLayout.height - gd._fullLayout.margin.t - gd._fullLayout.margin.b; |
| 22 | + |
| 23 | + expect(+legendHeight).toBe(plotHeight); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should insert a scrollbar', function() { |
| 27 | + var scrollBar = legend.getElementsByClassName('scrollbar')[0]; |
| 28 | + |
| 29 | + expect(scrollBar).toBeDefined(); |
| 30 | + expect(scrollBar.getAttribute('x')).not.toBe(null); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should scroll when there\'s a wheel event', function() { |
| 34 | + var scrollBox = legend.getElementsByClassName('scrollbox')[0]; |
| 35 | + |
| 36 | + legend.dispatchEvent(scrollTo(100)); |
| 37 | + |
| 38 | + // Compare against -5 because of a scroll factor of 20 |
| 39 | + // ( 100 / 20 === 5 ) |
| 40 | + expect(scrollBox.getAttribute('transform')).toBe('translate(0, -5)'); |
| 41 | + expect(scrollBox.getAttribute('data-scroll')).toBe('-5'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should constrain scrolling to the contents', function() { |
| 45 | + var scrollBox = legend.getElementsByClassName('scrollbox')[0], |
| 46 | + scrollHeight = scrollBox.getBoundingClientRect().height, |
| 47 | + legendHeight = legend.getAttribute('height'), |
| 48 | + maxHeight = legendHeight - scrollHeight - 12; |
| 49 | + |
| 50 | + legend.dispatchEvent(scrollTo(-100)); |
| 51 | + expect(scrollBox.getAttribute('transform')).toBe('translate(0, 0)'); |
| 52 | + |
| 53 | + legend.dispatchEvent(scrollTo(10000)); |
| 54 | + expect(scrollBox.getAttribute('transform')).toBe('translate(0, ' + maxHeight + ')'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should scale the scrollbar movement from top to bottom', function() { |
| 58 | + var scrollBar = legend.getElementsByClassName('scrollbar')[0], |
| 59 | + legendHeight = legend.getAttribute('height'); |
| 60 | + |
| 61 | + // The scrollbar is 20px tall and has 4px margins |
| 62 | + |
| 63 | + legend.dispatchEvent(scrollTo(-1000)); |
| 64 | + expect(+scrollBar.getAttribute('y')).toBe(4); |
| 65 | + |
| 66 | + legend.dispatchEvent(scrollTo(10000)); |
| 67 | + expect(+scrollBar.getAttribute('y')).toBe(legendHeight - 4 - 20); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('when plotted with few traces', function() { |
| 72 | + var gd; |
| 73 | + |
| 74 | + beforeEach(function() { |
| 75 | + gd = createGraph(); |
| 76 | + Plotly.plot(gd, [{ x: [1,2,3], y: [2,3,4], name: 'Test' }], {}); |
| 77 | + }); |
| 78 | + |
| 79 | + afterEach(destroyGraph); |
| 80 | + |
| 81 | + it('should not display the scrollbar', function() { |
| 82 | + var scrollBar = document.getElementsByClassName('scrollbar')[0]; |
| 83 | + |
| 84 | + expect(scrollBar).toBeUndefined(); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | + |
| 90 | +function scrollTo(delta) { |
| 91 | + return new WheelEvent('wheel', { deltaY: delta }); |
| 92 | +} |
0 commit comments