X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2Fannotations.js;h=d0a2f7ffbfecc57f12858a6b649cdfd1fc732fe7;hb=0b85865a8b8c34b1d63f11bad02360e96ad8e02a;hp=939c83fe8fa28689fbb7b529c635a197f3706740;hpb=4c919f55a9d057278452891a9a6fe5da621e5ce4;p=dygraphs.git diff --git a/auto_tests/tests/annotations.js b/auto_tests/tests/annotations.js index 939c83f..d0a2f7f 100644 --- a/auto_tests/tests/annotations.js +++ b/auto_tests/tests/annotations.js @@ -3,16 +3,16 @@ * * @author danvk@google.com (Dan Vanderkam) */ -var annotationsTestCase = TestCase("annotations"); +var AnnotationsTestCase = TestCase("annotations"); -annotationsTestCase.prototype.setUp = function() { +AnnotationsTestCase.prototype.setUp = function() { document.body.innerHTML = "
"; }; -annotationsTestCase.prototype.tearDown = function() { +AnnotationsTestCase.prototype.tearDown = function() { }; -annotationsTestCase.prototype.testAnnotationsDrawn = function() { +AnnotationsTestCase.prototype.testAnnotationsDrawn = function() { var opts = { width: 480, height: 320 @@ -59,7 +59,7 @@ annotationsTestCase.prototype.testAnnotationsDrawn = function() { // 1. Invalid series name (e.g. 'X' or 'non-existent') // 2. Passing a string as 'x' instead of a number (e.g. x: '1') -annotationsTestCase.prototype.testAnnotationsDontDisappearOnResize = function() { +AnnotationsTestCase.prototype.testAnnotationsDontDisappearOnResize = function() { var opts = { }; var data = "X,Y\n" + @@ -98,7 +98,7 @@ annotationsTestCase.prototype.testAnnotationsDontDisappearOnResize = function() }; // Verify that annotations outside of the visible x-range are not shown. -annotationsTestCase.prototype.testAnnotationsOutOfRangeX = function() { +AnnotationsTestCase.prototype.testAnnotationsOutOfRangeX = function() { var opts = { }; var data = "X,Y\n" + @@ -141,7 +141,7 @@ annotationsTestCase.prototype.testAnnotationsOutOfRangeX = function() { }; // Verify that annotations outside of the visible y-range are not shown. -annotationsTestCase.prototype.testAnnotationsOutOfRangeY = function() { +AnnotationsTestCase.prototype.testAnnotationsOutOfRangeY = function() { var opts = { }; var data = "X,Y\n" + @@ -175,3 +175,98 @@ annotationsTestCase.prototype.testAnnotationsOutOfRangeY = function() { a1 = document.getElementsByClassName('ann1'); assertEquals(1, a1.length); }; + +AnnotationsTestCase.prototype.testAnnotationsDrawnInDrawCallback = function() { + var data = "X,Y\n" + + "0,-1\n" + + "1,0\n" + + "2,1\n"; + + var graph = document.getElementById("graph"); + + var calls = []; + var g = new Dygraph(graph, data, { + width: 480, + height: 320, + drawCallback: function(g, initial) { + calls.push(initial); + if (initial) { + g.setAnnotations([ + { + series: 'Y', + x: 1, + shortText: 'A', + text: 'Long A', + }, + ]); + } + } + }); + + assertEquals([true, false], calls); +}; + + +// Test that annotations on the same point are stacked. +// Regression test for http://code.google.com/p/dygraphs/issues/detail?id=256 +AnnotationsTestCase.prototype.testAnnotationsStacked = function() { + var data = 'X,Y1,Y2\n' + + '0,1,2\n' + + '1,2,3\n'; + var graph = document.getElementById("graph"); + var annotations = [ + { + series: 'Y1', + x: 0, + shortText: '1', + attachAtBottom: true + }, + { + series: 'Y2', + x: 0, + shortText: '2', + attachAtBottom: true + } + ]; + var g = new Dygraph(graph, data, { + width: 480, + height: 320 + }); + g.setAnnotations(annotations); + + var annEls = document.getElementsByClassName('dygraphDefaultAnnotation'); + assertEquals(2, annEls.length); + + assertEquals(annEls[0].offsetLeft, annEls[1].offsetLeft); + assert(annEls[1].offsetTop < annEls[0].offsetTop - 10); +}; + + +// Test the .ready() method, which is most often used with setAnnotations(). +AnnotationsTestCase.prototype.testReady = function() { + var data = 'X,Y1,Y2\n' + + '0,1,2\n' + + '1,2,3\n'; + var mockXhr = Util.overrideXMLHttpRequest(data); + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, "data.csv", { + width: 480, + height: 320 + }); + + var ready_calls = 0; + g.ready(function() { ready_calls++; }); + + assertEquals(0, ready_calls); + mockXhr.respond(); + assertEquals(1, ready_calls); + + // Make sure that ready isn't called on redraws. + g.updateOptions({}); + assertEquals(1, ready_calls); + + // Or data changes. + g.updateOptions({file: data}); + assertEquals(1, ready_calls); +};