chartjs-test-utils 0.2.0 → 0.3.1
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.
- package/README.md +1 -0
- package/dist/chartjs-test-utils.js +133 -11
- package/package.json +9 -7
package/README.md
CHANGED
|
@@ -4,3 +4,4 @@ Chart.js test utils package. For usage examples, take a look at these repositori
|
|
|
4
4
|
|
|
5
5
|
- [Chart.js](https://github.com/chartjs/Chart.js)
|
|
6
6
|
- [chartjs-plugin-annotation](https://github.com/chartjs/chartjs-plugin-annotation)
|
|
7
|
+
- [chartjs-plugin-datalabels](https://github.com/chartjs/chartjs-plugin-datalabels)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* chartjs-test-utils v0.
|
|
2
|
+
* chartjs-test-utils v0.3.1
|
|
3
3
|
* https://github.com/chartjs/chartjs-test-utils#readme
|
|
4
4
|
* (c) 2021 chartjs-plugin-annotation Contributors
|
|
5
5
|
* Released under the MIT License
|
|
@@ -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();
|
|
@@ -689,6 +689,119 @@ async function triggerMouseEvent(chart, type, el) {
|
|
|
689
689
|
await promise;
|
|
690
690
|
}
|
|
691
691
|
|
|
692
|
+
function isObject(value) {
|
|
693
|
+
return Object.prototype.toString.call(value) === '[object Object]';
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
function isArray(value) {
|
|
697
|
+
return Object.prototype.toString.call(value) === '[object Array]';
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function fmt(val) {
|
|
701
|
+
if (typeof val === 'string') {
|
|
702
|
+
return `"${val}"`;
|
|
703
|
+
}
|
|
704
|
+
if (isArray(val) || isObject(val)) {
|
|
705
|
+
return JSON.stringify(val);
|
|
706
|
+
}
|
|
707
|
+
return `${val}`;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function compareArray(actual, expected, path) {
|
|
711
|
+
let ret = true;
|
|
712
|
+
const diff = [];
|
|
713
|
+
|
|
714
|
+
if (!isArray(actual)) {
|
|
715
|
+
diff.push(`${path}: Expected ${fmt(actual)} to be an array`);
|
|
716
|
+
ret = false;
|
|
717
|
+
actual = {};
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (actual.length !== expected.length) {
|
|
721
|
+
diff.push(`${path}.length: Expected ${actual.length} to equal ${expected.length}`);
|
|
722
|
+
ret = false;
|
|
723
|
+
}
|
|
724
|
+
for (let i = 0; i < expected.length; i++) {
|
|
725
|
+
const act = actual[i];
|
|
726
|
+
const exp = expected[i];
|
|
727
|
+
const cmp = compareOption(act, exp, `${path}[${i}]`);
|
|
728
|
+
if (isObject(cmp)) {
|
|
729
|
+
if (!cmp.pass) {
|
|
730
|
+
diff.push(cmp.message);
|
|
731
|
+
ret = false;
|
|
732
|
+
}
|
|
733
|
+
} else if (!cmp) {
|
|
734
|
+
diff.push(`${path}[${i}]: Expected ${fmt(act)} to equal ${fmt(exp)}`);
|
|
735
|
+
ret = false;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return {
|
|
739
|
+
pass: ret,
|
|
740
|
+
message: diff.join('\n')
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function compareObject(actual, expected, path = '') {
|
|
745
|
+
let ret = true;
|
|
746
|
+
const diff = [];
|
|
747
|
+
|
|
748
|
+
if (!isObject(actual)) {
|
|
749
|
+
diff.push(`${path}: Expected ${fmt(actual)} to be an object`);
|
|
750
|
+
ret = false;
|
|
751
|
+
actual = {};
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
if (path !== '') {
|
|
755
|
+
path = path + '.';
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
for (const key in expected) {
|
|
759
|
+
if (typeof key === 'string' && key.startsWith('_')) {
|
|
760
|
+
continue;
|
|
761
|
+
}
|
|
762
|
+
if (!Object.prototype.hasOwnProperty.call(expected, key)) {
|
|
763
|
+
continue;
|
|
764
|
+
}
|
|
765
|
+
const act = actual[key];
|
|
766
|
+
const exp = expected[key];
|
|
767
|
+
const cmp = compareOption(act, exp, `${path}${key}`);
|
|
768
|
+
if (isObject(cmp)) {
|
|
769
|
+
if (!cmp.pass) {
|
|
770
|
+
diff.push(cmp.message);
|
|
771
|
+
ret = false;
|
|
772
|
+
}
|
|
773
|
+
} else if (!cmp) {
|
|
774
|
+
diff.push(`${path}${key}: Expected ${fmt(act)} to equal ${fmt(exp)}`);
|
|
775
|
+
ret = false;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return {
|
|
779
|
+
pass: ret,
|
|
780
|
+
message: diff.join('\n')
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
function compareOption(actual, expected, path) {
|
|
785
|
+
let ret = true;
|
|
786
|
+
|
|
787
|
+
if (isObject(expected)) {
|
|
788
|
+
ret = compareObject(actual, expected, path);
|
|
789
|
+
} else if (isArray(expected)) {
|
|
790
|
+
ret = compareArray(actual, expected, path);
|
|
791
|
+
} else {
|
|
792
|
+
ret = actual === expected;
|
|
793
|
+
}
|
|
794
|
+
return ret;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
function toEqualOptions() {
|
|
798
|
+
return {
|
|
799
|
+
compare(actual, expected) {
|
|
800
|
+
return compareObject(actual, expected);
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
|
|
692
805
|
function toPercent(value) {
|
|
693
806
|
return Math.round(value * 10000) / 100;
|
|
694
807
|
}
|
|
@@ -726,14 +839,16 @@ function buildPixelMatchPreview(actual, expected, diff, threshold, tolerance, co
|
|
|
726
839
|
var item = document.createElement('div');
|
|
727
840
|
item.style.cssText = 'text-align: center; font: 12px monospace; line-height: 1.4; margin: 8px';
|
|
728
841
|
item.innerHTML = '<div style="margin: 8px; height: 32px">' + values.label + '</div>';
|
|
729
|
-
|
|
842
|
+
var canvas = canvasFromImageData(values.data);
|
|
843
|
+
canvas.style.cssText = 'border: 1px dashed red';
|
|
844
|
+
item.appendChild(canvas);
|
|
730
845
|
wrapper.appendChild(item);
|
|
731
846
|
});
|
|
732
847
|
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
}
|
|
848
|
+
wrapper.toString = () => `Fixture test failed:
|
|
849
|
+
Difference: ${count}px / ${toPercent(ratio)}%
|
|
850
|
+
Threshold: ${toPercent(threshold)}%
|
|
851
|
+
Tolerance: ${toPercent(tolerance)}%`;
|
|
737
852
|
|
|
738
853
|
return wrapper;
|
|
739
854
|
}
|
|
@@ -863,7 +978,7 @@ function toEqualImageData() {
|
|
|
863
978
|
var debug = opts.debug || false;
|
|
864
979
|
var tolerance = opts.tolerance === undefined ? 0.001 : opts.tolerance;
|
|
865
980
|
var threshold = opts.threshold === undefined ? 0.1 : opts.threshold;
|
|
866
|
-
var ctx, idata, ddata, w, h, count, ratio;
|
|
981
|
+
var ctx, idata, ddata, w, h, aw, ah, count, ratio;
|
|
867
982
|
|
|
868
983
|
if (actual instanceof Chart) {
|
|
869
984
|
ctx = actual.ctx;
|
|
@@ -876,9 +991,15 @@ function toEqualImageData() {
|
|
|
876
991
|
if (ctx) {
|
|
877
992
|
h = expected.height;
|
|
878
993
|
w = expected.width;
|
|
879
|
-
|
|
994
|
+
aw = ctx.canvas.width;
|
|
995
|
+
ah = ctx.canvas.height;
|
|
996
|
+
idata = ctx.getImageData(0, 0, aw, ah);
|
|
880
997
|
ddata = createImageData(w, h);
|
|
881
|
-
|
|
998
|
+
if (aw === w && ah === h) {
|
|
999
|
+
count = pixelmatch_1(idata.data, expected.data, ddata.data, w, h, {threshold: threshold});
|
|
1000
|
+
} else {
|
|
1001
|
+
count = Math.abs(aw * ah - w * h);
|
|
1002
|
+
}
|
|
882
1003
|
ratio = count / (w * h);
|
|
883
1004
|
|
|
884
1005
|
if ((ratio > tolerance) || debug) {
|
|
@@ -902,7 +1023,8 @@ var matchers = {
|
|
|
902
1023
|
toEqualOneOf,
|
|
903
1024
|
toBeValidChart,
|
|
904
1025
|
toBeChartOfSize,
|
|
905
|
-
toEqualImageData
|
|
1026
|
+
toEqualImageData,
|
|
1027
|
+
toEqualOptions
|
|
906
1028
|
};
|
|
907
1029
|
|
|
908
1030
|
function readFile(url, callback) {
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chartjs-test-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Chart.js test utils",
|
|
5
5
|
"main": "dist/chartjs-test-utils.js",
|
|
6
6
|
"module": "dist/chartjs-test-utils.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "rollup -c",
|
|
9
9
|
"lint": "eslint src/**/*.js",
|
|
10
|
-
"test": "npm run lint"
|
|
10
|
+
"test": "npm run lint && npx mocha -r esm"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -30,16 +30,18 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/chartjs/chartjs-test-utils#readme",
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@rollup/plugin-commonjs": "^
|
|
34
|
-
"@rollup/plugin-node-resolve": "^
|
|
35
|
-
"eslint": "^7.
|
|
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.3.0",
|
|
40
|
+
"rollup": "^2.39.0"
|
|
39
41
|
},
|
|
40
42
|
"dependencies": {
|
|
41
43
|
"jasmine": "^3.6.4",
|
|
42
|
-
"karma": "^6.1.
|
|
44
|
+
"karma": "^6.1.1",
|
|
43
45
|
"karma-jasmine": "^4.0.1",
|
|
44
46
|
"pixelmatch": "^5.2.1"
|
|
45
47
|
}
|