Skip to content

Commit bfa8274

Browse files
committed
test - add more color helper for tests
1 parent c978086 commit bfa8274

File tree

1 file changed

+31
-4
lines changed
  • tests/integration/playwright/src

1 file changed

+31
-4
lines changed

tests/integration/playwright/src/utils.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,37 @@ export type RGBColor = {
128128
alpha?: number;
129129
};
130130

131-
export async function checkColor(element, cssProperty, rgbColors: RGBColor) {
132-
const colorString = rgbColors.alpha !== undefined
133-
? `rgba(${rgbColors.red}, ${rgbColors.green}, ${rgbColors.blue}, ${rgbColors.alpha})`
134-
: `rgb(${rgbColors.red}, ${rgbColors.green}, ${rgbColors.blue})`;
131+
export type HexColor = string;
132+
133+
export function isRGBColor(color: any): color is RGBColor {
134+
return (
135+
typeof color === 'object' &&
136+
'red' in color &&
137+
'green' in color &&
138+
'blue' in color
139+
);
140+
}
141+
142+
export function isHexColor(color: any): color is HexColor {
143+
return (
144+
typeof color === 'string' &&
145+
/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(color)
146+
);
147+
}
148+
149+
export async function checkColor(element, cssProperty, color: RGBColor | HexColor) {
150+
let colorString: string;
151+
// Check if the color is an RGBColor object or a string
152+
if (isRGBColor(color)) {
153+
colorString = color.alpha !== undefined
154+
? `rgba(${color.red}, ${color.green}, ${color.blue}, ${color.alpha})`
155+
: `rgb(${color.red}, ${color.green}, ${color.blue})`;
156+
} else if (isHexColor(color)) {
157+
colorString = color as string;
158+
} else {
159+
throw new Error('Invalid color format. Use either RGBColor or HexColor.');
160+
}
161+
// Check the CSS property
135162
await expect(element).toHaveCSS(cssProperty, colorString);
136163
}
137164

0 commit comments

Comments
 (0)