X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2Fdygraph-options-tests.js;h=ded9ee3ebdb25c51bb65fb77d048fdcb31268ceb;hb=f0e472002843b5e61aa9467f97f755280c91a46b;hp=792dcdbe3a6f0b6502cb8ab36c97d86729e4c1f2;hpb=a0d2e89b1cc62ad1e6a3bf0211e303030c52663e;p=dygraphs.git diff --git a/auto_tests/tests/dygraph-options-tests.js b/auto_tests/tests/dygraph-options-tests.js index 792dcdb..ded9ee3 100644 --- a/auto_tests/tests/dygraph-options-tests.js +++ b/auto_tests/tests/dygraph-options-tests.js @@ -1,19 +1,25 @@ /** * @fileoverview Test cases for DygraphOptions. */ -var DygraphOptionsTestCase = TestCase("dygraph-options-tests"); -DygraphOptionsTestCase.prototype.setUp = function() { - document.body.innerHTML = "
"; -}; +import Dygraph from '../../src/dygraph'; +import DygraphOptions from '../../src/dygraph-options'; +import OPTIONS_REFERENCE from '../../src/dygraph-options-reference'; -DygraphOptionsTestCase.prototype.tearDown = function() { -}; +describe("dygraph-options-tests", function() { + +cleanupAfterEach(); + +var graph; + +beforeEach(function() { + graph = document.getElementById("graph"); +}); /* * Pathalogical test to ensure getSeriesNames works */ -DygraphOptionsTestCase.prototype.testGetSeriesNames = function() { +it('testGetSeriesNames', function() { var opts = { width: 480, height: 320 @@ -22,20 +28,19 @@ DygraphOptionsTestCase.prototype.testGetSeriesNames = function() { "0,-1,0,0"; // Kind of annoying that you need a DOM to test the object. - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); // We don't need to get at g's attributes_ object just // to test DygraphOptions. var o = new DygraphOptions(g); - assertEquals(["Y", "Y2", "Y3"], o.seriesNames()); -}; + assert.deepEqual(["Y", "Y2", "Y3"], o.seriesNames()); +}); /* * Ensures that even if logscale is set globally, it doesn't impact the * x axis. */ -DygraphOptionsTestCase.prototype.getLogscaleForX = function() { +it('testGetLogscaleForX', function() { var opts = { width: 480, height: 320 @@ -44,13 +49,74 @@ DygraphOptionsTestCase.prototype.getLogscaleForX = function() { "1,-1,2,3"; // Kind of annoying that you need a DOM to test the object. - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - assertFalse(g.getOptionForAxis('logscale', 'x')); - assertFalse(g.getOptionForAxis('logscale', 'y')); + assert.isFalse(!!g.getOptionForAxis('logscale', 'x')); + assert.isFalse(!!g.getOptionForAxis('logscale', 'y')); g.updateOptions({ logscale : true }); - assertFalse(g.getOptionForAxis('logscale', 'x')); - assertTrue(g.getOptionForAxis('logscale', 'y')); + assert.isFalse(!!g.getOptionForAxis('logscale', 'x')); + assert.isTrue(!!g.getOptionForAxis('logscale', 'y')); +}); + +// Helper to gather all warnings emitted by Dygraph constructor. +// Removes everything after the first open parenthesis in each warning. +// Returns them in a (possibly empty) list. +var getWarnings = function(div, data, opts) { + var warnings = []; + var oldWarn = console.warn; + console.warn = function(message) { + warnings.push(message.replace(/ \(.*/, '')); + }; + try { + new Dygraph(graph, data, opts); + } catch (e) { + } + console.warn = oldWarn; + return warnings; }; + +it('testLogWarningForNonexistentOption', function() { + if (!OPTIONS_REFERENCE) { + return; // this test won't pass in non-debug mode. + } + + var data = "X,Y,Y2,Y3\n" + + "1,-1,2,3"; + + var expectWarning = function(opts, badOptionName) { + DygraphOptions.resetWarnings_(); + var warnings = getWarnings(graph, data, opts); + assert.deepEqual(['Unknown option ' + badOptionName], warnings); + }; + var expectNoWarning = function(opts) { + DygraphOptions.resetWarnings_(); + var warnings = getWarnings(graph, data, opts); + assert.deepEqual([], warnings); + }; + + expectNoWarning({}); + expectWarning({nonExistentOption: true}, 'nonExistentOption'); + expectWarning({series: {Y: {nonExistentOption: true}}}, 'nonExistentOption'); + // expectWarning({Y: {nonExistentOption: true}}); + expectWarning({axes: {y: {anotherNonExistentOption: true}}}, 'anotherNonExistentOption'); + expectWarning({highlightSeriesOpts: {anotherNonExistentOption: true}}, 'anotherNonExistentOption'); + expectNoWarning({highlightSeriesOpts: {strokeWidth: 20}}); + expectNoWarning({strokeWidth: 20}); +}); + +it('testOnlyLogsEachWarningOnce', function() { + if (!OPTIONS_REFERENCE) { + return; // this test won't pass in non-debug mode. + } + + var data = "X,Y,Y2,Y3\n" + + "1,-1,2,3"; + + var warnings1 = getWarnings(graph, data, {nonExistent: true}); + var warnings2 = getWarnings(graph, data, {nonExistent: true}); + assert.deepEqual(['Unknown option nonExistent'], warnings1); + assert.deepEqual([], warnings2); +}); + +});