Merge pull request #574 from danvk/test-warnings
[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 */
89fdcedb 7describe("pathological-cases", function() {
395e98a3 8
1b7afc93
DV
9var restoreConsole;
10var logs = {};
89fdcedb 11beforeEach(function() {
395e98a3 12 document.body.innerHTML = "<div id='graph'></div>";
1b7afc93 13 restoreConsole = Util.captureConsole(logs);
89fdcedb 14});
395e98a3 15
89fdcedb 16afterEach(function() {
1b7afc93 17 restoreConsole();
89fdcedb 18});
395e98a3 19
89fdcedb 20it('testZeroPoint', function() {
395e98a3
DV
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);
89fdcedb 29});
395e98a3 30
89fdcedb 31it('testOnePoint', function() {
395e98a3
DV
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);
89fdcedb 41});
e0ff43a1 42
89fdcedb 43it('testCombinations', function() {
fa460473
KW
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,
ccecde93
KW
75 xRangePad: 2,
76 yRangePad: 4
fa460473
KW
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);
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
142 var graph = document.getElementById("graph");
143 var g = new Dygraph(graph, data, opts);
89fdcedb 144});
2c623e51 145
89fdcedb 146it('testDivAsString', function() {
2c623e51
RK
147 var data = "X,Y\n" +
148 "1,2\n";
149
747681e6 150 var g = new Dygraph('graph', data, {});
89fdcedb 151});
62054606
DV
152
153
89fdcedb 154it('testConstantSeriesNegative', function() {
62054606
DV
155 var data = "X,Y\n" +
156 "1,-1\n" +
157 "2,-1\n";
158
89fdcedb 159 var g = new Dygraph('graph', data, {});
62054606
DV
160 // This check could be loosened to
161 // g.yAxisRange()[0] < g.yAxisRange()[1] if it breaks in the future.
89fdcedb
DV
162 assert.deepEqual([-1.1, -0.9], g.yAxisRange());
163});
62054606
DV
164
165
89fdcedb 166it('testConstantSeriesNegativeIncludeZero', function() {
62054606
DV
167 var data = "X,Y\n" +
168 "1,-1\n" +
169 "2,-1\n";
170
89fdcedb 171 var g = new Dygraph('graph', data, {includeZero: true});
62054606
DV
172 // This check could be loosened to
173 // g.yAxisRange()[0] < g.yAxisRange()[1] if it breaks in the future.
89fdcedb
DV
174 assert.deepEqual([-1.1, 0], g.yAxisRange());
175});
176
5db9ad5d
DV
177it('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
89fdcedb 191});