From 3881b9127f4c29470d19bb95d3b135533e5c76d6 Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil Date: Wed, 17 Oct 2018 13:33:38 -0400 Subject: [PATCH 1/2] test that validateTemplate accets DOM element as input --- test/jasmine/tests/template_test.js | 48 ++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/test/jasmine/tests/template_test.js b/test/jasmine/tests/template_test.js index 600076ca613..d1226c0bfdc 100644 --- a/test/jasmine/tests/template_test.js +++ b/test/jasmine/tests/template_test.js @@ -263,13 +263,7 @@ describe('template interactions', function() { describe('validateTemplate', function() { - function checkValidate(mock, expected, countToCheck) { - var template = mock.layout.template; - var mockNoTemplate = Lib.extendDeep({}, mock); - delete mockNoTemplate.layout.template; - - var out1 = Plotly.validateTemplate(mock); - var out2 = Plotly.validateTemplate(mockNoTemplate, template); + function compareOutputs(out1, out2, expected, countToCheck) { expect(out2).toEqual(out1); if(expected) { expect(countToCheck ? out1.slice(0, countToCheck) : out1) @@ -280,17 +274,41 @@ describe('validateTemplate', function() { } } + function checkValidate(mock, expected, countToCheck) { + var template = mock.layout.template; + var mockNoTemplate = Lib.extendDeep({}, mock); + delete mockNoTemplate.layout.template; + + // Test with objects as argument + var out1 = Plotly.validateTemplate(mock); + var out2 = Plotly.validateTemplate(mockNoTemplate, template); + expect(out2).toEqual(out1); + compareOutputs(out1, out2, expected, countToCheck); + + // Test with DOM elements as argument + var gd = createGraphDiv(), gdNotemplate = createGraphDiv(); + return Plotly.newPlot(gd, mock) + .then(function() {return Plotly.newPlot(gdNotemplate, mockNoTemplate);}) + .then(function() { + var out1 = Plotly.validateTemplate(gd); + var out2 = Plotly.validateTemplate(gdNotemplate, template); + compareOutputs(out1, out2, expected, countToCheck); + }) + .catch(failTest) + .then(destroyGraphDiv); + } + var cleanMock = Lib.extendDeep({}, templateMock); cleanMock.layout.annotations.pop(); cleanMock.data.pop(); cleanMock.data.splice(1, 1); cleanMock.layout.template.data.bar.pop(); - it('returns undefined when the template matches precisely', function() { - checkValidate(cleanMock); + it('returns undefined when the template matches precisely', function(done) { + checkValidate(cleanMock).then(done); }); - it('catches all classes of regular issue', function() { + it('catches all classes of regular issue', function(done) { var messyMock = Lib.extendDeep({}, templateMock); messyMock.data.push({type: 'box', x0: 1, y: [1, 2, 3]}); messyMock.layout.template.layout.geo = {projection: {type: 'orthographic'}}; @@ -347,10 +365,10 @@ describe('validateTemplate', function() { path: 'layout.annotations[4]', templateitemname: 'nope', msg: 'There are no templates for item layout.annotations[4] with name nope' - }]); + }]).then(done); }); - it('catches missing template.data', function() { + it('catches missing template.data', function(done) { var noDataMock = Lib.extendDeep({}, cleanMock); delete noDataMock.layout.template.data; @@ -360,17 +378,17 @@ describe('validateTemplate', function() { }], // check only the first error - we don't care about the specifics // uncovered after we already know there's no template.data - 1); + 1).then(done); }); - it('catches missing template.layout', function() { + it('catches missing template.layout', function(done) { var noLayoutMock = Lib.extendDeep({}, cleanMock); delete noLayoutMock.layout.template.layout; checkValidate(noLayoutMock, [{ code: 'layout', msg: 'The template has no key layout.' - }], 1); + }], 1).then(done); }); }); From 2ccb81539903eb7239471f6fb4d23965d60f32f0 Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil Date: Wed, 17 Oct 2018 13:44:35 -0400 Subject: [PATCH 2/2] reuse same graph div in test to ensure proper cleanup --- test/jasmine/tests/template_test.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/jasmine/tests/template_test.js b/test/jasmine/tests/template_test.js index d1226c0bfdc..0e4097c6b59 100644 --- a/test/jasmine/tests/template_test.js +++ b/test/jasmine/tests/template_test.js @@ -286,14 +286,12 @@ describe('validateTemplate', function() { compareOutputs(out1, out2, expected, countToCheck); // Test with DOM elements as argument - var gd = createGraphDiv(), gdNotemplate = createGraphDiv(); + var gd = createGraphDiv(); return Plotly.newPlot(gd, mock) - .then(function() {return Plotly.newPlot(gdNotemplate, mockNoTemplate);}) - .then(function() { - var out1 = Plotly.validateTemplate(gd); - var out2 = Plotly.validateTemplate(gdNotemplate, template); - compareOutputs(out1, out2, expected, countToCheck); - }) + .then(function() {out1 = Plotly.validateTemplate(gd);}) + .then(function() {return Plotly.newPlot(gd, mockNoTemplate);}) + .then(function() {out2 = Plotly.validateTemplate(gd, template);}) + .then(function() {compareOutputs(out1, out2, expected, countToCheck);}) .catch(failTest) .then(destroyGraphDiv); }