Skip to content

test: add ColorPicker tests and Storybook story#23

Merged
Mateus Veloso (mateusveloso) merged 2 commits intomainfrom
test/LAI-5067-colorpicker-tests
Mar 30, 2026
Merged

test: add ColorPicker tests and Storybook story#23
Mateus Veloso (mateusveloso) merged 2 commits intomainfrom
test/LAI-5067-colorpicker-tests

Conversation

@mateusveloso
Copy link
Copy Markdown
Contributor

@mateusveloso Mateus Veloso (mateusveloso) commented Mar 30, 2026

Summary

Follow-up do PR #22 — adiciona testes e story do Storybook para o ColorPicker.

Testes (10 casos):

  • Renderização do trigger com cor correta
  • Popover abre/fecha corretamente
  • Input hex aceita valores válidos e rejeita inválidos
  • Confirmar chama onChange com a cor selecionada
  • Fechar sem confirmar não chama onChange
  • className é propagado pro trigger

Storybook:

  • Default — picker com valor e label
  • WithDifferentColors — 5 pickers com cores diferentes
  • InForm — uso em formulário de etiqueta

Test plan

  • 10 testes passando localmente
  • Verificar stories no Storybook

Summary by CodeRabbit

  • Tests

    • Added a comprehensive test suite for the ColorPicker covering initial render, popover open/close behavior, hex input validation, confirmation flow, and prop forwarding to the trigger.
  • Documentation

    • Added Storybook stories demonstrating ColorPicker in three scenarios: default controlled usage, multiple instances with preset colors, and integration in a form with labeled input and live color display.

- 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>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

Walkthrough

I 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

Cohort / File(s) Summary
Storybook Documentation
src/components/ui/ColorPicker/ColorPicker.stories.tsx
New Storybook module exporting meta and three stories (Default, WithDifferentColors, InForm) with controlled example usage, argTypes for value and onChange, centered layout, and jest/autodocs metadata.
Unit Tests
src/components/ui/ColorPicker/ColorPicker.test.tsx
New test suite that mocks react-colorful, verifies initial render and trigger styling, popover open/close behavior, hex input valid/invalid handling, confirmation behavior calling onChange, Escape key dismissal not calling onChange, and className prop forwarding.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I pity the fool who skips this commit,
Stories and tests make the colors legit,
Popovers, hexes, and a class that sticks,
Click, confirm, escape—no nasty tricks,
Ship it bright, now that's the gist! 🎨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description provides detailed information about tests and Storybook stories added, but does not follow the repository's required template structure with 'What changed?', 'Screenshots', and 'Checklist' sections. Restructure the description to match the template: add 'What changed?' section, include 'Screenshots' section if applicable, and use the standard checklist format with proper markdown checkboxes.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main changes: adding tests and Storybook stories for ColorPicker, which aligns perfectly with the pull request objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/LAI-5067-colorpicker-tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between a1065d8 and e2dcf1f.

📒 Files selected for processing (2)
  • src/components/ui/ColorPicker/ColorPicker.stories.tsx
  • src/components/ui/ColorPicker/ColorPicker.test.tsx

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Picker on 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

📥 Commits

Reviewing files that changed from the base of the PR and between e2dcf1f and e656796.

📒 Files selected for processing (1)
  • src/components/ui/ColorPicker/ColorPicker.stories.tsx

@mateusveloso Mateus Veloso (mateusveloso) merged commit 546db10 into main Mar 30, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants