Tweaks to get all tests passing
[dygraphs.git] / auto_tests / tests / sanity.js
CommitLineData
718ad8e2 1// Copyright (c) 2011 Google, Inc.
644eff8b
RK
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 */
7165f97b
RK
27var DEAD_SIMPLE_DATA = [[ 10, 2100 ]];
28var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]];
644eff8b 29
89fdcedb 30describe("dygraphs-sanity", function() {
644eff8b 31
89fdcedb 32beforeEach(function() {
644eff8b 33 document.body.innerHTML = "<div id='graph'></div>";
89fdcedb 34});
644eff8b
RK
35
36/**
37 * The sanity test of sanity tests.
38 */
89fdcedb
DV
39it('testTrue', function() {
40 assert.isTrue(true);
41});
644eff8b
RK
42
43/**
44 * Sanity test that ensures the graph element exists.
45 */
89fdcedb 46it('testGraphExists', function() {
644eff8b 47 var graph = document.getElementById("graph");
89fdcedb
DV
48 assert.isNotNull(graph);
49});
644eff8b 50
7165f97b
RK
51// TODO(konigsberg): Move the following tests to a new package that
52// tests all kinds of toDomCoords, toDataCoords, toPercent, et cetera.
53
644eff8b
RK
54/**
55 * A sanity test of sorts, by ensuring the dygraph is created, and
56 * isn't just some piece of junk object.
57 */
89fdcedb 58it('testToString', function() {
644eff8b
RK
59 var graph = document.getElementById("graph");
60 var g = new Dygraph(graph, DEAD_SIMPLE_DATA, {});
89fdcedb
DV
61 assert.isNotNull(g);
62 assert.equal("[Dygraph graph]", g.toString());
63});
644eff8b
RK
64
65/**
66 * Test that when no valueRange is specified, the y axis range is
67 * adjusted by 10% on top.
68 */
89fdcedb 69it('testYAxisRange_default', function() {
644eff8b 70 var graph = document.getElementById("graph");
89fdcedb 71 assert.equal(0, graph.style.length);
644eff8b 72 var g = new Dygraph(graph, ZERO_TO_FIFTY, {});
89fdcedb
DV
73 assert.deepEqual([0, 55], g.yAxisRange(0));
74});
644eff8b
RK
75
76/**
77 * Test that valueRange matches the y-axis range specifically.
78 */
89fdcedb 79it('testYAxisRange_custom', function() {
644eff8b
RK
80 var graph = document.getElementById("graph");
81 var g = new Dygraph(graph, ZERO_TO_FIFTY, { valueRange: [0,50] });
89fdcedb 82 assert.deepEqual([0, 50], g.yAxisRange(0));
4dd0ac55 83 g.updateOptions({valueRange: null, axes: {y: {valueRange: [10, 40]}}});
89fdcedb
DV
84 assert.deepEqual([10, 40], g.yAxisRange(0));
85});
644eff8b 86
7165f97b 87/**
644eff8b
RK
88 * Test that valueRange matches the y-axis range specifically.
89 *
90 * This is based on the assumption that 20 pixels are dedicated to the
91 * axis label and tick marks.
92 * TODO(konigsberg): change yAxisLabelWidth to 0 (or 20) and try again.
93 */
89fdcedb 94it('testToDomYCoord', function() {
644eff8b
RK
95 var graph = document.getElementById("graph");
96 var g = new Dygraph(graph, ZERO_TO_FIFTY, { height: 70, valueRange: [0,50] });
97
89fdcedb
DV
98 assert.equal(50, g.toDomYCoord(0));
99 assert.equal(0, g.toDomYCoord(50));
4dd0ac55
RV
100
101 for (var x = 0; x <= 50; x++) {
dc910fce 102 assert.closeTo(50 - x, g.toDomYCoord(x), 0.00001);
4dd0ac55
RV
103 }
104 g.updateOptions({valueRange: null, axes: {y: {valueRange: [0, 50]}}});
105
89fdcedb
DV
106 assert.equal(50, g.toDomYCoord(0));
107 assert.equal(0, g.toDomYCoord(50));
4dd0ac55 108
644eff8b 109 for (var x = 0; x <= 50; x++) {
dc910fce 110 assert.closeTo(50 - x, g.toDomYCoord(x), 0.00001);
644eff8b 111 }
89fdcedb 112});
e0ca6cd3 113
758a629f
DV
114/**
115 * Test that the two-argument form of the constructor (no options) works.
116 */
89fdcedb 117it('testTwoArgumentConstructor', function() {
758a629f
DV
118 var graph = document.getElementById("graph");
119 new Dygraph(graph, ZERO_TO_FIFTY);
89fdcedb 120});
758a629f 121
e0ca6cd3
RK
122// Here is the first of a series of tests that just ensure the graph is drawn
123// without exception.
124//TODO(konigsberg): Move to its own test case.
89fdcedb 125it('testFillStack1', function() {
e0ca6cd3
RK
126 var graph = document.getElementById("graph");
127 new Dygraph(graph, ZERO_TO_FIFTY, { stackedGraph: true });
89fdcedb 128});
e0ca6cd3 129
89fdcedb 130it('testFillStack2', function() {
e0ca6cd3
RK
131 var graph = document.getElementById("graph");
132 new Dygraph(graph, ZERO_TO_FIFTY, { stackedGraph: true, fillGraph: true });
89fdcedb 133});
e0ca6cd3 134
89fdcedb 135it('testFillStack3', function() {
e0ca6cd3
RK
136 var graph = document.getElementById("graph");
137 new Dygraph(graph, ZERO_TO_FIFTY, { fillGraph: true });
89fdcedb
DV
138});
139
140});