Skip to content

Commit 58b3efb

Browse files
committed
chore: refactor to use buildFolderOptions
- adjust methods - add and adjust UTs
1 parent 562f6c9 commit 58b3efb

10 files changed

Lines changed: 169 additions & 69 deletions

File tree

packages/image-comparison-core/src/commands/checkFullPageScreen.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,17 @@ vi.mock('../helpers/utils.js', () => ({
3232
autoSaveBaseline: false,
3333
savePerInstance: false,
3434
isHybridApp: false,
35-
})
35+
}),
36+
buildFolderOptions: vi.fn().mockReturnValue({
37+
autoSaveBaseline: false,
38+
actualFolder: '/mock/actual',
39+
baselineFolder: '/mock/baseline',
40+
diffFolder: '/mock/diff',
41+
browserName: 'chrome',
42+
deviceName: 'Desktop',
43+
isMobile: false,
44+
savePerInstance: false,
45+
}),
3646
}))
3747

3848
vi.mock('../methods/images.js', () => ({

packages/image-comparison-core/src/commands/checkFullPageScreen.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import saveFullPageScreen from './saveFullPageScreen.js'
33
import type { ImageCompareResult } from '../methods/images.interfaces.js'
44
import type { SaveFullPageOptions } from './fullPage.interfaces.js'
55
import { methodCompareOptions } from '../helpers/options.js'
6-
import { extractCommonCheckVariables } from '../helpers/utils.js'
6+
import { extractCommonCheckVariables, buildFolderOptions } from '../helpers/utils.js'
77
import type { InternalCheckFullPageMethodOptions } from './check.interfaces.js'
88

99
/**
@@ -21,22 +21,15 @@ export default async function checkFullPageScreen(
2121
}: InternalCheckFullPageMethodOptions
2222
): Promise<ImageCompareResult | number> {
2323
// 1. Extract common variables
24+
const commonCheckVariables = extractCommonCheckVariables({ folders, instanceData, wicOptions: checkFullPageOptions.wic })
2425
const {
25-
browserName,
26-
deviceName,
2726
deviceRectangles,
2827
isAndroid,
2928
isIOS,
30-
isMobile,
3129
isAndroidNativeWebScreenshot,
3230
platformName,
33-
autoSaveBaseline,
34-
isHybridApp,
35-
savePerInstance,
36-
actualFolder,
37-
baselineFolder,
38-
diffFolder
39-
} = extractCommonCheckVariables({ folders, instanceData, wicOptions: checkFullPageOptions.wic })
31+
isHybridApp
32+
} = commonCheckVariables
4033
const {
4134
disableBlinkingCursor,
4235
disableCSSAnimation,
@@ -90,16 +83,7 @@ export default async function checkFullPageScreen(
9083
devicePixelRatio,
9184
deviceRectangles,
9285
fileName,
93-
folderOptions: {
94-
autoSaveBaseline,
95-
actualFolder,
96-
baselineFolder,
97-
diffFolder,
98-
browserName,
99-
deviceName,
100-
isMobile,
101-
savePerInstance,
102-
},
86+
folderOptions: buildFolderOptions({ commonCheckVariables }),
10387
isAndroid,
10488
isAndroidNativeWebScreenshot,
10589
isIOS,

packages/image-comparison-core/src/commands/checkWebElement.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ vi.mock('../helpers/utils.js', () => ({
4545
platformName: 'Windows',
4646
autoSaveBaseline: false,
4747
savePerInstance: false,
48-
})
48+
}),
49+
buildFolderOptions: vi.fn().mockReturnValue({
50+
autoSaveBaseline: false,
51+
actualFolder: '/mock/actual',
52+
baselineFolder: '/mock/baseline',
53+
diffFolder: '/mock/diff',
54+
browserName: 'chrome',
55+
deviceName: 'Desktop',
56+
isMobile: false,
57+
savePerInstance: false,
58+
}),
4959
}))
5060

5161
describe('checkWebElement', () => {

packages/image-comparison-core/src/commands/checkWebElement.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import saveWebElement from './saveWebElement.js'
33
import type { ImageCompareResult } from '../methods/images.interfaces.js'
44
import type { SaveElementOptions } from './element.interfaces.js'
55
import { methodCompareOptions } from '../helpers/options.js'
6-
import { extractCommonCheckVariables } from '../helpers/utils.js'
6+
import { extractCommonCheckVariables, buildFolderOptions } from '../helpers/utils.js'
77
import type { InternalCheckElementMethodOptions } from './check.interfaces.js'
88

99
/**
@@ -22,20 +22,13 @@ export default async function checkWebElement(
2222
}: InternalCheckElementMethodOptions
2323
): Promise<ImageCompareResult | number> {
2424
// 1. Extract common variables
25+
const commonCheckVariables = extractCommonCheckVariables({ folders, instanceData, wicOptions: checkElementOptions.wic })
2526
const {
26-
browserName,
27-
deviceName,
2827
deviceRectangles,
2928
isAndroid,
30-
isMobile,
3129
isAndroidNativeWebScreenshot,
32-
platformName,
33-
autoSaveBaseline,
34-
savePerInstance,
35-
actualFolder,
36-
baselineFolder,
37-
diffFolder
38-
} = extractCommonCheckVariables({ folders, instanceData, wicOptions: checkElementOptions.wic })
30+
platformName
31+
} = commonCheckVariables
3932
const {
4033
disableBlinkingCursor,
4134
disableCSSAnimation,
@@ -88,16 +81,7 @@ export default async function checkWebElement(
8881
devicePixelRatio,
8982
deviceRectangles,
9083
fileName,
91-
folderOptions: {
92-
autoSaveBaseline,
93-
actualFolder,
94-
baselineFolder,
95-
diffFolder,
96-
browserName,
97-
deviceName,
98-
isMobile,
99-
savePerInstance,
100-
},
84+
folderOptions: buildFolderOptions({ commonCheckVariables }),
10185
isAndroid,
10286
isAndroidNativeWebScreenshot,
10387
platformName,

packages/image-comparison-core/src/commands/checkWebScreen.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,17 @@ vi.mock('../helpers/utils.js', () => ({
4444
isAndroidNativeWebScreenshot: false,
4545
autoSaveBaseline: false,
4646
savePerInstance: false,
47-
})
47+
}),
48+
buildFolderOptions: vi.fn().mockReturnValue({
49+
autoSaveBaseline: false,
50+
actualFolder: '/mock/actual',
51+
baselineFolder: '/mock/baseline',
52+
diffFolder: '/mock/diff',
53+
browserName: 'chrome',
54+
deviceName: 'Desktop',
55+
isMobile: false,
56+
savePerInstance: false,
57+
}),
4858
}))
4959

5060
describe('checkWebScreen', () => {

packages/image-comparison-core/src/commands/checkWebScreen.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { executeImageCompare } from '../methods/images.js'
33
import type { ImageCompareOptions, ImageCompareResult } from '../methods/images.interfaces.js'
44
import type { SaveScreenOptions } from './screen.interfaces.js'
55
import { screenMethodCompareOptions } from '../helpers/options.js'
6-
import { extractCommonCheckVariables } from '../helpers/utils.js'
6+
import { extractCommonCheckVariables, buildFolderOptions } from '../helpers/utils.js'
77
import type { InternalCheckScreenMethodOptions } from './check.interfaces.js'
88

99
/**
@@ -21,19 +21,12 @@ export default async function checkWebScreen(
2121
}: InternalCheckScreenMethodOptions
2222
): Promise<ImageCompareResult | number> {
2323
// 1. Extract common variables
24+
const commonCheckVariables = extractCommonCheckVariables({ folders, instanceData, wicOptions: checkScreenOptions.wic })
2425
const {
25-
browserName,
26-
deviceName,
2726
deviceRectangles,
2827
isAndroid,
29-
isMobile,
30-
isAndroidNativeWebScreenshot,
31-
autoSaveBaseline,
32-
savePerInstance,
33-
actualFolder,
34-
baselineFolder,
35-
diffFolder
36-
} = extractCommonCheckVariables({ folders, instanceData, wicOptions: checkScreenOptions.wic })
28+
isAndroidNativeWebScreenshot
29+
} = commonCheckVariables
3730
const {
3831
disableBlinkingCursor,
3932
disableCSSAnimation,
@@ -78,16 +71,7 @@ export default async function checkWebScreen(
7871
devicePixelRatio,
7972
deviceRectangles,
8073
fileName,
81-
folderOptions: {
82-
autoSaveBaseline,
83-
actualFolder,
84-
baselineFolder,
85-
diffFolder,
86-
browserName,
87-
deviceName,
88-
isMobile,
89-
savePerInstance,
90-
},
74+
folderOptions: buildFolderOptions({ commonCheckVariables }),
9175
isAndroid,
9276
isAndroidNativeWebScreenshot,
9377
}

packages/image-comparison-core/src/helpers/__snapshots__/utils.test.ts.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`utils > buildFolderOptions > should build folder options from common check variables 1`] = `
4+
{
5+
"actualFolder": "/path/to/actual",
6+
"autoSaveBaseline": true,
7+
"baselineFolder": "/path/to/baseline",
8+
"browserName": "chrome",
9+
"deviceName": "iPhone 12",
10+
"diffFolder": "/path/to/diff",
11+
"isMobile": true,
12+
"savePerInstance": false,
13+
}
14+
`;
15+
16+
exports[`utils > buildFolderOptions > should handle all properties correctly 1`] = `
17+
{
18+
"actualFolder": "/test/actual",
19+
"autoSaveBaseline": false,
20+
"baselineFolder": "/test/baseline",
21+
"browserName": "firefox",
22+
"deviceName": "Desktop",
23+
"diffFolder": "/test/diff",
24+
"isMobile": false,
25+
"savePerInstance": true,
26+
}
27+
`;
28+
329
exports[`utils > calculateDprData > should multiply all number values by the dpr value 1`] = `
430
{
531
"1": 6,

packages/image-comparison-core/src/helpers/utils.interfaces.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,34 @@ export interface CommonCheckVariables {
181181
}
182182

183183
export interface ExtractCommonCheckVariablesOptions {
184-
/** The folder paths for storing screenshots */
184+
/** The folders object */
185185
folders: Folders;
186-
/** Instance data containing device and browser information */
186+
/** The instance data object */
187187
instanceData: any;
188-
/** WIC options containing settings and compare options */
188+
/** The wic options object */
189189
wicOptions: any;
190190
}
191+
192+
export interface FolderOptions {
193+
/** Whether to auto-save baseline images */
194+
autoSaveBaseline: boolean;
195+
/** The actual folder path */
196+
actualFolder: string;
197+
/** The baseline folder path */
198+
baselineFolder: string;
199+
/** The diff folder path */
200+
diffFolder: string;
201+
/** The browser name */
202+
browserName: string;
203+
/** The device name */
204+
deviceName: string;
205+
/** Whether this is a mobile device */
206+
isMobile: boolean;
207+
/** Whether to save per instance */
208+
savePerInstance: boolean;
209+
}
210+
211+
export interface BuildFolderOptionsOptions {
212+
/** Common check variables that include all the needed folder options properties */
213+
commonCheckVariables: CommonCheckVariables;
214+
}

packages/image-comparison-core/src/helpers/utils.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ vi.mock('node:fs', async () => {
1212
})
1313
import logger from '@wdio/logger'
1414
import {
15+
buildFolderOptions,
1516
calculateDprData,
1617
canUseBidiScreenshot,
1718
checkAndroidChromeDriverScreenshot,
@@ -925,4 +926,49 @@ describe('utils', () => {
925926
expect(result.deviceName).toBe('Pixel 4')
926927
})
927928
})
929+
930+
describe('buildFolderOptions', () => {
931+
it('should build folder options from common check variables', () => {
932+
const commonCheckVariables = {
933+
actualFolder: '/path/to/actual',
934+
baselineFolder: '/path/to/baseline',
935+
diffFolder: '/path/to/diff',
936+
browserName: 'chrome',
937+
deviceName: 'iPhone 12',
938+
deviceRectangles: { screenSize: { width: 390, height: 844 } },
939+
isAndroid: false,
940+
isMobile: true,
941+
isAndroidNativeWebScreenshot: true,
942+
autoSaveBaseline: true,
943+
savePerInstance: false,
944+
}
945+
946+
const result = buildFolderOptions({ commonCheckVariables })
947+
948+
expect(result).toMatchSnapshot()
949+
})
950+
951+
it('should handle all properties correctly', () => {
952+
const commonCheckVariables = {
953+
actualFolder: '/test/actual',
954+
baselineFolder: '/test/baseline',
955+
diffFolder: '/test/diff',
956+
browserName: 'firefox',
957+
deviceName: 'Desktop',
958+
deviceRectangles: { screenSize: { width: 1920, height: 1080 } },
959+
isAndroid: true,
960+
isMobile: false,
961+
isAndroidNativeWebScreenshot: false,
962+
autoSaveBaseline: false,
963+
savePerInstance: true,
964+
platformName: 'Android',
965+
isIOS: false,
966+
isHybridApp: true,
967+
}
968+
969+
const result = buildFolderOptions({ commonCheckVariables })
970+
971+
expect(result).toMatchSnapshot()
972+
})
973+
})
928974
})

