Skip to content

Commit a5435e4

Browse files
committed
test: add createCompareReport tests
1 parent ba3af64 commit a5435e4

2 files changed

Lines changed: 378 additions & 0 deletions

File tree

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`createCompareReport > should create a report for desktop browser 1`] = `
4+
{
5+
"boundingBoxes": {
6+
"diffBoundingBoxes": [],
7+
"ignoredBoxes": [],
8+
},
9+
"commandName": "test-command",
10+
"description": "test parent",
11+
"fileData": {
12+
"actualFilePath": "/actual/test.png",
13+
"baselineFilePath": "/baseline/test.png",
14+
"diffFilePath": "/diff/test.png",
15+
"fileName": "test.png",
16+
"size": {
17+
"actual": {
18+
"height": 100,
19+
"width": 100,
20+
},
21+
"baseline": {
22+
"height": 100,
23+
"width": 100,
24+
},
25+
"diff": {
26+
"height": 100,
27+
"width": 100,
28+
},
29+
},
30+
},
31+
"framework": "wdio",
32+
"instanceData": {
33+
"browser": {
34+
"name": "chrome",
35+
"version": "100",
36+
},
37+
"platform": {
38+
"name": "windows",
39+
"version": "10",
40+
},
41+
},
42+
"misMatchPercentage": "0",
43+
"rawMisMatchPercentage": 0,
44+
"tag": "test-tag",
45+
"test": "test title",
46+
}
47+
`;
48+
49+
exports[`createCompareReport > should create a report for mobile app 1`] = `
50+
{
51+
"boundingBoxes": {
52+
"diffBoundingBoxes": [],
53+
"ignoredBoxes": [],
54+
},
55+
"commandName": "test-command",
56+
"description": "test parent",
57+
"fileData": {
58+
"actualFilePath": "/actual/test.png",
59+
"baselineFilePath": "/baseline/test.png",
60+
"diffFilePath": "/diff/test.png",
61+
"fileName": "test.png",
62+
"size": {
63+
"actual": {
64+
"height": 100,
65+
"width": 100,
66+
},
67+
"baseline": {
68+
"height": 100,
69+
"width": 100,
70+
},
71+
"diff": {
72+
"height": 100,
73+
"width": 100,
74+
},
75+
},
76+
},
77+
"framework": "wdio",
78+
"instanceData": {
79+
"app": "my-app",
80+
"deviceName": "Pixel 6",
81+
"platform": {
82+
"name": "android",
83+
"version": "12",
84+
},
85+
},
86+
"misMatchPercentage": "0",
87+
"rawMisMatchPercentage": 0,
88+
"tag": "test-tag",
89+
"test": "test title",
90+
}
91+
`;
92+
93+
exports[`createCompareReport > should create a report for mobile browser 1`] = `
94+
{
95+
"boundingBoxes": {
96+
"diffBoundingBoxes": [],
97+
"ignoredBoxes": [],
98+
},
99+
"commandName": "test-command",
100+
"description": "test parent",
101+
"fileData": {
102+
"actualFilePath": "/actual/test.png",
103+
"baselineFilePath": "/baseline/test.png",
104+
"diffFilePath": "/diff/test.png",
105+
"fileName": "test.png",
106+
"size": {
107+
"actual": {
108+
"height": 100,
109+
"width": 100,
110+
},
111+
"baseline": {
112+
"height": 100,
113+
"width": 100,
114+
},
115+
"diff": {
116+
"height": 100,
117+
"width": 100,
118+
},
119+
},
120+
},
121+
"framework": "wdio",
122+
"instanceData": {
123+
"browser": {
124+
"name": "safari",
125+
"version": "15",
126+
},
127+
"deviceName": "iPhone 12",
128+
"platform": {
129+
"name": "ios",
130+
"version": "15",
131+
},
132+
},
133+
"misMatchPercentage": "0",
134+
"rawMisMatchPercentage": 0,
135+
"tag": "test-tag",
136+
"test": "test title",
137+
}
138+
`;
139+
140+
exports[`createCompareReport > should create a report without diff file path 1`] = `
141+
{
142+
"boundingBoxes": {
143+
"diffBoundingBoxes": [],
144+
"ignoredBoxes": [],
145+
},
146+
"commandName": "test-command",
147+
"description": "test parent",
148+
"fileData": {
149+
"actualFilePath": "/actual/test.png",
150+
"baselineFilePath": "/baseline/test.png",
151+
"fileName": "test.png",
152+
"size": {
153+
"actual": {
154+
"height": 100,
155+
"width": 100,
156+
},
157+
"baseline": {
158+
"height": 100,
159+
"width": 100,
160+
},
161+
"diff": {
162+
"height": 100,
163+
"width": 100,
164+
},
165+
},
166+
},
167+
"framework": "wdio",
168+
"instanceData": {
169+
"browser": {
170+
"name": "chrome",
171+
"version": "100",
172+
},
173+
"platform": {
174+
"name": "windows",
175+
"version": "10",
176+
},
177+
},
178+
"misMatchPercentage": "0",
179+
"rawMisMatchPercentage": 0,
180+
"tag": "test-tag",
181+
"test": "test title",
182+
}
183+
`;
184+
185+
exports[`createCompareReport > should include misMatchPercentage in the report 1`] = `
186+
{
187+
"boundingBoxes": {
188+
"diffBoundingBoxes": [],
189+
"ignoredBoxes": [],
190+
},
191+
"commandName": "test-command",
192+
"description": "test parent",
193+
"fileData": {
194+
"actualFilePath": "/actual/test.png",
195+
"baselineFilePath": "/baseline/test.png",
196+
"diffFilePath": "/diff/test.png",
197+
"fileName": "test.png",
198+
"size": {
199+
"actual": {
200+
"height": 100,
201+
"width": 100,
202+
},
203+
"baseline": {
204+
"height": 100,
205+
"width": 100,
206+
},
207+
"diff": {
208+
"height": 100,
209+
"width": 100,
210+
},
211+
},
212+
},
213+
"framework": "wdio",
214+
"instanceData": {
215+
"browser": {
216+
"name": "chrome",
217+
"version": "100",
218+
},
219+
"platform": {
220+
"name": "windows",
221+
"version": "10",
222+
},
223+
},
224+
"misMatchPercentage": "5.5",
225+
"rawMisMatchPercentage": 5.5,
226+
"tag": "test-tag",
227+
"test": "test title",
228+
}
229+
`;
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest'
2+
import { writeFileSync } from 'node:fs'
3+
import { createCompareReport } from './createCompareReport.js'
4+
import type { CompareData } from '../resemble/compare.interfaces.js'
5+
import type { BoundingBox, IgnoreBoxes } from './images.interfaces.js'
6+
import type { BaseDimensions } from '../base.interfaces.js'
7+
8+
vi.mock('node:fs', () => ({
9+
writeFileSync: vi.fn(),
10+
}))
11+
12+
describe('createCompareReport', () => {
13+
const createMockData = (misMatchPercentage = 0): CompareData => ({
14+
misMatchPercentage,
15+
rawMisMatchPercentage: misMatchPercentage,
16+
getBuffer: () => Buffer.from(''),
17+
diffBounds: { top: 0, left: 0, bottom: 0, right: 0 },
18+
analysisTime: 0,
19+
diffPixels: [],
20+
})
21+
22+
const createMockFolders = (includeDiff = true) => ({
23+
actualFolderPath: '/actual',
24+
baselineFolderPath: '/baseline',
25+
...(includeDiff && { diffFolderPath: '/diff' }),
26+
})
27+
28+
const createMockSize = (): { actual: BaseDimensions; baseline: BaseDimensions; diff: BaseDimensions } => ({
29+
actual: { width: 100, height: 100 },
30+
baseline: { width: 100, height: 100 },
31+
diff: { width: 100, height: 100 },
32+
})
33+
34+
const createMockBoundingBoxes = () => ({
35+
diffBoundingBoxes: [] as BoundingBox[],
36+
ignoredBoxes: [] as IgnoreBoxes[],
37+
})
38+
39+
const createTestContext = (options: {
40+
app?: string;
41+
browser?: { name: string; version: string };
42+
deviceName?: string;
43+
isIOS?: boolean;
44+
isAndroid?: boolean;
45+
isMobile?: boolean;
46+
platform?: { name: string; version: string };
47+
} = {}) => ({
48+
commandName: 'test-command',
49+
instanceData: {
50+
app: options.app ?? 'not-known',
51+
browser: options.browser ?? { name: 'chrome', version: '100' },
52+
deviceName: options.deviceName ?? 'desktop',
53+
isIOS: options.isIOS ?? false,
54+
isAndroid: options.isAndroid ?? false,
55+
isMobile: options.isMobile ?? false,
56+
platform: options.platform ?? { name: 'windows', version: '10' },
57+
},
58+
framework: 'wdio',
59+
parent: 'test parent',
60+
tag: 'test-tag',
61+
title: 'test title',
62+
})
63+
64+
beforeEach(() => {
65+
vi.clearAllMocks()
66+
})
67+
68+
it('should create a report for desktop browser', () => {
69+
createCompareReport({
70+
boundingBoxes: createMockBoundingBoxes(),
71+
data: createMockData(),
72+
fileName: 'test.png',
73+
folders: createMockFolders(),
74+
size: createMockSize(),
75+
testContext: createTestContext(),
76+
})
77+
78+
const writtenData = JSON.parse((writeFileSync as any).mock.calls[0][1])
79+
expect(writtenData).toMatchSnapshot()
80+
})
81+
82+
it('should create a report for mobile browser', () => {
83+
createCompareReport({
84+
boundingBoxes: createMockBoundingBoxes(),
85+
data: createMockData(),
86+
fileName: 'test.png',
87+
folders: createMockFolders(),
88+
size: createMockSize(),
89+
testContext: createTestContext({
90+
browser: { name: 'safari', version: '15' },
91+
deviceName: 'iPhone 12',
92+
isIOS: true,
93+
isMobile: true,
94+
platform: { name: 'ios', version: '15' },
95+
}),
96+
})
97+
98+
const writtenData = JSON.parse((writeFileSync as any).mock.calls[0][1])
99+
expect(writtenData).toMatchSnapshot()
100+
})
101+
102+
it('should create a report for mobile app', () => {
103+
createCompareReport({
104+
boundingBoxes: createMockBoundingBoxes(),
105+
data: createMockData(),
106+
fileName: 'test.png',
107+
folders: createMockFolders(),
108+
size: createMockSize(),
109+
testContext: createTestContext({
110+
app: 'my-app',
111+
deviceName: 'Pixel 6',
112+
isAndroid: true,
113+
isMobile: true,
114+
platform: { name: 'android', version: '12' },
115+
}),
116+
})
117+
118+
const writtenData = JSON.parse((writeFileSync as any).mock.calls[0][1])
119+
expect(writtenData).toMatchSnapshot()
120+
})
121+
122+
it('should create a report without diff file path', () => {
123+
createCompareReport({
124+
boundingBoxes: createMockBoundingBoxes(),
125+
data: createMockData(),
126+
fileName: 'test.png',
127+
folders: createMockFolders(false),
128+
size: createMockSize(),
129+
testContext: createTestContext(),
130+
})
131+
132+
const writtenData = JSON.parse((writeFileSync as any).mock.calls[0][1])
133+
expect(writtenData).toMatchSnapshot()
134+
})
135+
136+
it('should include misMatchPercentage in the report', () => {
137+
createCompareReport({
138+
boundingBoxes: createMockBoundingBoxes(),
139+
data: createMockData(5.5),
140+
fileName: 'test.png',
141+
folders: createMockFolders(),
142+
size: createMockSize(),
143+
testContext: createTestContext(),
144+
})
145+
146+
const writtenData = JSON.parse((writeFileSync as any).mock.calls[0][1])
147+
expect(writtenData).toMatchSnapshot()
148+
})
149+
})

0 commit comments

Comments
 (0)