Fork off Karma file for in-browser debug
[dygraphs.git] / auto_tests / tests / pathological_cases.js
1 /**
2 * @fileoverview Tests zero and one-point charts.
3 * These don't have to render nicely, they just have to not crash.
4 *
5 * @author dan@dygraphs.com (Dan Vanderkam)
6 */
7 describe("pathological-cases", function() {
8
9 var restoreConsole;
10 var logs = {};
11 beforeEach(function() {
12 document.body.innerHTML = "<div id='graph'></div>";
13 restoreConsole = Util.captureConsole(logs);
14 });
15
16 afterEach(function() {
17 restoreConsole();
18 });
19
20 it('testZeroPoint', function() {
21 var opts = {
22 width: 480,
23 height: 320
24 };
25 var data = "X,Y\n";
26
27 var graph = document.getElementById("graph");
28 var g = new Dygraph(graph, data, opts);
29 });
30
31 it('testOnePoint', function() {
32 var opts = {
33 width: 480,
34 height: 320
35 };
36 var data = "X,Y\n" +
37 "1,2\n";
38
39 var graph = document.getElementById("graph");
40 var g = new Dygraph(graph, data, opts);
41 });
42
43 it('testCombinations', function() {
44 var dataSets = {
45 empty: [],
46 onePoint: [[10, 2]],
47 nanPoint: [[10, NaN]],
48 nanPoints: [[10, NaN], [20, NaN]],
49 multiNan1: [[10, NaN, 2], [20, 3, NaN]],
50 multiNan2: [[10, NaN, 2], [20, NaN, 4]],
51 multiNan3: [[10, NaN, NaN], [20, 3, 4], [30, NaN, NaN]],
52 atZero: [[0, 0]],
53 atZero2: [[0, 0, 0]],
54 negative: [[-10, -1]],
55 acrossZero: [[-10, 1], [10, 2]],
56 normal: [[0,1,9], [10,3,5], [20,2,7], [30,4,3]]
57 };
58
59 var baseOpts = {
60 lines: {},
61 stacked: {
62 stackedGraph: true
63 }
64 };
65
66 var variantOpts = {
67 none: {},
68 avoidMinZero: {
69 avoidMinZero: true,
70 includeZero: true
71 },
72 padded: {
73 includeZero: true,
74 drawAxesAtZero: true,
75 xRangePad: 2,
76 yRangePad: 4
77 }
78 };
79
80 for (var baseName in baseOpts) {
81 var base = baseOpts[baseName];
82 for (var variantName in variantOpts) {
83 var variant = variantOpts[variantName];
84
85 var opts = {
86 width: 300,
87 height: 150,
88 labelsDivWidth: 100,
89 pointSize: 10
90 };
91 for (var key in base) {
92 if (base.hasOwnProperty(key)) opts[key] = base[key];
93 }
94 for (var key in variant) {
95 if (variant.hasOwnProperty(key)) opts[key] = variant[key];
96 }
97
98 var h = document.createElement('h3');
99 h.appendChild(document.createTextNode(baseName + ' ' + variantName));
100 document.body.appendChild(h);
101 for (var dataName in dataSets) {
102 var data = dataSets[dataName];
103
104 var box = document.createElement('fieldset');
105 box.style.display = 'inline-block';
106 var legend = document.createElement('legend');
107 legend.appendChild(document.createTextNode(dataName));
108 box.appendChild(legend);
109 var gdiv = document.createElement('div');
110 gdiv.style.display = 'inline-block';
111 box.appendChild(gdiv);
112 document.body.appendChild(box);
113
114 var cols = data && data[0] ? data[0].length : 0;
115 opts.labels = ['X', 'A', 'B', 'C'].slice(0, cols);
116
117 var g = new Dygraph(gdiv, data, opts);
118
119 if (dataName == 'empty') {
120 assert.deepEqual(logs, {
121 log: [], warn: [],
122 error: ["Can't plot empty data set"]
123 });
124 logs.error = []; // reset
125 } else {
126 assert.deepEqual(logs, {log: [], warn: [], error: []});
127 }
128 }
129 }
130 }
131 });
132
133 it('testNullLegend', function() {
134 var opts = {
135 width: 480,
136 height: 320,
137 labelsDiv: null
138 };
139 var data = "X,Y\n" +
140 "1,2\n";
141
142 var graph = document.getElementById("graph");
143 var g = new Dygraph(graph, data, opts);
144 });
145
146 it('testDivAsString', function() {
147 var data = "X,Y\n" +
148 "1,2\n";
149
150 var g = new Dygraph('graph', data, {});
151 });
152
153
154 it('testConstantSeriesNegative', function() {
155 var data = "X,Y\n" +
156 "1,-1\n" +
157 "2,-1\n";
158
159 var g = new Dygraph('graph', data, {});
160 // This check could be loosened to
161 // g.yAxisRange()[0] < g.yAxisRange()[1] if it breaks in the future.
162 assert.deepEqual([-1.1, -0.9], g.yAxisRange());
163 });
164
165
166 it('testConstantSeriesNegativeIncludeZero', function() {
167 var data = "X,Y\n" +
168 "1,-1\n" +
169 "2,-1\n";
170
171 var g = new Dygraph('graph', data, {includeZero: true});
172 // This check could be loosened to
173 // g.yAxisRange()[0] < g.yAxisRange()[1] if it breaks in the future.
174 assert.deepEqual([-1.1, 0], g.yAxisRange());
175 });
176
177 it('should throw with non-existent divs', function() {
178 var data = "X,Y\n" +
179 "1,-1\n" +
180 "2,1\n";
181
182 assert.throws(function() {
183 new Dygraph(null, data);
184 }, /non-existent div/);
185
186 assert.throws(function() {
187 new Dygraph('non-existent-div-id', data);
188 }, /non-existent div/);
189 });
190
191 });