consolidating scripts
[dygraphs.git] / auto_tests / tests / plugins_legend.js
1 import Dygraph from '../../src/dygraph';
2 import LegendPlugin from '../../src/plugins/legend';
3 import Util from './Util';
4
5 describe("plugins-legend", function() {
6
7 var graph;
8
9 cleanupAfterEach();
10 beforeEach(function() {
11 var testDiv = document.getElementById('graph');
12 testDiv.innerHTML = "<div id='inner-graph'></div><div id='label'></div>";
13 graph = document.getElementById('inner-graph');
14 });
15
16 it('testLegendEscape', function() {
17 var opts = {
18 width: 480,
19 height: 320
20 };
21 var data = "X,<script>alert('XSS')</script>\n" +
22 "0,-1\n" +
23 "1,0\n" +
24 "2,1\n" +
25 "3,0\n"
26 ;
27
28 var g = new Dygraph(graph, data, opts);
29
30 var legendPlugin = new LegendPlugin();
31 legendPlugin.activate(g);
32 var e = {
33 selectedX: 'selectedX',
34 selectedPoints: [{
35 canvasy: 100,
36 name: "<script>alert('XSS')</script>",
37 yval: 10,
38 }],
39 dygraph: g
40 }
41 legendPlugin.select(e);
42
43 var legendSpan = legendPlugin.legend_div_.querySelector("span b span");
44 assert.equal(legendSpan.innerHTML, "&lt;script&gt;alert('XSS')&lt;/script&gt;");
45 });
46
47
48 it('should let labelsDiv be a string', function() {
49 var labelsDiv = document.getElementById('label');
50 var g = new Dygraph(graph, 'X,Y\n1,2\n', {labelsDiv: 'label'});
51 null
52 g.setSelection(0);
53 assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent));
54 });
55
56 it('should let labelsDiv be an Element', function() {
57 var labelsDiv = document.getElementById('label');
58 var g = new Dygraph(graph, 'X,Y\n1,2\n', { labelsDiv: labelsDiv });
59 assert.isNull(labelsDiv.getAttribute('class')); // dygraph-legend not added.
60 g.setSelection(0);
61 assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent));
62 });
63
64 it('should render dashed patterns', function() {
65 var g = new Dygraph(graph, 'X,Y\n1,2\n', {
66 strokePattern: [5, 5],
67 color: 'red',
68 legend: 'always'
69 });
70
71 // The legend has a dashed line and a label.
72 var legendEl = document.querySelector('.dygraph-legend > span');
73 assert.equal(' Y', legendEl.textContent);
74 var dashEl = document.querySelector('.dygraph-legend > span > div');
75 assert.equal(window.getComputedStyle(dashEl)['border-bottom-color'],
76 'rgb(255, 0, 0)');
77 });
78
79 });