fix disappearing annotations bug
[dygraphs.git] / auto_tests / tests / annotations.js
1 /**
2 * @fileoverview Tests relating to annotations
3 *
4 * @author danvk@google.com (Dan Vanderkam)
5 */
6 var annotationsTestCase = TestCase("annotations");
7
8 annotationsTestCase.prototype.setUp = function() {
9 document.body.innerHTML = "<div id='graph'></div>";
10 };
11
12 annotationsTestCase.prototype.tearDown = function() {
13 };
14
15 annotationsTestCase.prototype.testAnnotationsDrawn = function() {
16 var opts = {
17 width: 480,
18 height: 320
19 };
20 var data = "X,Y\n" +
21 "0,-1\n" +
22 "1,0\n" +
23 "2,1\n" +
24 "3,0\n"
25 ;
26
27 var graph = document.getElementById("graph");
28 var g = new Dygraph(graph, data, opts);
29 g.setAnnotations([
30 {
31 series: 'Y',
32 x: 1,
33 shortText: 'A',
34 text: 'Long A',
35 cssClass: 'ann1'
36 },
37 {
38 series: 'Y',
39 x: 2,
40 shortText: 'B',
41 text: 'Long B',
42 cssClass: 'ann2'
43 }
44 ]);
45
46 assertEquals(2, g.annotations().length);
47 var a1 = document.getElementsByClassName('ann1');
48 assertEquals(1, a1.length);
49 a1 = a1[0];
50 assertEquals('A', a1.textContent);
51
52 var a2 = document.getElementsByClassName('ann2');
53 assertEquals(1, a2.length);
54 a2 = a2[0];
55 assertEquals('B', a2.textContent);
56 };
57
58 // Some errors that should be flagged:
59 // 1. Invalid series name (e.g. 'X' or 'non-existent')
60 // 2. Passing a string as 'x' instead of a number (e.g. x: '1')
61
62 annotationsTestCase.prototype.testAnnotationsDontDisappearOnResize = function() {
63 var opts = {
64 };
65 var data = "X,Y\n" +
66 "0,-1\n" +
67 "1,0\n" +
68 "2,1\n" +
69 "3,0\n"
70 ;
71
72 var graph = document.getElementById("graph");
73 var g = new Dygraph(graph, data, opts);
74 g.setAnnotations([
75 {
76 series: 'Y',
77 x: 1,
78 shortText: 'A',
79 text: 'Long A',
80 cssClass: 'ann1'
81 }
82 ]);
83
84 // Check that it displays at all
85 assertEquals(1, g.annotations().length);
86 var a1 = document.getElementsByClassName('ann1');
87 assertEquals(1, a1.length);
88 a1 = a1[0];
89 assertEquals('A', a1.textContent);
90
91 // ... and that resizing doesn't kill it.
92 g.resize(400, 300);
93 assertEquals(1, g.annotations().length);
94 var a1 = document.getElementsByClassName('ann1');
95 assertEquals(1, a1.length);
96 a1 = a1[0];
97 assertEquals('A', a1.textContent);
98 };