X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2Frange_tests.js;h=f14d30fdb71c034e996bd9358c756075d54420fb;hb=fd6b8dadbaa0614671bef1508c23d6a932e25081;hp=4fcb8223fd5c75929d21c4390cb8f7161c599f21;hpb=36a27704957dda706074a857884d52c1cf5a974c;p=dygraphs.git diff --git a/auto_tests/tests/range_tests.js b/auto_tests/tests/range_tests.js index 4fcb822..f14d30f 100644 --- a/auto_tests/tests/range_tests.js +++ b/auto_tests/tests/range_tests.js @@ -19,13 +19,19 @@ // THE SOFTWARE. -/** +/** * @fileoverview Test valueRange and dateWindow changes. * * @author konigsberg@google.com (Robert Konigsberg) */ + +import Dygraph from '../../src/dygraph'; + +import DygraphOps from './DygraphOps'; +import {assertDeepCloseTo} from './custom_asserts'; + var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]]; -var ZERO_TO_FIFTY_STEPS = function() { +var ZERO_TO_FIFTY_STEPS = (function() { var a = []; var x = 10; var y = 0; @@ -34,23 +40,26 @@ var ZERO_TO_FIFTY_STEPS = function() { a.push([x + (step * .2), y + step]); } return a; -} (); +}()); + var FIVE_TO_ONE_THOUSAND = [ - [ 1, 10 ], [ 2, 20 ], [ 3, 30 ], [ 4, 40 ] , [ 5, 50 ], + [ 1, 10 ], [ 2, 20 ], [ 3, 30 ], [ 4, 40 ] , [ 5, 50 ], [ 6, 60 ], [ 7, 70 ], [ 8, 80 ], [ 9, 90 ] , [ 10, 1000 ]]; -var RangeTestCase = TestCase("range-tests"); +describe("range-tests", function() { -RangeTestCase.prototype.setUp = function() { - document.body.innerHTML = "
"; -}; +cleanupAfterEach(); -RangeTestCase.prototype.createGraph = function(opts) { - var graph = document.getElementById("graph"); - var g = new Dygraph(graph, ZERO_TO_FIFTY_STEPS, opts); +var createGraph = function(opts, data, expectRangeX, expectRangeY) { + if (data === undefined) data = ZERO_TO_FIFTY_STEPS; + if (expectRangeX === undefined) expectRangeX = [10, 20]; + if (expectRangeY === undefined) expectRangeY = [0, 55]; + if (!opts) opts = {}; + opts['labels'] = ['X', 'Y']; + var g = new Dygraph('graph', data, opts); - assertEquals([10, 20], g.xAxisRange()); - assertEquals([0, 55], g.yAxisRange(0)); + assertDeepCloseTo(expectRangeX, g.xAxisRange(), 0.01); + assertDeepCloseTo(expectRangeY, g.yAxisRange(0), 0.01); return g; }; @@ -59,35 +68,59 @@ RangeTestCase.prototype.createGraph = function(opts) { * Test that changes to valueRange and dateWindow are reflected * appropriately. */ -RangeTestCase.prototype.testRangeSetOperations = function() { - var g = this.createGraph({valueRange : [ 0, 55 ]}); +it('testRangeSetOperations', function() { + var g = createGraph({valueRange : [ 0, 55 ]}); g.updateOptions({ dateWindow : [ 12, 18 ] }); - assertEquals([12, 18], g.xAxisRange()); - assertEquals([0, 55], g.yAxisRange(0)); + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([0, 55], g.yAxisRange(0)); g.updateOptions({ valueRange : [ 10, 40 ] }); - assertEquals([12, 18], g.xAxisRange()); - assertEquals([10, 40], g.yAxisRange(0)); + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([10, 40], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, NaN] }); + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([10, 44.2], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, 40] }); + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([10, 40], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, null] }); + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([10, 44.2], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, 40] }); + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([10, 40], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, undefined] }); + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([10, 44.2], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, 40] }); + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([10, 40], g.yAxisRange(0)); g.updateOptions({ }); - assertEquals([12, 18], g.xAxisRange()); - assertEquals([10, 40], g.yAxisRange(0)); - + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([10, 40], g.yAxisRange(0)); + g.updateOptions({valueRange : null, axes: {y:{valueRange : [15, 20]}}}); - assertEquals([12, 18], g.xAxisRange()); - assertEquals([15, 20], g.yAxisRange(0)); + assert.deepEqual([12, 18], g.xAxisRange()); + assert.deepEqual([15, 20], g.yAxisRange(0)); g.updateOptions({ dateWindow : null, valueRange : null, axes: null }); - assertEquals([10, 20], g.xAxisRange()); - assertEquals([0, 55], g.yAxisRange(0)); -}; + assert.deepEqual([10, 20], g.xAxisRange()); + assert.deepEqual([0, 55], g.yAxisRange(0)); +}); /** * Verify that when zoomed in by mouse operations, an empty call to * updateOptions doesn't change the displayed ranges. */ -RangeTestCase.prototype.zoom = function(g, xRange, yRange) { +var zoom = function(g, xRange, yRange) { var originalXRange = g.xAxisRange(); var originalYRange = g.yAxisRange(0); @@ -95,16 +128,16 @@ RangeTestCase.prototype.zoom = function(g, xRange, yRange) { DygraphOps.dispatchMouseMove(g, xRange[1], yRange[0]); // this is really necessary. DygraphOps.dispatchMouseUp(g, xRange[1], yRange[0]); - assertEqualsDelta(xRange, g.xAxisRange(), 0.2); - // assertEqualsDelta(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle. + assertDeepCloseTo(xRange, g.xAxisRange(), 0.2); + // assert.closeTo(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle. var midX = (xRange[1] - xRange[0]) / 2; DygraphOps.dispatchMouseDown(g, midX, yRange[0]); DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary. DygraphOps.dispatchMouseUp(g, midX, yRange[1]); - assertEqualsDelta(xRange, g.xAxisRange(), 0.2); - assertEqualsDelta(yRange, g.yAxisRange(0), 0.2); + assertDeepCloseTo(xRange, g.xAxisRange(), 0.2); + assertDeepCloseTo(yRange, g.yAxisRange(0), 0.2); } @@ -112,52 +145,213 @@ RangeTestCase.prototype.zoom = function(g, xRange, yRange) { * Verify that when zoomed in by mouse operations, an empty call to * updateOptions doesn't change the displayed ranges. */ -RangeTestCase.prototype.testEmptyUpdateOptions_doesntUnzoom = function() { - var g = this.createGraph(); - this.zoom(g, [ 11, 18 ], [ 35, 40 ]); +it('testEmptyUpdateOptions_doesntUnzoom', function() { + var g = createGraph(); + zoom(g, [ 11, 18 ], [ 35, 40 ]); - assertEqualsDelta([11, 18], g.xAxisRange(), 0.1); - assertEqualsDelta([35, 40], g.yAxisRange(0), 0.2); + assertDeepCloseTo([11, 18], g.xAxisRange(), 0.1); + assertDeepCloseTo([35, 40], g.yAxisRange(0), 0.2); g.updateOptions({}); - assertEqualsDelta([11, 18], g.xAxisRange(), 0.1); - assertEqualsDelta([35, 40], g.yAxisRange(0), 0.2); -} + assertDeepCloseTo([11, 18], g.xAxisRange(), 0.1); + assertDeepCloseTo([35, 40], g.yAxisRange(0), 0.2); +}); /** * Verify that when zoomed in by mouse operations, a call to * updateOptions({ dateWindow : null, valueRange : null }) fully * unzooms. */ -RangeTestCase.prototype.testRestoreOriginalRanges_viaUpdateOptions = function() { - var g = this.createGraph(); - this.zoom(g, [ 11, 18 ], [ 35, 40 ]); +it('testRestoreOriginalRanges_viaUpdateOptions', function() { + var g = createGraph(); + zoom(g, [ 11, 18 ], [ 35, 40 ]); g.updateOptions({ dateWindow : null, valueRange : null }); - assertEquals([0, 55], g.yAxisRange(0)); - assertEquals([10, 20], g.xAxisRange()); -} + assert.deepEqual([0, 55], g.yAxisRange(0)); + assert.deepEqual([10, 20], g.xAxisRange()); +}); /** * Verify that log scale axis range is properly specified. */ -RangeTestCase.prototype.testLogScaleExcludesZero = function() { - var g = new Dygraph("graph", FIVE_TO_ONE_THOUSAND, { logscale : true }); - assertEquals([10, 1099], g.yAxisRange(0)); - +it('testLogScaleExcludesZero', function() { + var g = new Dygraph("graph", FIVE_TO_ONE_THOUSAND, { + logscale: true, + labels: ['X', 'Y'] + }); + assert.deepEqual([10, 1099], g.yAxisRange(0)); + g.updateOptions({ logscale : false }); - assertEquals([0, 1099], g.yAxisRange(0)); -} + assert.deepEqual([0, 1099], g.yAxisRange(0)); +}); /** * Verify that includeZero range is properly specified. */ -RangeTestCase.prototype.testIncludeZeroIncludesZero = function() { - var g = new Dygraph("graph", [[0, 500], [500, 1000]], { includeZero : true }); - assertEquals([0, 1100], g.yAxisRange(0)); - +it('testIncludeZeroIncludesZero', function() { + var g = new Dygraph("graph", [[0, 500], [500, 1000]], { + includeZero: true, + labels: ['X', 'Y'] + }); + assert.deepEqual([0, 1100], g.yAxisRange(0)); + g.updateOptions({ includeZero : false }); - assertEquals([450, 1050], g.yAxisRange(0)); -} + assert.deepEqual([450, 1050], g.yAxisRange(0)); +}); + + +/** + * Verify that includeZero range is properly specified per axis. + */ +it('testIncludeZeroPerAxis', function() { + var g = new Dygraph("graph", + 'X,A,B\n'+ + '0,50,50\n'+ + '50,110,110\n', + { + drawPoints: true, + pointSize:5, + series:{ + A: { + axis: 'y', + pointSize: 10 + }, + B: { + axis: 'y2' + } + }, + axes: { + 'y2': { includeZero: true } + } + }); + + + assert.deepEqual([44, 116], g.yAxisRange(0)); + assert.deepEqual([0, 121], g.yAxisRange(1)); + + g.updateOptions({ + axes: { + 'y2': { includeZero: false } + } + }); + + assert.deepEqual([44, 116], g.yAxisRange(1)); +}); + +/** + * Verify that very large Y ranges don't break things. + */ +it('testHugeRange', function() { + var g = new Dygraph("graph", [[0, -1e120], [1, 1e230]], { + includeZero: true, + labels: ['X', 'Y'] + }); + assert.closeTo(1, -1e229 / g.yAxisRange(0)[0], 0.001); + assert.closeTo(1, 1.1e230 / g.yAxisRange(0)[1], 0.001); +}); + +/** + * Verify ranges with user-specified padding, implicit avoidMinZero. + */ +it('testPaddingAuto', function() { + var g = createGraph({ + xRangePad: 42, + yRangePad: 30 + }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]); +}); + +/** + * Verify auto range with drawAxesAtZero. + */ +it('testPaddingAutoAxisAtZero', function() { + var g = createGraph({ + drawAxesAtZero: true, + }, ZERO_TO_FIFTY_STEPS, [10, 20], [0, 55]); +}); + +/** + * Verify user-specified range with padding and drawAxesAtZero options. + * Try explicit range matching the auto range, should have identical results. + */ +it('testPaddingRange1', function() { + var g = createGraph({ + valueRange: [0, 50], + xRangePad: 42, + yRangePad: 30, + drawAxesAtZero: true + }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]); +}); + +/** + * Verify user-specified range with padding and drawAxesAtZero options. + * User-supplied range differs from the auto range. + */ +it('testPaddingRange2', function() { + var g = createGraph({ + valueRange: [10, 60], + xRangePad: 42, + yRangePad: 30, + drawAxesAtZero: true, + }, ZERO_TO_FIFTY_STEPS, [9, 21], [5, 65]); +}); + +/** + * Verify drawAxesAtZero and includeZero. + */ +it('testPaddingYAtZero', function() { + var g = createGraph({ + includeZero: true, + xRangePad: 42, + yRangePad: 30, + drawAxesAtZero: true, + }, [ + [-10, 10], + [10, 20], + [30, 50] + ], [-14, 34], [-5, 55]); +}); + +/** + * Verify logscale, compat mode. + */ +it('testLogscaleCompat', function() { + var g = createGraph({ + logscale: true + }, + [[-10, 10], [10, 10], [30, 1000]], + [-10, 30], [10, 1099]); +}); + +/** + * Verify logscale, new mode. + */ +it('testLogscalePad', function() { + var g = createGraph({ + logscale: true, + yRangePad: 30 + }, + [[-10, 10], [10, 10], [30, 1000]], + [-10, 30], [5.623, 1778.279]); +}); + +/** + * Verify scrolling all-zero region, new-style. + */ +it('testZeroScroll2', function() { + var g = new Dygraph( + document.getElementById("graph"), + "X,Y\n" + + "1,0\n" + + "8,0\n" + + "9,0.1\n", + { + animatedZooms: true, + drawAxesAtZero: true, + xRangePad: 4, + yRangePad: 4 + }); +}); + +});