Skip to content

Commit 655d7bf

Browse files
authored
Merge pull request #3118 from plotly/test-validateTemplate-dom
test that validateTemplate accepts DOM element as input
2 parents 5468d6a + 2ccb815 commit 655d7bf

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

test/jasmine/tests/template_test.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,7 @@ describe('template interactions', function() {
263263

264264
describe('validateTemplate', function() {
265265

266-
function checkValidate(mock, expected, countToCheck) {
267-
var template = mock.layout.template;
268-
var mockNoTemplate = Lib.extendDeep({}, mock);
269-
delete mockNoTemplate.layout.template;
270-
271-
var out1 = Plotly.validateTemplate(mock);
272-
var out2 = Plotly.validateTemplate(mockNoTemplate, template);
266+
function compareOutputs(out1, out2, expected, countToCheck) {
273267
expect(out2).toEqual(out1);
274268
if(expected) {
275269
expect(countToCheck ? out1.slice(0, countToCheck) : out1)
@@ -280,17 +274,39 @@ describe('validateTemplate', function() {
280274
}
281275
}
282276

277+
function checkValidate(mock, expected, countToCheck) {
278+
var template = mock.layout.template;
279+
var mockNoTemplate = Lib.extendDeep({}, mock);
280+
delete mockNoTemplate.layout.template;
281+
282+
// Test with objects as argument
283+
var out1 = Plotly.validateTemplate(mock);
284+
var out2 = Plotly.validateTemplate(mockNoTemplate, template);
285+
expect(out2).toEqual(out1);
286+
compareOutputs(out1, out2, expected, countToCheck);
287+
288+
// Test with DOM elements as argument
289+
var gd = createGraphDiv();
290+
return Plotly.newPlot(gd, mock)
291+
.then(function() {out1 = Plotly.validateTemplate(gd);})
292+
.then(function() {return Plotly.newPlot(gd, mockNoTemplate);})
293+
.then(function() {out2 = Plotly.validateTemplate(gd, template);})
294+
.then(function() {compareOutputs(out1, out2, expected, countToCheck);})
295+
.catch(failTest)
296+
.then(destroyGraphDiv);
297+
}
298+
283299
var cleanMock = Lib.extendDeep({}, templateMock);
284300
cleanMock.layout.annotations.pop();
285301
cleanMock.data.pop();
286302
cleanMock.data.splice(1, 1);
287303
cleanMock.layout.template.data.bar.pop();
288304

289-
it('returns undefined when the template matches precisely', function() {
290-
checkValidate(cleanMock);
305+
it('returns undefined when the template matches precisely', function(done) {
306+
checkValidate(cleanMock).then(done);
291307
});
292308

293-
it('catches all classes of regular issue', function() {
309+
it('catches all classes of regular issue', function(done) {
294310
var messyMock = Lib.extendDeep({}, templateMock);
295311
messyMock.data.push({type: 'box', x0: 1, y: [1, 2, 3]});
296312
messyMock.layout.template.layout.geo = {projection: {type: 'orthographic'}};
@@ -347,10 +363,10 @@ describe('validateTemplate', function() {
347363
path: 'layout.annotations[4]',
348364
templateitemname: 'nope',
349365
msg: 'There are no templates for item layout.annotations[4] with name nope'
350-
}]);
366+
}]).then(done);
351367
});
352368

353-
it('catches missing template.data', function() {
369+
it('catches missing template.data', function(done) {
354370
var noDataMock = Lib.extendDeep({}, cleanMock);
355371
delete noDataMock.layout.template.data;
356372

@@ -360,17 +376,17 @@ describe('validateTemplate', function() {
360376
}],
361377
// check only the first error - we don't care about the specifics
362378
// uncovered after we already know there's no template.data
363-
1);
379+
1).then(done);
364380
});
365381

366-
it('catches missing template.layout', function() {
382+
it('catches missing template.layout', function(done) {
367383
var noLayoutMock = Lib.extendDeep({}, cleanMock);
368384
delete noLayoutMock.layout.template.layout;
369385

370386
checkValidate(noLayoutMock, [{
371387
code: 'layout',
372388
msg: 'The template has no key layout.'
373-
}], 1);
389+
}], 1).then(done);
374390
});
375391

376392
});

0 commit comments

Comments
 (0)