Fork off Karma file for in-browser debug
[dygraphs.git] / auto_tests / tests / sanity.js
1 // Copyright (c) 2011 Google, Inc.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21
22 /**
23 * @fileoverview Test cases that ensure Dygraphs works at all.
24 *
25 * @author konigsberg@google.com (Robert Konigsberg)
26 */
27
28 describe("dygraphs-sanity", function() {
29
30 var DEAD_SIMPLE_DATA = 'X,Y\n10,2100';
31 var ZERO_TO_FIFTY = 'X,Y\n10,0\n20,50';
32
33 beforeEach(function() {
34 document.body.innerHTML = "<div id='graph'></div>";
35 });
36
37 /**
38 * The sanity test of sanity tests.
39 */
40 it('testTrue', function() {
41 assert.isTrue(true);
42 });
43
44 /**
45 * Sanity test that ensures the graph element exists.
46 */
47 it('testGraphExists', function() {
48 var graph = document.getElementById("graph");
49 assert.isNotNull(graph);
50 });
51
52 // TODO(konigsberg): Move the following tests to a new package that
53 // tests all kinds of toDomCoords, toDataCoords, toPercent, et cetera.
54
55 /**
56 * A sanity test of sorts, by ensuring the dygraph is created, and
57 * isn't just some piece of junk object.
58 */
59 it('testToString', function() {
60 var graph = document.getElementById("graph");
61 var g = new Dygraph(graph, DEAD_SIMPLE_DATA, {});
62 assert.isNotNull(g);
63 assert.equal("[Dygraph graph]", g.toString());
64 });
65
66 /**
67 * Test that when no valueRange is specified, the y axis range is
68 * adjusted by 10% on top.
69 */
70 it('testYAxisRange_default', function() {
71 var graph = document.getElementById("graph");
72 assert.equal(0, graph.style.length);
73 var g = new Dygraph(graph, ZERO_TO_FIFTY, {});
74 assert.deepEqual([0, 55], g.yAxisRange(0));
75 });
76
77 /**
78 * Test that valueRange matches the y-axis range specifically.
79 */
80 it('testYAxisRange_custom', function() {
81 var graph = document.getElementById("graph");
82 var g = new Dygraph(graph, ZERO_TO_FIFTY, { valueRange: [0,50] });
83 assert.deepEqual([0, 50], g.yAxisRange(0));
84 g.updateOptions({valueRange: null, axes: {y: {valueRange: [10, 40]}}});
85 assert.deepEqual([10, 40], g.yAxisRange(0));
86 });
87
88 /**
89 * Test that valueRange matches the y-axis range specifically.
90 *
91 * This is based on the assumption that 20 pixels are dedicated to the
92 * axis label and tick marks.
93 * TODO(konigsberg): change yAxisLabelWidth to 0 (or 20) and try again.
94 */
95 it('testToDomYCoord', function() {
96 var graph = document.getElementById("graph");
97 var g = new Dygraph(graph, ZERO_TO_FIFTY, { height: 70, valueRange: [0,50] });
98
99 assert.equal(50, g.toDomYCoord(0));
100 assert.equal(0, g.toDomYCoord(50));
101
102 for (var x = 0; x <= 50; x++) {
103 assert.closeTo(50 - x, g.toDomYCoord(x), 0.00001);
104 }
105 g.updateOptions({valueRange: null, axes: {y: {valueRange: [0, 50]}}});
106
107 assert.equal(50, g.toDomYCoord(0));
108 assert.equal(0, g.toDomYCoord(50));
109
110 for (var x = 0; x <= 50; x++) {
111 assert.closeTo(50 - x, g.toDomYCoord(x), 0.00001);
112 }
113 });
114
115 /**
116 * Test that the two-argument form of the constructor (no options) works.
117 */
118 it('testTwoArgumentConstructor', function() {
119 var graph = document.getElementById("graph");
120 new Dygraph(graph, ZERO_TO_FIFTY);
121 });
122
123 // Here is the first of a series of tests that just ensure the graph is drawn
124 // without exception.
125 //TODO(konigsberg): Move to its own test case.
126 it('testFillStack1', function() {
127 var graph = document.getElementById("graph");
128 new Dygraph(graph, ZERO_TO_FIFTY, { stackedGraph: true });
129 });
130
131 it('testFillStack2', function() {
132 var graph = document.getElementById("graph");
133 new Dygraph(graph, ZERO_TO_FIFTY, { stackedGraph: true, fillGraph: true });
134 });
135
136 it('testFillStack3', function() {
137 var graph = document.getElementById("graph");
138 new Dygraph(graph, ZERO_TO_FIFTY, { fillGraph: true });
139 });
140
141 });