|
1 | 1 | import Jimp from 'jimp'; |
2 | 2 | import compareImage from './compareImage'; |
3 | | -import logger from './logger'; |
4 | | -import { globalConfig } from './defaultConfig'; |
5 | 3 |
|
6 | | -Jimp.read = path => |
7 | | - new Promise((resolve, reject) => { |
8 | | - if (path === './screenshots/test1.png' || path === './differencify_report/test1.png') { |
9 | | - return resolve('image'); |
| 4 | +const mockConfig = { |
| 5 | + screenshots: './screenshots', |
| 6 | + testReportPath: './differencify_report', |
| 7 | + saveDifferencifiedImage: false, |
| 8 | + mismatchThreshold: 0.01, |
| 9 | +}; |
| 10 | + |
| 11 | +jest.mock('jimp', () => ({ |
| 12 | + read: jest.fn(), |
| 13 | + distance: jest.fn(), |
| 14 | + diff: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | + |
| 18 | +jest.mock('./logger', () => ({ |
| 19 | + log: jest.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +describe('Compare Image', () => { |
| 23 | + beforeEach(() => { |
| 24 | + Jimp.distance.mockReturnValue(0); |
| 25 | + Jimp.diff.mockReturnValue({ percent: 0 }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('calls Jimp with correct image names', async () => { |
| 29 | + expect.assertions(2); |
| 30 | + await compareImage(mockConfig, 'test'); |
| 31 | + expect(Jimp.read).toHaveBeenCalledWith('./differencify_report/test.png'); |
| 32 | + expect(Jimp.read).toHaveBeenCalledWith('./screenshots/test.png'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('throws correct error if it cannot read image', async () => { |
| 36 | + expect.assertions(2); |
| 37 | + Jimp.read |
| 38 | + .mockReturnValueOnce(Promise.reject('error1')) |
| 39 | + .mockReturnValueOnce(Promise.resolve()) |
| 40 | + .mockReturnValueOnce(Promise.reject('error2')); |
| 41 | + |
| 42 | + try { |
| 43 | + await compareImage(mockConfig, 'test1'); |
| 44 | + } catch (err) { |
| 45 | + expect(err.message).toEqual('test1: failed to read reference image error1'); |
| 46 | + } |
| 47 | + |
| 48 | + try { |
| 49 | + expect(await compareImage(mockConfig, 'test2')).toThrow(); |
| 50 | + } catch (err) { |
| 51 | + expect(err.message).toEqual('test2: failed to read test image error2'); |
10 | 52 | } |
11 | | - return reject('error'); |
12 | 53 | }); |
13 | 54 |
|
14 | | -Jimp.distance = (referenceImage, testImage) => { |
15 | | - if (referenceImage === 'image' && testImage === 'image') { |
16 | | - return 0; |
17 | | - } |
18 | | - return 1; |
19 | | -}; |
| 55 | + it('returns correct value if difference below threshold', async () => { |
| 56 | + const result = await compareImage(mockConfig, 'test'); |
| 57 | + expect(result).toEqual('test: no mismatch found ✅'); |
| 58 | + }); |
20 | 59 |
|
21 | | -Jimp.diff = () => jest.fn(); |
| 60 | + it('returns correct value if only difference above threshold', async () => { |
| 61 | + Jimp.diff.mockReturnValue({ percent: 0.02 }); |
22 | 62 |
|
23 | | -let loggerCalls = []; |
24 | | -logger.log = (...args) => { |
25 | | - loggerCalls.push(...args); |
26 | | -}; |
| 63 | + const result = await compareImage(mockConfig, 'test'); |
| 64 | + expect(result).toEqual('test: no mismatch found ✅'); |
| 65 | + }); |
27 | 66 |
|
28 | | -describe('Compare Image', () => { |
29 | | - afterEach(() => { |
30 | | - loggerCalls = []; |
31 | | - }); |
32 | | - it('returns correct values', async () => |
33 | | - compareImage(globalConfig, 'test1') |
34 | | - .catch((result) => { |
35 | | - expect(result).toEqual('Failed to read test image!'); |
36 | | - expect(loggerCalls[0]).toEqual('Saving the diff image to disk'); |
37 | | - })); |
| 67 | + it('returns correct value if only distance above threshold', async () => { |
| 68 | + Jimp.distance.mockReturnValue(0.02); |
| 69 | + |
| 70 | + const result = await compareImage(mockConfig, 'test'); |
| 71 | + expect(result).toEqual('test: no mismatch found ✅'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('throws error if distance and difference are above threshold', async () => { |
| 75 | + expect.assertions(1); |
| 76 | + Jimp.distance.mockReturnValue(0.02); |
| 77 | + Jimp.diff.mockReturnValue({ percent: 0.02 }); |
| 78 | + |
| 79 | + try { |
| 80 | + await compareImage(mockConfig, 'test'); |
| 81 | + } catch (err) { |
| 82 | + expect(err.message).toEqual(`test: mismatch found❗ |
| 83 | + Result: |
| 84 | + distance: 0.02 |
| 85 | + diff: 0.02 |
| 86 | + misMatchThreshold: 0.01 |
| 87 | + `); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it('writes to disk diff image if saveDifferencifiedImage is true', async () => { |
| 92 | + expect.assertions(1); |
| 93 | + Jimp.distance.mockReturnValue(0.02); |
| 94 | + const mockWrite = jest.fn(); |
| 95 | + Jimp.diff.mockReturnValue({ |
| 96 | + percent: 0.02, |
| 97 | + image: { |
| 98 | + write: mockWrite, |
| 99 | + }, |
| 100 | + }); |
| 101 | + try { |
| 102 | + // eslint-disable-next-line prefer-object-spread/prefer-object-spread |
| 103 | + await compareImage(Object.assign({}, mockConfig, { saveDifferencifiedImage: true }), 'test'); |
| 104 | + } catch (err) { |
| 105 | + expect(mockWrite).toHaveBeenCalledWith('./differencify_report/test_differencified.png'); |
| 106 | + } |
| 107 | + }); |
38 | 108 | }); |
0 commit comments