]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add support for *.js test fixture config (#5777)
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Sat, 20 Oct 2018 09:38:48 +0000 (11:38 +0200)
committerGitHub <noreply@github.com>
Sat, 20 Oct 2018 09:38:48 +0000 (11:38 +0200)
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).

17 files changed:
gulpfile.js
karma.conf.js
test/context.js [moved from test/jasmine.context.js with 100% similarity]
test/fixture.js [new file with mode: 0644]
test/fixtures/controller.bubble/radius-scriptable.js [new file with mode: 0644]
test/fixtures/controller.bubble/radius-scriptable.png [new file with mode: 0644]
test/index.js [moved from test/jasmine.index.js with 88% similarity]
test/matchers.js [moved from test/jasmine.matchers.js with 99% similarity]
test/specs/controller.bar.tests.js
test/specs/controller.bubble.tests.js
test/specs/controller.line.tests.js
test/specs/controller.polarArea.tests.js
test/specs/controller.radar.tests.js
test/specs/core.scale.tests.js
test/specs/element.point.tests.js
test/specs/plugin.filler.tests.js
test/utils.js [moved from test/jasmine.utils.js with 72% similarity]

index f6795fa7f56201340ad4f58934dd3e8569d6af32..daba620f0f55c9bc84386814911e88dfbd2000d8 100644 (file)
@@ -198,27 +198,15 @@ function docsTask(done) {
   });
 }
 
