-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for *.js test fixture config (#5777)
JSON doesn't support functions which are needed to create scriptable options, so implement a very basic method to load a JavaScript file exporting the config in `module.exports`. Also rename test sources (remove the `jasmine.` prefix), cleanup `karma.conf.js` and add an example .js fixture config (bubble radius option).
- Loading branch information
1 parent
5816770
commit 2dbf1cd
Showing
17 changed files
with
162 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* global __karma__ */ | ||
|
||
'use strict'; | ||
|
||
var utils = require('./utils'); | ||
|
||
function readFile(url, callback) { | ||
var request = new XMLHttpRequest(); | ||
request.onreadystatechange = function() { | ||
if (request.readyState === 4) { | ||
return callback(request.responseText); | ||
} | ||
}; | ||
|
||
request.open('GET', url, false); | ||
request.send(null); | ||
} | ||
|
||
function loadConfig(url, callback) { | ||
var regex = /\.(json|js)$/i; | ||
var matches = url.match(regex); | ||
var type = matches ? matches[1] : 'json'; | ||
var cfg = null; | ||
|
||
readFile(url, function(content) { | ||
switch (type) { | ||
case 'js': | ||
// eslint-disable-next-line | ||
cfg = new Function('"use strict"; var module = {};' + content + '; return module.exports;')(); | ||
break; | ||
case 'json': | ||
cfg = JSON.parse(content); | ||
break; | ||
default: | ||
} | ||
|
||
callback(cfg); | ||
}); | ||
} | ||
|
||
function specFromFixture(description, inputs) { | ||
var input = inputs.js || inputs.json; | ||
it(input, function(done) { | ||
loadConfig(input, function(json) { | ||
var chart = utils.acquireChart(json.config, json.options); | ||
if (!inputs.png) { | ||
fail('Missing PNG comparison file for ' + inputs.json); | ||
done(); | ||
} | ||
|
||
utils.readImageData(inputs.png, function(expected) { | ||
expect(chart).toEqualImageData(expected, json); | ||
utils.releaseChart(chart); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function specsFromFixtures(path) { | ||
var regex = new RegExp('(^/base/test/fixtures/' + path + '.+)\\.(png|json|js)'); | ||
var inputs = {}; | ||
|
||
Object.keys(__karma__.files || {}).forEach(function(file) { | ||
var matches = file.match(regex); | ||
var name = matches && matches[1]; | ||
var type = matches && matches[2]; | ||
|
||
if (name && type) { | ||
inputs[name] = inputs[name] || {}; | ||
inputs[name][type] = file; | ||
} | ||
}); | ||
|
||
return function() { | ||
Object.keys(inputs).forEach(function(key) { | ||
specFromFixture(key, inputs[key]); | ||
}); | ||
}; | ||
} | ||
|
||
module.exports = { | ||
specs: specsFromFixtures | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module.exports = { | ||
config: { | ||
type: 'bubble', | ||
data: { | ||
datasets: [{ | ||
data: [ | ||
{x: 0, y: 0}, | ||
{x: 1, y: 0}, | ||
{x: 2, y: 0}, | ||
{x: 3, y: 0}, | ||
{x: 4, y: 0}, | ||
{x: 5, y: 0} | ||
], | ||
radius: function(ctx) { | ||
return ctx.dataset.data[ctx.dataIndex].x * 4; | ||
} | ||
}] | ||
}, | ||
options: { | ||
legend: false, | ||
title: false, | ||
scales: { | ||
xAxes: [{display: false}], | ||
yAxes: [{display: false}] | ||
}, | ||
elements: { | ||
point: { | ||
backgroundColor: '#444' | ||
} | ||
}, | ||
layout: { | ||
padding: { | ||
left: 24, | ||
right: 24 | ||
} | ||
} | ||
} | ||
}, | ||
options: { | ||
canvas: { | ||
height: 128, | ||
width: 256 | ||
} | ||
} | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters