chartjs-test-utils 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * chartjs-test-utils v0.2.1
2
+ * chartjs-test-utils v0.4.0
3
3
  * https://github.com/chartjs/chartjs-test-utils#readme
4
- * (c) 2021 chartjs-plugin-annotation Contributors
4
+ * (c) 2022 chartjs-plugin-annotation Contributors
5
5
  * Released under the MIT License
6
6
  */
7
7
  // Code from https://stackoverflow.com/questions/4406864/html-canvas-unit-testing
@@ -533,13 +533,13 @@ function readImageData(url, callback) {
533
533
  /**
534
534
  * Injects a new canvas (and div wrapper) and creates the associated Chart instance
535
535
  * using the given config. Additional options allow tweaking elements generation.
536
- * @param {object} config - Chart config.
537
- * @param {object} options - Chart acquisition options.
538
- * @param {object} options.canvas - Canvas attributes.
539
- * @param {object} options.wrapper - Canvas wrapper attributes.
540
- * @param {boolean} options.useOffscreenCanvas - use an OffscreenCanvas instead of the normal HTMLCanvasElement.
541
- * @param {boolean} options.useShadowDOM - use shadowDom
542
- * @param {boolean} options.persistent - If true, the chart will not be released after the spec.
536
+ * @param {object} [config] - Chart config.
537
+ * @param {object} [options] - Chart acquisition options.
538
+ * @param {object} [options.canvas] - Canvas attributes.
539
+ * @param {object} [options.wrapper] - Canvas wrapper attributes.
540
+ * @param {boolean} [options.useOffscreenCanvas] - use an OffscreenCanvas instead of the normal HTMLCanvasElement.
541
+ * @param {boolean} [options.useShadowDOM] - use shadowDom
542
+ * @param {boolean} [options.persistent] - If true, the chart will not be released after the spec.
543
543
  */
544
544
  function _acquireChart(config, options) {
545
545
  var wrapper = document.createElement('div');
@@ -648,7 +648,7 @@ function afterEvent(chart, type, callback) {
648
648
  var override = chart._eventHandler;
649
649
  chart._eventHandler = function(event) {
650
650
  override.call(this, event);
651
- if (event.type === type) {
651
+ if (event.type === type || (event.native && event.native.type === type)) {
652
652
  chart._eventHandler = override;
653
653
  // eslint-disable-next-line callback-return
654
654
  callback();
@@ -687,6 +687,8 @@ async function triggerMouseEvent(chart, type, el) {
687
687
  node.dispatchEvent(event);
688
688
 
689
689
  await promise;
690
+
691
+ return event;
690
692
  }
691
693
 
692
694
  function isObject(value) {
@@ -839,14 +841,16 @@ function buildPixelMatchPreview(actual, expected, diff, threshold, tolerance, co
839
841
  var item = document.createElement('div');
840
842
  item.style.cssText = 'text-align: center; font: 12px monospace; line-height: 1.4; margin: 8px';
841
843
  item.innerHTML = '<div style="margin: 8px; height: 32px">' + values.label + '</div>';
842
- item.appendChild(canvasFromImageData(values.data));
844
+ var canvas = canvasFromImageData(values.data);
845
+ canvas.style.cssText = 'border: 1px dashed red';
846
+ item.appendChild(canvas);
843
847
  wrapper.appendChild(item);
844
848
  });
845
849
 
846
- // WORKAROUND: https://github.com/karma-runner/karma-jasmine/issues/139
847
- wrapper.indexOf = function() {
848
- return -1;
849
- };
850
+ wrapper.toString = () => `Fixture test failed:
851
+ Difference: ${count}px / ${toPercent(ratio)}%
852
+ Threshold: ${toPercent(threshold)}%
853
+ Tolerance: ${toPercent(tolerance)}%`;
850
854
 
851
855
  return wrapper;
852
856
  }
@@ -976,7 +980,7 @@ function toEqualImageData() {
976
980
  var debug = opts.debug || false;
977
981
  var tolerance = opts.tolerance === undefined ? 0.001 : opts.tolerance;
978
982
  var threshold = opts.threshold === undefined ? 0.1 : opts.threshold;
979
- var ctx, idata, ddata, w, h, count, ratio;
983
+ var ctx, idata, ddata, w, h, aw, ah, count, ratio;
980
984
 
981
985
  if (actual instanceof Chart) {
982
986
  ctx = actual.ctx;
@@ -989,9 +993,15 @@ function toEqualImageData() {
989
993
  if (ctx) {
990
994
  h = expected.height;
991
995
  w = expected.width;
992
- idata = ctx.getImageData(0, 0, w, h);
996
+ aw = ctx.canvas.width;
997
+ ah = ctx.canvas.height;
998
+ idata = ctx.getImageData(0, 0, aw, ah);
993
999
  ddata = createImageData(w, h);
994
- count = pixelmatch_1(idata.data, expected.data, ddata.data, w, h, {threshold: threshold});
1000
+ if (aw === w && ah === h) {
1001
+ count = pixelmatch_1(idata.data, expected.data, ddata.data, w, h, {threshold: threshold});
1002
+ } else {
1003
+ count = Math.abs(aw * ah - w * h);
1004
+ }
995
1005
  ratio = count / (w * h);
996
1006
 
997
1007
  if ((ratio > tolerance) || debug) {
@@ -1116,13 +1126,13 @@ var charts = {};
1116
1126
  /**
1117
1127
  * Injects a new canvas (and div wrapper) and creates the associated Chart instance
1118
1128
  * using the given config. Additional options allow tweaking elements generation.
1119
- * @param {object} config - Chart config.
1120
- * @param {object} options - Chart acquisition options.
1121
- * @param {object} options.canvas - Canvas attributes.
1122
- * @param {object} options.wrapper - Canvas wrapper attributes.
1123
- * @param {boolean} options.useOffscreenCanvas - use an OffscreenCanvas instead of the normal HTMLCanvasElement.
1124
- * @param {boolean} options.useShadowDOM - use shadowDom
1125
- * @param {boolean} options.persistent - If true, the chart will not be released after the spec.
1129
+ * @param {object} [config] - Chart config.
1130
+ * @param {object} [options] - Chart acquisition options.
1131
+ * @param {object} [options.canvas] - Canvas attributes.
1132
+ * @param {object} [options.wrapper] - Canvas wrapper attributes.
1133
+ * @param {boolean} [options.useOffscreenCanvas] - use an OffscreenCanvas instead of the normal HTMLCanvasElement.
1134
+ * @param {boolean} [options.useShadowDOM] - use shadowDom
1135
+ * @param {boolean} [options.persistent] - If true, the chart will not be released after the spec.
1126
1136
  */
1127
1137
  function acquireChart(config, options) {
1128
1138
  var chart = _acquireChart(config, options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chartjs-test-utils",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Chart.js test utils",
5
5
  "main": "dist/chartjs-test-utils.js",
6
6
  "module": "dist/chartjs-test-utils.js",
@@ -30,19 +30,21 @@
30
30
  },
31
31
  "homepage": "https://github.com/chartjs/chartjs-test-utils#readme",
32
32
  "devDependencies": {
33
- "@rollup/plugin-commonjs": "^17.1.0",
34
- "@rollup/plugin-node-resolve": "^11.1.1",
35
- "eslint": "^7.19.0",
33
+ "@rollup/plugin-commonjs": "^20.0.0",
34
+ "@rollup/plugin-node-resolve": "^13.0.0",
35
+ "eslint": "^7.20.0",
36
36
  "eslint-config-chartjs": "^0.3.0",
37
37
  "eslint-plugin-es": "^4.1.0",
38
38
  "esm": "^3.2.25",
39
- "mocha": "^8.2.1",
40
- "rollup": "^2.38.5"
39
+ "mocha": "^8.3.0",
40
+ "rollup": "^2.39.0"
41
41
  },
42
42
  "dependencies": {
43
- "jasmine": "^3.6.4",
44
- "karma": "^6.1.0",
45
- "karma-jasmine": "^4.0.1",
46
43
  "pixelmatch": "^5.2.1"
44
+ },
45
+ "peerDependencies": {
46
+ "jasmine": "^3.6.4",
47
+ "karma": "^6.1.1",
48
+ "karma-jasmine": "^4.0.1"
47
49
  }
48
50
  }