test: add ColorPicker tests and Storybook story#23
test: add ColorPicker tests and Storybook story#23Mateus Veloso (mateusveloso) merged 2 commits intomainfrom
Conversation
- 10 unit tests covering rendering, popover interaction, hex input validation, confirm/cancel behavior, and props forwarding - Storybook stories: Default, WithDifferentColors, InForm Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
WalkthroughI pity the fool! Added Storybook stories and a test suite for ColorPicker: three interactive stories (Default, WithDifferentColors, InForm) and comprehensive unit tests covering rendering, popover behavior, hex input validation, confirmation, Escape dismissal, and className forwarding. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/components/ui/ColorPicker/ColorPicker.test.tsx (1)
92-104: Tighten the invalid-hex assertion so it proves the exact fallback state.Right now, Line 103 only proves the value is not
#ZZZZZZ. Assert the expected value explicitly (Line 102 comment says it should remain#), so this test catches regressions harder.Suggested test hardening
- // Should not accept invalid hex — input stays at # after clear - expect(hexInput).not.toHaveValue("#ZZZZZZ"); + // Should not accept invalid hex — input stays at # after clear + expect(hexInput).toHaveValue("#");🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/components/ui/ColorPicker/ColorPicker.test.tsx` around lines 92 - 104, The test in ColorPicker.test.tsx currently only asserts the hex input is not "#ZZZZZZ" after typing invalid characters; update the assertion to assert the exact fallback value (the input should equal "#" per the comment) so the test verifies the precise behavior of the ColorPicker component. Locate the test case named "rejects invalid hex characters", target the hex input obtained via screen.getByDisplayValue("#7C3AED") and replace the negative assertion with an explicit equality assertion that the input value is "#" to harden the test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/components/ui/ColorPicker/ColorPicker.stories.tsx`:
- Around line 1-3: Reorder the three imports in ColorPicker.stories.tsx to
satisfy Biome's import order: place external module imports first (import {
useState } from "react"), then third-party type imports (import type { Meta,
StoryObj } from "@storybook/react-vite"), and finally local imports (import {
ColorPicker } from "./ColorPicker"); update the import lines in that file
accordingly so CI lint passes.
---
Nitpick comments:
In `@src/components/ui/ColorPicker/ColorPicker.test.tsx`:
- Around line 92-104: The test in ColorPicker.test.tsx currently only asserts
the hex input is not "#ZZZZZZ" after typing invalid characters; update the
assertion to assert the exact fallback value (the input should equal "#" per the
comment) so the test verifies the precise behavior of the ColorPicker component.
Locate the test case named "rejects invalid hex characters", target the hex
input obtained via screen.getByDisplayValue("#7C3AED") and replace the negative
assertion with an explicit equality assertion that the input value is "#" to
harden the test.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2e8e9fea-602d-45cb-9ce4-a90469dcfd93
📒 Files selected for processing (2)
src/components/ui/ColorPicker/ColorPicker.stories.tsxsrc/components/ui/ColorPicker/ColorPicker.test.tsx
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/components/ui/ColorPicker/ColorPicker.stories.tsx (1)
45-50: I pity the fool declaring a component inside.map().Line 46 creates
Pickeron every render, which can remount children and reset internal state unexpectedly. Extract it to a stable component.Refactor to a stable child component
+const ColorPickerItem = ({ initialColor }: { initialColor: string }) => { + const [color, setColor] = useState(initialColor); + return <ColorPicker onChange={setColor} value={color} />; +}; export const WithDifferentColors: Story = { render: () => { const colors = ["#EF4444", "#F59E0B", "#10B981", "#3B82F6", "#8B5CF6"]; return ( <div className="flex items-center gap-3"> {colors.map((initialColor) => { - const Picker = () => { - const [color, setColor] = useState(initialColor); - return <ColorPicker onChange={setColor} value={color} />; - }; - return <Picker key={initialColor} />; + return <ColorPickerItem initialColor={initialColor} key={initialColor} />; })} </div> ); }, };🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/components/ui/ColorPicker/ColorPicker.stories.tsx` around lines 45 - 50, The inline Picker component created inside colors.map causes a new component definition on every render and can reset state; extract it to a stable top-level component (e.g., ColorPickerExample) that accepts props (initialColor: string) and internally uses useState to manage color, then replace the inline Picker in the map with <ColorPickerExample key={initialColor} initialColor={initialColor} /> so ColorPicker (and its state) is stable across renders.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/components/ui/ColorPicker/ColorPicker.stories.tsx`:
- Around line 13-21: The story defines Storybook controls (argTypes
value/onChange) but the rendered story uses local state via setColor, so
Controls/Actions are out of sync; change the story to wire the component to
Storybook args instead of local state by using args.value for the ColorPicker
value and calling the Storybook-provided onChange (or use useArgs/updateArgs)
instead of setColor; update the story render where onChange={setColor} and value
is read from local state so it uses onChange={args.onChange} (or updateArgs to
set args.value) and value={args.value} so the Controls and Actions panels
reflect the real story state.
---
Nitpick comments:
In `@src/components/ui/ColorPicker/ColorPicker.stories.tsx`:
- Around line 45-50: The inline Picker component created inside colors.map
causes a new component definition on every render and can reset state; extract
it to a stable top-level component (e.g., ColorPickerExample) that accepts props
(initialColor: string) and internally uses useState to manage color, then
replace the inline Picker in the map with <ColorPickerExample key={initialColor}
initialColor={initialColor} /> so ColorPicker (and its state) is stable across
renders.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: bc896752-6a87-47df-bbe5-6e7bfdbedf68
📒 Files selected for processing (1)
src/components/ui/ColorPicker/ColorPicker.stories.tsx
Summary
Follow-up do PR #22 — adiciona testes e story do Storybook para o ColorPicker.
Testes (10 casos):
Storybook:
Test plan
Summary by CodeRabbit
Tests
Documentation