| 1 | /** |
| 2 | * @fileoverview Tests for data formats. |
| 3 | * |
| 4 | * @author konigsberg@google.com (Robert Konigsberg) |
| 5 | */ |
| 6 | |
| 7 | import Dygraph from '../../src/dygraph'; |
| 8 | |
| 9 | describe("formats", function() { |
| 10 | |
| 11 | cleanupAfterEach(); |
| 12 | |
| 13 | var dataString = |
| 14 | "X,Y\n" + |
| 15 | "0,-1\n" + |
| 16 | "1,0\n" + |
| 17 | "2,1\n" + |
| 18 | "3,0\n"; |
| 19 | |
| 20 | var dataArray = |
| 21 | [[0,-1], |
| 22 | [1,0], |
| 23 | [2,1], |
| 24 | [3,0]]; |
| 25 | var BASE_OPTS = {labels: ['X', 'Y']}; |
| 26 | |
| 27 | it('testCsv', function() { |
| 28 | var data = dataString; |
| 29 | var graph = document.getElementById("graph"); |
| 30 | var g = new Dygraph(graph, data, {}); |
| 31 | assertData(g); |
| 32 | }); |
| 33 | |
| 34 | it('testArray', function() { |
| 35 | var data = dataArray; |
| 36 | var graph = document.getElementById("graph"); |
| 37 | var g = new Dygraph(graph, data, BASE_OPTS); |
| 38 | assertData(g); |
| 39 | }); |
| 40 | |
| 41 | it('testFunctionReturnsCsv', function() { |
| 42 | var data = function() { return dataString; }; |
| 43 | |
| 44 | var graph = document.getElementById("graph"); |
| 45 | var g = new Dygraph(graph, data, {}); |
| 46 | assertData(g); |
| 47 | }); |
| 48 | |
| 49 | it('testFunctionDefinesArray', function() { |
| 50 | var array = dataArray; |
| 51 | var data = function() { return array; } |
| 52 | |
| 53 | var graph = document.getElementById("graph"); |
| 54 | var g = new Dygraph(graph, data, BASE_OPTS); |
| 55 | assertData(g); |
| 56 | }); |
| 57 | |
| 58 | it('testXValueParser', function() { |
| 59 | var data = |
| 60 | "X,Y\n" + |
| 61 | "d,-1\n" + |
| 62 | "e,0\n" + |
| 63 | "f,1\n" + |
| 64 | "g,0\n"; |
| 65 | |
| 66 | var graph = document.getElementById("graph"); |
| 67 | var g = new Dygraph(graph, data, { |
| 68 | xValueParser : function(str) { |
| 69 | assert.equal(1, str.length); |
| 70 | return str.charCodeAt(0) - "a".charCodeAt(0); |
| 71 | } |
| 72 | }); |
| 73 | |
| 74 | assert.equal(3, g.getValue(0, 0)); |
| 75 | assert.equal(4, g.getValue(1, 0)); |
| 76 | assert.equal(5, g.getValue(2, 0)); |
| 77 | assert.equal(6, g.getValue(3, 0)); |
| 78 | }); |
| 79 | |
| 80 | it('should throw on strings in native format', () => { |
| 81 | assert.throws(() => { |
| 82 | new Dygraph('graph', [['1', '10'], ['2', '20']]) |
| 83 | }, /expected number or date/i); |
| 84 | |
| 85 | assert.throws(() => { |
| 86 | new Dygraph('graph', [[new Date(), '10'], [new Date(), '20']]) |
| 87 | }, /expected number or array/i); |
| 88 | }); |
| 89 | |
| 90 | var assertData = function(g) { |
| 91 | var expected = dataArray; |
| 92 | |
| 93 | assert.equal(4, g.numRows()); |
| 94 | assert.equal(2, g.numColumns()); |
| 95 | |
| 96 | for (var i = 0; i < 4; i++) { |
| 97 | for (var j = 0; j < 2; j++) { |
| 98 | assert.equal(expected[i][j], g.getValue(i, j)); |
| 99 | } |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | }); |