Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbmatter committed May 15, 2024
1 parent 2b0bba9 commit 5ef29ed
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions public/editor/utils.doesStrLookLikeColor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ describe("doesStrLookLikeColor function tests", () => {
expect(doesStrLookLikeColor("FFFFFF")).toBe(false);
});

test("returns false for invalid color with more than 6 digits", () => {
test("returns false for invalid color with weird number of digits", () => {
expect(doesStrLookLikeColor("#FFFFFFF")).toBe(false);
});

test("returns false for invalid color with less than 3 digits", () => {
expect(doesStrLookLikeColor("#FF")).toBe(false);
expect(doesStrLookLikeColor("#FFFF")).toBe(false);
});

test("returns false for invalid characters in color", () => {
expect(doesStrLookLikeColor("#ZZZ999")).toBe(false);
});

test("works for rgba colors", () => {
expect(doesStrLookLikeColor("rgba(20,0,100,0.5)")).toBe(true);
expect(doesStrLookLikeColor("rgba(20, 0, 100, 0.5)")).toBe(true);
expect(doesStrLookLikeColor("rgba(20,0,100,0.5")).toBe(false);
});
});
2 changes: 1 addition & 1 deletion public/editor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const luma = (colorHex: string): number => {

export const doesStrLookLikeColor = (str: string): boolean => {
const regex =
/^(#([0-9A-F]{3,4}){1,2}$|rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})(,\s*((0(?:\.\d+)?|1(?:\.0)?)))?\))$/i;
/^#[0-9A-F]{3}$|^#[0-9A-F]{6}$|^rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})(,\s*((0(?:\.\d+)?|1(?:\.0)?)))?\)$/i;
return regex.test(str);
};

Expand Down

0 comments on commit 5ef29ed

Please sign in to comment.