From f3cbe61e2511cb11d1779b7b5ee41da9b78aada0 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 11 May 2011 23:57:24 -0400 Subject: [PATCH] add tests for axis labels to pin down the current behavior --- auto_tests/misc/local.html | 2 + auto_tests/tests/axis_labels.js | 111 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 auto_tests/tests/axis_labels.js diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index 111d07a..046a11e 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -22,6 +22,8 @@ + +
diff --git a/auto_tests/tests/axis_labels.js b/auto_tests/tests/axis_labels.js new file mode 100644 index 0000000..f1c06d6 --- /dev/null +++ b/auto_tests/tests/axis_labels.js @@ -0,0 +1,111 @@ +/** + * @fileoverview Test cases for how axis labels are chosen and formatted. + * + * @author dan@dygraphs.com (Dan Vanderkam) + */ +var AxisLabelsTestCase = TestCase("axis-labels"); + +AxisLabelsTestCase.prototype.setUp = function() { + document.body.innerHTML = "
"; +}; + +AxisLabelsTestCase.prototype.tearDown = function() { +}; + +function getYLabels() { + var y_labels = document.getElementsByClassName("dygraph-axis-label-y"); + var ary = []; + for (var i = 0; i < y_labels.length; i++) { + ary.push(y_labels[i].innerHTML); + } + return ary; +} + +AxisLabelsTestCase.prototype.testMinusOneToOne = function() { + var opts = { + width: 480, + height: 320 + }; + var data = "X,Y\n" + + "0,-1\n" + + "1,0\n" + + "2,1\n" + + "3,0\n" + ; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + + // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better? + assertEquals(['-1','-0.5','0','0.5','1'], getYLabels()); + + // Go up to 2 + data += "4,2\n"; + g.updateOptions({file: data}); + assertEquals(['-1','-0.5','0','0.5','1','1.5','2'], getYLabels()); + + // Now 10 + data += "5,10\n"; + g.updateOptions({file: data}); + assertEquals(['-2','0','2','4','6','8','10'], getYLabels()); + + // Now 100 + data += "6,100\n"; + g.updateOptions({file: data}); + assertEquals(['0','20','40','60','80','100'], getYLabels()); +}; + +AxisLabelsTestCase.prototype.testSmallRangeNearZero = function() { + var opts = { + width: 480, + height: 320 + }; + var data = "X,Y\n" + + "0,-1\n" + + "1,0\n" + + "2,1\n" + + "3,0\n" + ; + opts.valueRange = [-0.1, 0.1]; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + assertEquals(["-0.1","-0.08","-0.06","-0.04","-0.02","0","0.02","0.04","0.06","0.08"], getYLabels()); + + opts.valueRange = [-0.05, 0.05]; + g.updateOptions(opts); + // TODO(danvk): why '1.00e-2' and not '0.01'? + assertEquals(["-0.05","-0.04","-0.03","-0.02","-0.01","0","1.00e-2","0.02","0.03","0.04"], getYLabels()); + + opts.valueRange = [-0.01, 0.01]; + g.updateOptions(opts); + assertEquals(["-0.01","-8.00e-3","-6.00e-3","-4.00e-3","-2.00e-3","0","2.00e-3","4.00e-3","6.00e-3","8.00e-3"], getYLabels()); +}; + +AxisLabelsTestCase.prototype.testSmallRangeAwayFromZero = function() { + var opts = { + width: 480, + height: 320 + }; + var data = "X,Y\n" + + "0,-1\n" + + "1,0\n" + + "2,1\n" + + "3,0\n" + ; + var graph = document.getElementById("graph"); + + opts.valueRange = [9.9, 10.1]; + var g = new Dygraph(graph, data, opts); + assertEquals(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], getYLabels()); + + opts.valueRange = [9.99, 10.01]; + g.updateOptions(opts); + // TODO(danvk): this is bad + assertEquals(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], getYLabels()); + + opts.valueRange = [9.999, 10.001]; + g.updateOptions(opts); + // TODO(danvk): this is even worse! + assertEquals(["10","10","10","10","10","10","10","10","10","10"], getYLabels()); +}; -- 2.7.4