1 // Copyright (c) 2011 Google, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
23 * @fileoverview Test cases that ensure Dygraphs works at all.
25 * @author konigsberg@google.com (Robert Konigsberg)
28 describe("dygraphs-sanity", function() {
30 var DEAD_SIMPLE_DATA
= 'X,Y\n10,2100';
31 var ZERO_TO_FIFTY
= 'X,Y\n10,0\n20,50';
33 beforeEach(function() {
34 document
.body
.innerHTML
= "<div id='graph'></div>";
38 * The sanity test of sanity tests.
40 it('testTrue', function() {
45 * Sanity test that ensures the graph element exists.
47 it('testGraphExists', function() {
48 var graph
= document
.getElementById("graph");
49 assert
.isNotNull(graph
);
52 // TODO(konigsberg): Move the following tests to a new package that
53 // tests all kinds of toDomCoords, toDataCoords, toPercent, et cetera.
56 * A sanity test of sorts, by ensuring the dygraph is created, and
57 * isn't just some piece of junk object.
59 it('testToString', function() {
60 var graph
= document
.getElementById("graph");
61 var g
= new Dygraph(graph
, DEAD_SIMPLE_DATA
, {});
63 assert
.equal("[Dygraph graph]", g
.toString());
67 * Test that when no valueRange is specified, the y axis range is
68 * adjusted by 10% on top.
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));
78 * Test that valueRange matches the y-axis range specifically.
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));
89 * Test that valueRange matches the y-axis range specifically.
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.
95 it('testToDomYCoord', function() {
96 var graph
= document
.getElementById("graph");
97 var g
= new Dygraph(graph
, ZERO_TO_FIFTY
, { height
: 70, valueRange
: [0,50] });
99 assert
.equal(50, g
.toDomYCoord(0));
100 assert
.equal(0, g
.toDomYCoord(50));
102 for (var x
= 0; x
<= 50; x
++) {
103 assert
.closeTo(50 - x
, g
.toDomYCoord(x
), 0.00001);
105 g
.updateOptions({valueRange
: null, axes
: {y
: {valueRange
: [0, 50]}}});
107 assert
.equal(50, g
.toDomYCoord(0));
108 assert
.equal(0, g
.toDomYCoord(50));
110 for (var x
= 0; x
<= 50; x
++) {
111 assert
.closeTo(50 - x
, g
.toDomYCoord(x
), 0.00001);
116 * Test that the two-argument form of the constructor (no options) works.
118 it('testTwoArgumentConstructor', function() {
119 var graph
= document
.getElementById("graph");
120 new Dygraph(graph
, ZERO_TO_FIFTY
);
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 });
131 it('testFillStack2', function() {
132 var graph
= document
.getElementById("graph");
133 new Dygraph(graph
, ZERO_TO_FIFTY
, { stackedGraph
: true, fillGraph
: true });
136 it('testFillStack3', function() {
137 var graph
= document
.getElementById("graph");
138 new Dygraph(graph
, ZERO_TO_FIFTY
, { fillGraph
: true });