Fork off Karma file for in-browser debug
[dygraphs.git] / auto_tests / tests / data_api.js
CommitLineData
fcf37b29
DV
1/**
2 * @fileoverview Tests for data access methods.
3 *
4 * @author danvdk@gmail.com (Dan Vanderkam)
5 */
89fdcedb 6describe("data-api", function() {
fcf37b29 7
319d0361
DV
8var opts, graphDiv;
9
89fdcedb 10beforeEach(function() {
fcf37b29 11 document.body.innerHTML = "<div id='graph'></div>";
319d0361 12 opts = {
fcf37b29
DV
13 width: 480,
14 height: 320
15 };
16
319d0361 17 graphDiv = document.getElementById("graph");
89fdcedb 18});
fcf37b29 19
89fdcedb
DV
20afterEach(function() {
21});
fcf37b29 22
89fdcedb 23it('testBasicAccessors', function() {
319d0361 24 var g = new Dygraph(graphDiv, temperature_data, opts);
fcf37b29 25
89fdcedb
DV
26 assert.equal(365, g.numRows());
27 assert.equal(3, g.numColumns());
fcf37b29
DV
28
29 // 2007-01-01,62,39
89fdcedb
DV
30 assert.equal(62, g.getValue(0, 1));
31 assert.equal(39, g.getValue(0, 2));
fcf37b29
DV
32
33 // 2007-12-31,57,42
89fdcedb
DV
34 assert.equal(57, g.getValue(364, 1));
35 assert.equal(42, g.getValue(364, 2));
36});
fcf37b29
DV
37
38
89fdcedb 39it('testAccessorsCustomBars', function() {
319d0361 40 var g = new Dygraph(graphDiv, data_temp_high_low, {
fcf37b29
DV
41 customBars: true
42 });
43
89fdcedb
DV
44 assert.equal(1070, g.numRows());
45 assert.equal(3, g.numColumns());
fcf37b29
DV
46
47 // 2007-01-01,46;51;56,43;45;48
89fdcedb
DV
48 assert.deepEqual([46, 51, 56], g.getValue(0, 1));
49 assert.deepEqual([43, 45, 48], g.getValue(0, 2));
fcf37b29
DV
50
51 // 2009-12-05,37;42;47 (i.e. missing second column)
89fdcedb
DV
52 assert.deepEqual([37, 42, 47], g.getValue(1069, 1));
53 assert.deepEqual([null, null, null], g.getValue(1069, 2));
54});
fcf37b29
DV
55
56
57// Regression test for #554.
89fdcedb 58it('testGetRowForX', function() {
319d0361 59 var g = new Dygraph(graphDiv, [
fcf37b29
DV
60 "x,y",
61 "1,2",
62 "3,4",
63 "5,6",
64 "7,8",
65 "9,10"
319d0361 66 ].join('\n'), opts);
fcf37b29 67
89fdcedb
DV
68 assert.equal(null, g.getRowForX(0));
69 assert.equal(0, g.getRowForX(1));
70 assert.equal(null, g.getRowForX(2));
71 assert.equal(1, g.getRowForX(3));
72 assert.equal(null, g.getRowForX(4));
73 assert.equal(2, g.getRowForX(5));
74 assert.equal(null, g.getRowForX(6));
75 assert.equal(3, g.getRowForX(7));
76 assert.equal(null, g.getRowForX(8));
77 assert.equal(4, g.getRowForX(9));
78 assert.equal(null, g.getRowForX(10));
79});
fcf37b29
DV
80
81// If there are rows with identical x-values, getRowForX promises that it will
82// return the first one.
89fdcedb 83it('testGetRowForXDuplicates', function() {
319d0361 84 var g = new Dygraph(graphDiv, [
fcf37b29
DV
85 "x,y",
86 "1,2", // 0
87 "1,4", // 1
88 "1,6", // 2
89 "1,8", // 3
90 "1,6", // 4
91 "9,2", // 5
92 "9,4",
93 "9,6",
94 "9,8",
95 "9,10"
319d0361 96 ].join('\n'), opts);
fcf37b29 97
89fdcedb
DV
98 assert.equal(0, g.getRowForX(1));
99 assert.equal(null, g.getRowForX(2));
100 assert.equal(5, g.getRowForX(9));
101});
102
103});