-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathcreate-pixelmatch-differ.spec.js
83 lines (74 loc) · 2.75 KB
/
create-pixelmatch-differ.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs-extra');
const path = require('path');
const { PNG } = require('pngjs');
const createPixelmatchDiffer = require('./create-pixelmatch-differ');
const workingDirectory = `./pixelmatch-${Math.round(Math.random() * 1000)}`;
const darkGrayPath = path.join(workingDirectory, 'dark-dray.png');
const lightGrayPath = path.join(workingDirectory, 'light-dray.png');
function writeBase64Image({ outputPath, base64String }) {
const imageData = base64String.split(';base64,').pop();
fs.ensureFileSync(outputPath);
fs.writeFileSync(outputPath, imageData, { encoding: 'base64' });
}
describe('createPixelmatchDiffer', () => {
beforeEach(() => {
// 1x1 png picture
const darkGrayBase64String =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQYV2NISUn5DwAEiAIshLJN6AAAAABJRU5ErkJggg==';
writeBase64Image({
outputPath: darkGrayPath,
base64String: darkGrayBase64String,
});
// 1x1 png picture
const lightGrayBase64String =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQYV2M4ceLEfwAIDANYMDoQswAAAABJRU5ErkJggg==';
writeBase64Image({
outputPath: lightGrayPath,
base64String: lightGrayBase64String,
});
});
afterEach(() => {
fs.rmdirSync(workingDirectory, { recursive: true });
});
it('creates diff files in deeply nested directories', async () => {
const config = {};
const pixelmatchDiffer = createPixelmatchDiffer(config);
const diffPath = path.join(
workingDirectory,
'deeply',
'nested',
'diff.png'
);
const tolerance = 0;
await pixelmatchDiffer(darkGrayPath, lightGrayPath, diffPath, tolerance);
expect(fs.existsSync(diffPath)).toEqual(true);
});
it('should output only the diff image by default', async () => {
const config = {};
const diffPath = path.join(workingDirectory, 'diff.png');
const pixelmatchDiffer = createPixelmatchDiffer(config);
const tolerance = 0;
await pixelmatchDiffer(darkGrayPath, lightGrayPath, diffPath, tolerance);
const outputPNG = PNG.sync.read(fs.readFileSync(diffPath));
expect(outputPNG).toEqual(
expect.objectContaining({
width: 1,
height: 1,
})
);
});
it('should be able to save the image with previous and current version along with diff', async () => {
const config = {};
const diffPath = path.join(workingDirectory, 'diff.png');
const pixelmatchDiffer = createPixelmatchDiffer(config, true);
const tolerance = 0;
await pixelmatchDiffer(darkGrayPath, lightGrayPath, diffPath, tolerance);
const outputPNG = PNG.sync.read(fs.readFileSync(diffPath));
expect(outputPNG).toEqual(
expect.objectContaining({
width: 3,
height: 1,
})
);
});
});