X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2Fplugins_legend.js;h=e0384c84d453468b08530e84ef27020e11dcaa69;hb=91bd611cb031bf1f3d39650e3109135fd0e78791;hp=3288329fc60c1169be9782559b44847193334d50;hpb=192d58e7c9fab1ae32da4a2dac723955f1639a54;p=dygraphs.git diff --git a/auto_tests/tests/plugins_legend.js b/auto_tests/tests/plugins_legend.js index 3288329..e0384c8 100644 --- a/auto_tests/tests/plugins_legend.js +++ b/auto_tests/tests/plugins_legend.js @@ -1,18 +1,19 @@ -/** - * @fileoverview FILL THIS IN - * - * @author akiya.mizukoshi@gmail.com (Akiyah) - */ -var pluginsLegendTestCase = TestCase("plugins-legend"); +import Dygraph from '../../src/dygraph'; +import LegendPlugin from '../../src/plugins/legend'; +import Util from './Util'; -pluginsLegendTestCase.prototype.setUp = function() { - document.body.innerHTML = "
"; -}; +describe("plugins-legend", function() { -pluginsLegendTestCase.prototype.tearDown = function() { -}; +var graph; -pluginsLegendTestCase.prototype.testLegendEscape = function() { +cleanupAfterEach(); +beforeEach(function() { + var testDiv = document.getElementById('graph'); + testDiv.innerHTML = "
"; + graph = document.getElementById('inner-graph'); +}); + +it('testLegendEscape', function() { var opts = { width: 480, height: 320 @@ -24,10 +25,9 @@ pluginsLegendTestCase.prototype.testLegendEscape = function() { "3,0\n" ; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - var legendPlugin = new Dygraph.Plugins.Legend(); + var legendPlugin = new LegendPlugin(); legendPlugin.activate(g); var e = { selectedX: 'selectedX', @@ -40,7 +40,77 @@ pluginsLegendTestCase.prototype.testLegendEscape = function() { } legendPlugin.select(e); - var legendSpan = $(legendPlugin.legend_div_).find("span b span"); - assertEquals("<script>alert('XSS')</script>", legendSpan.html()); -}; + var legendSpan = legendPlugin.legend_div_.querySelector("span b span"); + assert.equal(legendSpan.innerHTML, "<script>alert('XSS')</script>"); +}); + + +it('should let labelsDiv be a string', function() { + var labelsDiv = document.getElementById('label'); + var g = new Dygraph(graph, 'X,Y\n1,2\n', {labelsDiv: 'label'}); +null + g.setSelection(0); + assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent)); +}); + +it('should let labelsDiv be an Element', function() { + var labelsDiv = document.getElementById('label'); + var g = new Dygraph(graph, 'X,Y\n1,2\n', { labelsDiv: labelsDiv }); + assert.isNull(labelsDiv.getAttribute('class')); // dygraph-legend not added. + g.setSelection(0); + assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent)); +}); + +it('should render dashed patterns', function() { + var g = new Dygraph(graph, 'X,Y\n1,2\n', { + strokePattern: [5, 5], + color: 'red', + legend: 'always' + }); + + // The legend has a dashed line and a label. + var legendEl = document.querySelector('.dygraph-legend > span'); + assert.equal(' Y', legendEl.textContent); + var dashEl = document.querySelector('.dygraph-legend > span > div'); + assert.equal(window.getComputedStyle(dashEl)['border-bottom-color'], + 'rgb(255, 0, 0)'); +}); + +it('should use a legendFormatter', function() { + var calls = []; + var g = new Dygraph(graph, 'X,Y\n1,2\n', { + color: 'red', + legend: 'always', + legendFormatter: function(data) { + calls.push(data); + // Note: can't check against `g` because it's not defined yet. + assert(this.toString().indexOf('Dygraph') >= 0); + return ''; + } + }); + + assert(calls.length == 1); // legend for no selected points + g.setSelection(0); + assert(calls.length == 2); // legend with selected points + g.clearSelection(); + assert(calls.length == 3); + + assert.equal(calls[0].x, undefined); + assert.equal(calls[1].x, 1); + assert.equal(calls[1].xHTML, '1'); + assert.equal(calls[2].x, undefined); + + assert.equal(calls[0].series.length, 1); + assert.equal(calls[1].series.length, 1); + assert.equal(calls[2].series.length, 1); + + assert.equal(calls[0].series[0].y, undefined); + assert.equal(calls[1].series[0].label, 'Y'); + assert.equal(calls[1].series[0].labelHTML, 'Y'); + assert.equal(calls[1].series[0].y, 2); + assert.equal(calls[1].series[0].yHTML, '2'); + assert.equal(calls[1].series[0].isVisible, true); + assert.equal(calls[2].series[0].y, undefined); +}); +});