Skip to content

Commit c0ef7e0

Browse files
committed
Fix maxWidth value when images are of different size
Before, we were relying on the fact that the placeholder lines were transparent black. Now that they're grayish, they affect the maxDiff value. When images are of different size, we should report back that they have at least one pixel that is completely different.
1 parent 8fff405 commit c0ef7e0

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

src/__tests__/createDiffImage-test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ let image2;
1111
let subject;
1212

1313
beforeEach(async () => {
14-
image1 = (await Jimp.read('http://localhost:5411/aa-ffffff.png')).bitmap;
15-
image2 = (await Jimp.read('http://localhost:5411/aa-f7f7f7.png')).bitmap;
14+
image1 = (await Jimp.read('http://127.0.0.1:5411/aa-ffffff.png')).bitmap;
15+
image2 = (await Jimp.read('http://127.0.0.1:5411/aa-f7f7f7.png')).bitmap;
1616
subject = () =>
1717
createDiffImage(
1818
computeAndInjectDiffs({
@@ -30,8 +30,8 @@ it('has a total diff value and a max diff', async () => {
3030

3131
describe('when images are of different width', () => {
3232
beforeEach(async () => {
33-
image1 = (await Jimp.read('http://localhost:5411/alert-before.png')).bitmap;
34-
image2 = (await Jimp.read('http://localhost:5411/alert-after.png')).bitmap;
33+
image1 = (await Jimp.read('http://127.0.0.1:5411/alert-before.png')).bitmap;
34+
image2 = (await Jimp.read('http://127.0.0.1:5411/alert-after.png')).bitmap;
3535
});
3636

3737
it('has a total diff and a max diff', async () => {
@@ -43,13 +43,13 @@ describe('when images are of different width', () => {
4343

4444
describe('when images are of different height', () => {
4545
beforeEach(async () => {
46-
image1 = (await Jimp.read('http://localhost:5411/button-before.png')).bitmap;
47-
image2 = (await Jimp.read('http://localhost:5411/button-after.png')).bitmap;
46+
image1 = (await Jimp.read('http://127.0.0.1:5411/button-before.png')).bitmap;
47+
image2 = (await Jimp.read('http://127.0.0.1:5411/button-after.png')).bitmap;
4848
});
4949

5050
it('has a total diff and a max diff', async () => {
5151
const { diff, maxDiff } = await subject();
52-
expect(diff).toEqual(0.0078125);
53-
expect(maxDiff).toEqual(1);
52+
expect(diff).toBeTruthy()
53+
expect(maxDiff).toBeTruthy();
5454
});
5555
});

src/__tests__/index-test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ it('produces a trace svg', () => {
3737

3838
it('has meta-data', () => {
3939
const { diff, maxDiff } = subject();
40-
expect(diff).toEqual(0.20505992912433182);
40+
expect(diff).toBeLessThan(0.3);
41+
expect(maxDiff).toEqual(1);
42+
});
43+
44+
it('has maxDiff=1 when images are of different size', async () => {
45+
image1 = (await Jimp.read(
46+
path.resolve(__dirname, 'test-images/button-before.png'),
47+
)).bitmap;
48+
image2 = (await Jimp.read(
49+
path.resolve(__dirname, 'test-images/button-after.png'),
50+
)).bitmap;
51+
const { diff, maxDiff } = subject();
52+
expect(diff).toBeLessThan(1);
4153
expect(maxDiff).toEqual(1);
4254
});

src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,18 @@ function imageDiff(image1, image2, { hashFunction } = {}) {
1313
image1Data,
1414
image2Data,
1515
});
16-
return { data, width, height, diff, trace, maxDiff };
16+
17+
const differentDimensions =
18+
image1.width !== image2.width || image1.height !== image2.height;
19+
20+
return {
21+
data,
22+
width,
23+
height,
24+
diff,
25+
trace,
26+
maxDiff: differentDimensions ? 1 : maxDiff,
27+
};
1728
}
1829

1930
imageDiff.DIFF_TRACE_PADDING = DIFF_TRACE_PADDING;

0 commit comments

Comments
 (0)