Skip to content

Commit 43da4ef

Browse files
committed
chore: reduce complexity of getConfig fn
Signed-off-by: Jakub Freisler <[email protected]>
1 parent e78ab4b commit 43da4ef

File tree

1 file changed

+39
-26
lines changed
  • packages/cypress-plugin-visual-regression-diff/src

1 file changed

+39
-26
lines changed

packages/cypress-plugin-visual-regression-diff/src/commands.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,27 @@ const constructCypressError = (log: Cypress.Log, err: Error) => {
6262
const capitalize = (text: string) =>
6363
text.charAt(0).toUpperCase() + text.slice(1);
6464

65-
const getPluginEnv = <R extends keyof Cypress.MatchImageOptions>(key: R) =>
65+
const getPluginEnv = <K extends keyof Cypress.MatchImageOptions>(key: K) =>
6666
Cypress.env(`pluginVisualRegression${capitalize(key)}`) as
67-
| Cypress.MatchImageOptions[R]
67+
| Cypress.MatchImageOptions[K]
6868
| undefined;
6969

70+
const booleanOption = <K extends keyof Cypress.MatchImageOptions, Return>(
71+
options: Cypress.MatchImageOptions,
72+
key: K,
73+
truthyValue: Return,
74+
falsyValue: Return
75+
) =>
76+
options[key] === false || getPluginEnv(key) === false
77+
? truthyValue
78+
: falsyValue;
79+
80+
const optionWithDefaults = <K extends keyof Cypress.MatchImageOptions>(
81+
options: Cypress.MatchImageOptions,
82+
key: K,
83+
defaultValue: NonNullable<Cypress.MatchImageOptions[K]>
84+
) => options[key] ?? getPluginEnv(key) ?? defaultValue;
85+
7086
const getImagesDir = (options: Cypress.MatchImageOptions) => {
7187
const imagesDir = options.imagesDir || getPluginEnv("imagesDir");
7288

@@ -80,34 +96,31 @@ const getImagesDir = (options: Cypress.MatchImageOptions) => {
8096
return imagesDir;
8197
};
8298

83-
export const getConfig = (options: Cypress.MatchImageOptions) => {
99+
const getImagesPath = (options: Cypress.MatchImageOptions) => {
84100
const imagesDir = getImagesDir(options);
85101

86-
return {
87-
scaleFactor:
88-
options.forceDeviceScaleFactor === false ||
89-
getPluginEnv("forceDeviceScaleFactor") === false
90-
? 1
91-
: 1 / window.devicePixelRatio,
92-
createMissingImages:
93-
options.createMissingImages ??
94-
getPluginEnv("createMissingImages") ??
95-
true,
96-
updateImages: options.updateImages ?? getPluginEnv("updateImages") ?? false,
97-
imagesPath:
98-
(imagesDir && `{spec_path}/${imagesDir}`) ||
99-
options.imagesPath ||
100-
getPluginEnv("imagesPath") ||
101-
"{spec_path}/__image_snapshots__",
102-
maxDiffThreshold:
103-
options.maxDiffThreshold ?? getPluginEnv("maxDiffThreshold") ?? 0.01,
104-
diffConfig: options.diffConfig || getPluginEnv("diffConfig") || {},
105-
screenshotConfig:
106-
options.screenshotConfig || getPluginEnv("screenshotConfig") || {},
107-
matchAgainstPath: options.matchAgainstPath || undefined,
108-
};
102+
return (
103+
(imagesDir && `{spec_path}/${imagesDir}`) ||
104+
optionWithDefaults(options, "imagesPath", "{spec_path}/__image_snapshots__")
105+
);
109106
};
110107

108+
export const getConfig = (options: Cypress.MatchImageOptions) => ({
109+
scaleFactor: booleanOption(
110+
options,
111+
"forceDeviceScaleFactor",
112+
1,
113+
1 / window.devicePixelRatio
114+
),
115+
createMissingImages: optionWithDefaults(options, "createMissingImages", true),
116+
updateImages: optionWithDefaults(options, "updateImages", false),
117+
imagesPath: getImagesPath(options),
118+
maxDiffThreshold: optionWithDefaults(options, "maxDiffThreshold", 0.01),
119+
diffConfig: optionWithDefaults(options, "diffConfig", {}),
120+
screenshotConfig: optionWithDefaults(options, "screenshotConfig", {}),
121+
matchAgainstPath: options.matchAgainstPath || undefined,
122+
});
123+
111124
Cypress.Commands.add(
112125
"matchImage",
113126
{ prevSubject: "optional" },

0 commit comments

Comments
 (0)