2018-10-30 07:46:44 -04:00
|
|
|
import pixelmatch from 'pixelmatch';
|
|
|
|
|
2018-04-08 06:20:05 -04:00
|
|
|
export default {
|
2018-10-30 07:46:44 -04:00
|
|
|
toImageDiffEqual: () => {
|
2020-12-23 16:10:24 -05:00
|
|
|
const getImageData = (img) => {
|
2018-10-30 07:46:44 -04:00
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
canvas.width = img.width;
|
|
|
|
canvas.height = img.height;
|
|
|
|
canvas.getContext('2d').drawImage(img, 0, 0);
|
|
|
|
return canvas.getContext('2d').getImageData(0, 0, img.width, img.height).data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
compare(actual, expected, threshold = 0.1) {
|
|
|
|
if (actual.height !== expected.height || actual.width !== expected.width) {
|
|
|
|
return {
|
|
|
|
pass: false,
|
|
|
|
message: `Expected image dimensions (h x w) of ${expected.height}x${expected.width}.
|
|
|
|
Received an image with ${actual.height}x${actual.width}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const { width, height } = actual;
|
|
|
|
const differentPixels = pixelmatch(
|
|
|
|
getImageData(actual),
|
|
|
|
getImageData(expected),
|
|
|
|
null,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
{ threshold },
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
pass: differentPixels < 20,
|
2020-12-23 07:10:26 -05:00
|
|
|
message: `${differentPixels} pixels differ more than ${
|
|
|
|
threshold * 100
|
|
|
|
} percent between input and output.`,
|
2018-10-30 07:46:44 -04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2018-04-08 06:20:05 -04:00
|
|
|
};
|