-function startTest() {
-  return [
-    {pattern: './test/fixtures/**/*.json', included: false},
-    {pattern: './test/fixtures/**/*.png', included: false},
-    './node_modules/moment/min/moment.min.js',
-    './test/jasmine.index.js',
-    './src/**/*.js',
-  ].concat(
-    argv.inputs ?
-      argv.inputs.split(';') :
-      ['./test/specs/**/*.js']
-  );
-}
-
 function unittestTask(done) {
   new karma.Server({
     configFile: path.join(__dirname, 'karma.conf.js'),
     singleRun: !argv.watch,
-    files: startTest(),
     args: {
-      coverage: !!argv.coverage
+      coverage: !!argv.coverage,
+      inputs: argv.inputs
+        ? argv.inputs.split(';')
+        : ['./test/specs/**/*.js']
     }
   },
   // https://github.com/karma-runner/gulp-karma/issues/18
index 5bb73ef03a0b0d72e9f3b12e7e1aa2b26612b590..4b3a301f178e27132f596b345c3028b7a663ee2d 100644 (file)
@@ -25,9 +25,18 @@ module.exports = function(karma) {
                        }
                },
 
+               files: [
+                       {pattern: 'test/fixtures/**/*.js', included: false},
+                       {pattern: 'test/fixtures/**/*.json', included: false},
+                       {pattern: 'test/fixtures/**/*.png', included: false},
+                       'node_modules/moment/min/moment.min.js',
+                       'test/index.js',
+                       'src/**/*.js'
+               ].concat(args.inputs),
+
                preprocessors: {
-                       './test/jasmine.index.js': ['browserify'],
-                       './src/**/*.js': ['browserify']
+                       'test/index.js': ['browserify'],
+                       'src/**/*.js': ['browserify']
                },
 
                browserify: {
similarity index 100%
rename from test/jasmine.context.js
rename to test/context.js
diff --git a/test/fixture.js b/test/fixture.js
new file mode 100644 (file)
index 0000000..5652c13
--- /dev/null
@@ -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
+};
+
diff --git a/test/fixtures/controller.bubble/radius-scriptable.js b/test/fixtures/controller.bubble/radius-scriptable.js
new file mode 100644 (file)
index 0000000..593316b
--- /dev/null
@@ -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
+               }
+       }
+};
diff --git a/test/fixtures/controller.bubble/radius-scriptable.png b/test/fixtures/controller.bubble/radius-scriptable.png
new file mode 100644 (file)
index 0000000..546466f
Binary files /dev/null and b/test/fixtures/controller.bubble/radius-scriptable.png differ
similarity index 88%
rename from test/jasmine.index.js
rename to test/index.js
index c1706130f2d8db1ffd64ae5467df9d3924347bb8..b3fa6cf3b602db0edd938260f73431210f5de4be 100644 (file)
@@ -1,6 +1,9 @@
-var Context = require('./jasmine.context');
-var matchers = require('./jasmine.matchers');
-var utils = require('./jasmine.utils');
+'use strict';
+
+var fixture = require('./fixture');
+var Context = require('./context');
+var matchers = require('./matchers');
+var utils = require('./utils');
 
 (function() {
 
@@ -42,7 +45,7 @@ var utils = require('./jasmine.utils');
                        'position: absolute' +
                '}');
 
-       jasmine.specsFromFixtures = utils.specsFromFixtures;
+       jasmine.fixture = fixture;
        jasmine.triggerMouseEvent = utils.triggerMouseEvent;
 
        beforeEach(function() {
similarity index 99%
rename from test/jasmine.matchers.js
rename to test/matchers.js
index 88f5de8fe7e8a2c2235883311d7092f838ce435a..92a6f970427fc8af667a3ac253c024f69500253f 100644 (file)
@@ -1,7 +1,7 @@
 'use strict';
 
 var pixelmatch = require('pixelmatch');
-var utils = require('./jasmine.utils');
+var utils = require('./utils');
 
 function toPercent(value) {
        return Math.round(value * 10000) / 100;
index 7957b6ab5db3a1e2dea7296dc8d745fc8261258b..0e843e1435fa3697dfc8eff20ae1716aca122efa 100644 (file)
@@ -1,5 +1,5 @@
 describe('Chart.controllers.bar', function() {
-       describe('auto', jasmine.specsFromFixtures('controller.bar'));
+       describe('auto', jasmine.fixture.specs('controller.bar'));
 
        it('should be constructed', function() {
                var chart = window.acquireChart({
index 2597b9ae6414ba50c145d09983c29f8e7da5cb45..a5f5b89a5de3af5f0689050be9e67e25fa245a42 100644 (file)
@@ -1,5 +1,5 @@
 describe('Chart.controllers.bubble', function() {
-       describe('auto', jasmine.specsFromFixtures('controller.bubble'));
+       describe('auto', jasmine.fixture.specs('controller.bubble'));
 
        it('should be constructed', function() {
                var chart = window.acquireChart({
index ed8f4ec5c80ef50ee56b73351658870b2bbd29b0..25a9ed6b0f5a276305bea71e308a164b9cb86d00 100644 (file)
@@ -1,5 +1,5 @@
 describe('Chart.controllers.line', function() {
-       describe('auto', jasmine.specsFromFixtures('controller.line'));
+       describe('auto', jasmine.fixture.specs('controller.line'));
 
        it('should be constructed', function() {
                var chart = window.acquireChart({
index 11e0be0b7d321935402d3cec60f9d44568d6f854..b706f8376a64cb9244a06aba44e57e81bcc4985b 100644 (file)
@@ -1,4 +1,4 @@
-describe('auto', jasmine.specsFromFixtures('controller.polarArea'));
+describe('auto', jasmine.fixture.specs('controller.polarArea'));
 
 describe('Chart.controllers.polarArea', function() {
        it('should be constructed', function() {
index 2bc0a2e0d24fb0690c1c0918957291d4958d1d6d..7e85e9e7eacec3a1038f7b8026373ea64e9b5c51 100644 (file)
@@ -1,5 +1,5 @@
 describe('Chart.controllers.radar', function() {
-       describe('auto', jasmine.specsFromFixtures('controller.radar'));
+       describe('auto', jasmine.fixture.specs('controller.radar'));
 
        it('Should be constructed', function() {
                var chart = window.acquireChart({
index dc2da042aca3310576b2cfe99aa1ea6c25d785ec..2981c35c9b42e9658b6eafb6f9f4af3601368a3a 100644 (file)
@@ -1,5 +1,5 @@
 describe('Core.scale', function() {
-       describe('auto', jasmine.specsFromFixtures('core.scale'));
+       describe('auto', jasmine.fixture.specs('core.scale'));
 
        it('should provide default scale label options', function() {
                expect(Chart.defaults.scale.scaleLabel).toEqual({
index 8d1227003b92d8c732e66a6581a8f4b974b35325..0f3a03e319d0fd7abf33213389df7a82056f0b1f 100644 (file)
@@ -1,5 +1,5 @@
 describe('Chart.elements.Point', function() {
-       describe('auto', jasmine.specsFromFixtures('element.point'));
+       describe('auto', jasmine.fixture.specs('element.point'));
 
        it ('Should be constructed', function() {
                var point = new Chart.elements.Point({
index 117f680360e1e6b46f78e898d9ebe486ac67fa2a..94979375832fc9379cb89009f41966f165ad733c 100644 (file)
@@ -7,7 +7,7 @@ describe('Plugin.filler', function() {
                });
        }
 
-       describe('auto', jasmine.specsFromFixtures('plugin.filler'));
+       describe('auto', jasmine.fixture.specs('plugin.filler'));
 
        describe('dataset.fill', function() {
                it('should support boundaries', function() {
similarity index 72%
rename from test/jasmine.utils.js
rename to test/utils.js
index 2c2006a2c074dc476527fa9c7561f9f85d5784e7..80430d4d5bcd09a81f6dba6a23b77fc1cc9d1d76 100644 (file)
@@ -1,18 +1,3 @@
-/* global __karma__ */
-
-function loadJSON(url, callback) {
-       var request = new XMLHttpRequest();
-       request.onreadystatechange = function() {
-               if (request.readyState === 4) {
-                       return callback(JSON.parse(request.responseText));
-               }
-       };
-
-       request.overrideMimeType('application/json');
-       request.open('GET', url, true);
-       request.send(null);
-}
-
 function createCanvas(w, h) {
        var canvas = document.createElement('canvas');
        canvas.width = w;
@@ -112,46 +97,6 @@ function injectCSS(css) {
        head.appendChild(style);
 }
 
-function specFromFixture(description, inputs) {
-       it(inputs.json, function(done) {
-               loadJSON(inputs.json, function(json) {
-                       var chart = acquireChart(json.config, json.options);
-                       if (!inputs.png) {
-                               fail('Missing PNG comparison file for ' + inputs.json);
-                               done();
-                       }
-
-                       readImageData(inputs.png, function(expected) {
-                               expect(chart).toEqualImageData(expected, json);
-                               releaseChart(chart);
-                               done();
-                       });
-               });
-       });
-}
-
-function specsFromFixtures(path) {
-       var regex = new RegExp('(^/base/test/fixtures/' + path + '.+)\\.(png|json)');
-       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]);
-               });
-       };
-}
-
 function waitForResize(chart, callback) {
        var override = chart.resize;
        chart.resize = function() {
@@ -180,7 +125,7 @@ module.exports = {
        createCanvas: createCanvas,
        acquireChart: acquireChart,
        releaseChart: releaseChart,
-       specsFromFixtures: specsFromFixtures,
+       readImageData: readImageData,
        triggerMouseEvent: triggerMouseEvent,
        waitForResize: waitForResize
 };