2.0.0 release fixes (#815)
[dygraphs.git] / auto_tests / tests / pathological_cases.js
CommitLineData
395e98a3
DV
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 */
e8c70e4e
DV
7
8import Dygraph from '../../src/dygraph';
9import Util from './Util';
10
89fdcedb 11describe("pathological-cases", function() {
395e98a3 12
e8c70e4e
DV
13cleanupAfterEach();
14
1b7afc93
DV
15var restoreConsole;
16var logs = {};
89fdcedb 17beforeEach(function() {
1b7afc93 18 restoreConsole = Util.captureConsole(logs);
89fdcedb 19});
395e98a3 20
89fdcedb 21afterEach(function() {
1b7afc93 22 restoreConsole();
89fdcedb 23});
395e98a3 24
e8c70e4e
DV
25var graph = document.getElementById("graph");
26
89fdcedb 27it('testZeroPoint', function() {
395e98a3
DV
28 var opts = {
29 width: 480,
30 height: 320
31 };
32 var data = "X,Y\n";
33
395e98a3 34 var g = new Dygraph(graph, data, opts);
89fdcedb 35});
395e98a3 36
89fdcedb 37it('testOnePoint', function() {
395e98a3
DV
38 var opts = {
39 width: 480,
40 height: 320
41 };
42 var data = "X,Y\n" +
43 "1,2\n";
44
395e98a3 45 var g = new Dygraph(graph, data, opts);
89fdcedb 46});
e0ff43a1 47
89fdcedb 48it('testCombinations', function() {
fa460473
KW
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: {},
fa460473
KW
73 padded: {
74 includeZero: true,
75 drawAxesAtZero: true,
ccecde93
KW
76 xRangePad: 2,
77 yRangePad: 4
fa460473
KW
78 }
79 };
80
81 for (var baseName in baseOpts) {
82 var base = baseOpts[baseName];
83 for (var variantName in variantOpts) {
84 var variant = variantOpts[variantName];
85
86 var opts = {
87 width: 300,
88 height: 150,
fa460473
KW
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));
e8c70e4e 100 graph.appendChild(h);
fa460473
KW
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);
e8c70e4e 112 graph.appendChild(box);
fa460473
KW
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);
1b7afc93
DV
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 }
fa460473
KW
128 }
129 }
130 }
89fdcedb 131});
fa460473 132
89fdcedb 133it('testNullLegend', function() {
e0ff43a1
DV
134 var opts = {
135 width: 480,
136 height: 320,
137 labelsDiv: null
138 };
139 var data = "X,Y\n" +
140 "1,2\n";
141
e0ff43a1 142 var g = new Dygraph(graph, data, opts);
89fdcedb 143});
2c623e51 144
89fdcedb 145it('testDivAsString', function() {
2c623e51
RK
146 var data = "X,Y\n" +
147 "1,2\n";
148
747681e6 149 var g = new Dygraph('graph', data, {});
89fdcedb 150});
62054606
DV
151
152
89fdcedb 153it('testConstantSeriesNegative', function() {
62054606
DV
154 var data = "X,Y\n" +
155 "1,-1\n" +
156 "2,-1\n";
157
89fdcedb 158 var g = new Dygraph('graph', data, {});
62054606
DV
159 // This check could be loosened to
160 // g.yAxisRange()[0] < g.yAxisRange()[1] if it breaks in the future.
89fdcedb
DV
161 assert.deepEqual([-1.1, -0.9], g.yAxisRange());
162});
62054606
DV
163
164
89fdcedb 165it('testConstantSeriesNegativeIncludeZero', function() {
62054606
DV
166 var data = "X,Y\n" +
167 "1,-1\n" +
168 "2,-1\n";
169
89fdcedb 170 var g = new Dygraph('graph', data, {includeZero: true});
62054606
DV
171 // This check could be loosened to
172 // g.yAxisRange()[0] < g.yAxisRange()[1] if it breaks in the future.
89fdcedb
DV
173 assert.deepEqual([-1.1, 0], g.yAxisRange());
174});
175
5db9ad5d
DV
176it('should throw with non-existent divs', function() {
177 var data = "X,Y\n" +
178 "1,-1\n" +
179 "2,1\n";
180
181 assert.throws(function() {
182 new Dygraph(null, data);
183 }, /non-existent div/);
184
185 assert.throws(function() {
186 new Dygraph('non-existent-div-id', data);
187 }, /non-existent div/);
188});
189
89fdcedb 190});