Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update toEqualImageData to verify size #11

Merged
merged 1 commit into from May 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/matchers.js
Expand Up @@ -41,7 +41,9 @@ function buildPixelMatchPreview(actual, expected, diff, threshold, tolerance, co
var item = document.createElement('div');
item.style.cssText = 'text-align: center; font: 12px monospace; line-height: 1.4; margin: 8px';
item.innerHTML = '<div style="margin: 8px; height: 32px">' + values.label + '</div>';
item.appendChild(canvasFromImageData(values.data));
var canvas = canvasFromImageData(values.data);
canvas.style.cssText = 'border: 1px dashed red';
item.appendChild(canvas);
wrapper.appendChild(item);
});

Expand Down Expand Up @@ -178,7 +180,7 @@ function toEqualImageData() {
var debug = opts.debug || false;
var tolerance = opts.tolerance === undefined ? 0.001 : opts.tolerance;
var threshold = opts.threshold === undefined ? 0.1 : opts.threshold;
var ctx, idata, ddata, w, h, count, ratio;
var ctx, idata, ddata, w, h, aw, ah, count, ratio;

if (actual instanceof Chart) {
ctx = actual.ctx;
Expand All @@ -191,9 +193,15 @@ function toEqualImageData() {
if (ctx) {
h = expected.height;
w = expected.width;
idata = ctx.getImageData(0, 0, w, h);
aw = ctx.canvas.width;
ah = ctx.canvas.height;
idata = ctx.getImageData(0, 0, aw, ah);
ddata = createImageData(w, h);
count = pixelmatch(idata.data, expected.data, ddata.data, w, h, {threshold: threshold});
if (aw === w && ah === h) {
count = pixelmatch(idata.data, expected.data, ddata.data, w, h, {threshold: threshold});
} else {
count = Math.abs(aw * ah - w * h);
}
ratio = count / (w * h);

if ((ratio > tolerance) || debug) {
Expand Down