Fix graphs created in not-displayed divs.
[dygraphs.git] / auto_tests / tests / resize.js
index d9dbbfc..5d68b1b 100644 (file)
@@ -5,6 +5,15 @@
  */
 var ResizeTestCase = TestCase("resize");
 
+ResizeTestCase.data =
+      "Date,Y\n" +
+      "2010/01/01,100\n" +
+      "2010/02/01,200\n" +
+      "2010/03/01,300\n" +
+      "2010/04/01,400\n" +
+      "2010/05/01,300\n" +
+      "2010/06/01,100\n";
+
 ResizeTestCase.prototype.setUp = function() {
   document.body.innerHTML = "<div id='graph'></div>";
 };
@@ -32,15 +41,7 @@ ResizeTestCase.prototype.testResizeMaintainsMouseOperations = function() {
     DygraphOps.dispatchMouseUp_Point(g, x2 - 1, y);
   }
 
-  g = new Dygraph(graph,
-      "Date,Y\n" +
-      "2010/01/01,100\n" +
-      "2010/02/01,200\n" +
-      "2010/03/01,300\n" +
-      "2010/04/01,400\n" +
-      "2010/05/01,300\n" +
-      "2010/06/01,100\n",
-      { highlightCallback : callback });
+  g = new Dygraph(graph, ResizeTestCase.data, {highlightCallback: callback});
 
   strum(g, 300, 640);
   assertEquals(6, callbackCount);
@@ -52,3 +53,41 @@ ResizeTestCase.prototype.testResizeMaintainsMouseOperations = function() {
   strum(g, 300, 500);
   assertEquals(6, callbackCount);
 };
+
+/**
+ * Tests that a graph created in a not-displayed div works as expected
+ * if the graph options include height and width. Resize not needed.
+ */
+ResizeTestCase.prototype.testHiddenDivWithSizedGraph = function() {
+  var div = document.getElementById("graph");
+
+  div.style.display = 'none';
+  var g = new Dygraph(div, ResizeTestCase.data, {width: 400, height: 300});
+  div.style.display = '';
+
+  var area = g.getArea();
+  assertTrue(area.w > 0);
+  assertTrue(area.h > 0);
+};
+
+/**
+ * Tests that a graph created in a not-displayed div with
+ * CSS-specified size but no graph height or width options works as
+ * expected. The user needs to call resize() on it after displaying
+ * it.
+ */
+ResizeTestCase.prototype.testHiddenDivWithResize = function() {
+  var div = document.getElementById("graph");
+
+  div.style.display = 'none';
+  div.style.width = '400px';
+  div.style.height = '300px';
+
+  var g = new Dygraph(div, ResizeTestCase.data, {});
+  div.style.display = '';
+
+  g.resize();
+  area = g.getArea();
+  assertTrue(area.w > 0);
+  assertTrue(area.h > 0);
+};