packages/image-comparison-core/src/helpers/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { join } from 'node:path'
33
import { DESKTOP, NOT_KNOWN } from './constants.js'
44
import { mkdirSync } from 'node:fs'
55
import type {
6+
BuildFolderOptionsOptions,
67
CommonCheckVariables,
78
ExecuteNativeClickOptions,
89
ExtractCommonCheckVariablesOptions,
10+
FolderOptions,
911
FormatFileDefaults,
1012
FormatFileNameOptions,
1113
GetAddressBarShadowPaddingOptions,
@@ -613,3 +615,23 @@ export function extractCommonCheckVariables(
613615
...(wicOptions.isHybridApp !== undefined && { isHybridApp: wicOptions.isHybridApp }),
614616
}
615617
}
618+
619+
/**
620+
* Builds folder options object used across all check methods to reduce duplication
621+
*/
622+
export function buildFolderOptions(
623+
options: BuildFolderOptionsOptions
624+
): FolderOptions {
625+
const { commonCheckVariables } = options
626+
627+
return {
628+
autoSaveBaseline: commonCheckVariables.autoSaveBaseline,
629+
actualFolder: commonCheckVariables.actualFolder,
630+
baselineFolder: commonCheckVariables.baselineFolder,
631+
diffFolder: commonCheckVariables.diffFolder,
632+
browserName: commonCheckVariables.browserName,
633+
deviceName: commonCheckVariables.deviceName,
634+
isMobile: commonCheckVariables.isMobile,
635+
savePerInstance: commonCheckVariables.savePerInstance,
636+
}
637+
}

0 commit comments

Comments
 (0)