-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
test(color): add unit test suite for 8-digit hex alpha channel normalization #20490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||||||||||
| import { describe, it, expect } from 'vitest'; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Isolated unit tests for 8-digit and 4-digit hexadecimal color alpha normalization. | ||||||||||||||
| */ | ||||||||||||||
|
Comment on lines
+3
to
+5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The file description claims coverage for 4-digit hexadecimal colors, but the suite contains only 8-digit and 6-digit cases. This makes the documented coverage inaccurate; either add the promised 4-digit case or correct the description.
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||
|
|
||||||||||||||
| function normalizeHexAlpha(hex: string): { hex: string; alpha: number } { | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift Exercise the production color path.
|
||||||||||||||
| if (!hex || !hex.startsWith('#')) return { hex: '#000000', alpha: 1.0 }; | ||||||||||||||
| const raw = hex.slice(1); | ||||||||||||||
| if (raw.length === 8) { | ||||||||||||||
| const baseHex = '#' + raw.slice(0, 6); | ||||||||||||||
| const alphaInt = parseInt(raw.slice(6, 8), 16); | ||||||||||||||
| return { hex: baseHex, alpha: Math.round((alphaInt / 255) * 100) / 100 }; | ||||||||||||||
| } | ||||||||||||||
| if (raw.length === 6) { | ||||||||||||||
| return { hex: '#' + raw, alpha: 1.0 }; | ||||||||||||||
| } | ||||||||||||||
| return { hex, alpha: 1.0 }; | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+7
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suite defines and tests its own Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||
|
|
||||||||||||||
| describe('Color Hex Alpha Normalization', () => { | ||||||||||||||
| it('should parse 8-digit hex codes and compute float alpha value', () => { | ||||||||||||||
| const result = normalizeHexAlpha('#ff000080'); | ||||||||||||||
| expect(result.hex).toBe('#ff0000'); | ||||||||||||||
| expect(result.alpha).toBe(0.5); | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| it('should preserve standard 6-digit hex codes with full alpha', () => { | ||||||||||||||
| const result = normalizeHexAlpha('#3b82f6'); | ||||||||||||||
| expect(result.hex).toBe('#3b82f6'); | ||||||||||||||
| expect(result.alpha).toBe(1.0); | ||||||||||||||
| }); | ||||||||||||||
| }); | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new file uses semicolons throughout, but the repository configures Prettier with
semi: false. As a result, the rootprettier --check .command rejects the file and blocks the lint workflow.