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