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