Skip to content

Commit bb82e3a

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

File tree

1 file changed

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

1 file changed

+42
-26
lines changed

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

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,30 @@ 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 = <
81+
K extends keyof Cypress.MatchImageOptions,
82+
Return extends Cypress.MatchImageOptions[K]
83+
>(
84+
options: Cypress.MatchImageOptions,
85+
key: K,
86+
defaultValue: Return
87+
) => options[key] ?? getPluginEnv(key) ?? defaultValue;
88+
7089
const getImagesDir = (options: Cypress.MatchImageOptions) => {
7190
const imagesDir = options.imagesDir || getPluginEnv("imagesDir");
7291

@@ -80,34 +99,31 @@ const getImagesDir = (options: Cypress.MatchImageOptions) => {
8099
return imagesDir;
81100
};
82101

83-
export const getConfig = (options: Cypress.MatchImageOptions) => {
102+
const getImagesPath = (options: Cypress.MatchImageOptions) => {
84103
const imagesDir = getImagesDir(options);
85104

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-
};
105+
return (
106+
(imagesDir && `{spec_path}/${imagesDir}`) ||
107+
optionWithDefaults(options, "imagesPath", "{spec_path}/__image_snapshots__")
108+
);
109109
};
110110

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

0 commit comments

Comments
 (0)