🧪 Add tests for useCardSettings hook#468
Conversation
Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
|
Warning Review limit reached
Next review available in: 36 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| (cardLayoutLib.toggleBlockVisibility as any).mockImplementation( | ||
| (prev: any, id: any) => ({ ...prev, toggled: id }) | ||
| ); |
There was a problem hiding this comment.
このモックは実際の toggleBlockVisibility と違い、blocks[].visible を更新せずにトップレベルの toggled だけを追加します。そのため toggleMainBlockVisibility("stats") のテストは、実際の CardLayout 更新が壊れても通ってしまい、hook とレイアウト更新処理の連携回帰を検出できません。
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/hooks/__tests__/useCardSettings.test.ts
Line: 63-65
Comment:
**Mocked Layout Shape Diverges**
このモックは実際の `toggleBlockVisibility` と違い、`blocks[].visible` を更新せずにトップレベルの `toggled` だけを追加します。そのため `toggleMainBlockVisibility("stats")` のテストは、実際の `CardLayout` 更新が壊れても通ってしまい、hook とレイアウト更新処理の連携回帰を検出できません。
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive test suite for the useCardSettings hook. The review feedback focuses on improving test robustness and type safety. Key recommendations include removing unnecessary mocks of pure utility functions to assert on actual state changes, avoiding unsafe as any type assertions by using proper TypeScript types and Vitest's vi.mocked() utility, and correcting structural mismatches in the mock layout data.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { useCardSettings } from "../useCardSettings"; | ||
| import * as cardSettingsLib from "@/lib/cardSettings"; | ||
| import * as cardLayoutLib from "@/lib/cardLayout"; |
| import * as cardSettingsLib from "@/lib/cardSettings"; | ||
| import * as cardLayoutLib from "@/lib/cardLayout"; | ||
| import { DEFAULT_CARD_LAYOUT } from "@/lib/types"; | ||
| import type { CardLayout } from "@/lib/types"; |
There was a problem hiding this comment.
Import CardBlockId from @/lib/types so we can use it for type casting in our tests instead of as any.
| import type { CardLayout } from "@/lib/types"; | |
| import type { CardLayout, CardBlockId } from "@/lib/types"; |
References
- Maintain explicit return types for functions in TypeScript to ensure type safety and API clarity. If a type is only used for annotations, use 'import type' instead of removing the annotation to satisfy linting rules.
| vi.mock("@/lib/cardLayout", async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import("@/lib/cardLayout")>(); | ||
| return { | ||
| ...actual, | ||
| toggleBlockVisibility: vi.fn(), | ||
| }; | ||
| }); |
There was a problem hiding this comment.
Mocking pure utility functions like toggleBlockVisibility from @/lib/cardLayout is an anti-pattern that introduces unnecessary complexity and type-safety workarounds (such as as any casting). Since toggleBlockVisibility is a pure function, we can use its actual implementation directly in the tests.
Let's remove this mock entirely.
| const mockLoadedSettings = { | ||
| layout: { | ||
| ...DEFAULT_CARD_LAYOUT, | ||
| blocks: [ | ||
| { id: "profile", visible: true, order: 0 }, | ||
| { id: "stats", visible: false, order: 1 }, | ||
| ] | ||
| } as CardLayout, |
There was a problem hiding this comment.
The mock layout uses an incorrect property order instead of column, which is the actual property defined in the CardBlock type. Using the type assertion as CardLayout masks this type mismatch.
Let's update the mock layout to adhere to the actual CardBlock type structure by using column and removing the unsafe type assertion.
const mockLoadedSettings = {
layout: {
...DEFAULT_CARD_LAYOUT,
blocks: [
{ id: "profile", visible: true, column: "full" },
{ id: "stats", visible: false, column: "left" },
]
},|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| (cardSettingsLib.loadCardSettings as any).mockReturnValue(mockLoadedSettings); |
There was a problem hiding this comment.
Avoid using as any type assertions when mocking functions. Instead, use Vitest's idiomatic vi.mocked() utility to maintain type safety.
| (cardSettingsLib.loadCardSettings as any).mockReturnValue(mockLoadedSettings); | |
| vi.mocked(cardSettingsLib.loadCardSettings).mockReturnValue(mockLoadedSettings); |
| (cardLayoutLib.toggleBlockVisibility as any).mockImplementation( | ||
| (prev: any, id: any) => ({ ...prev, toggled: id }) | ||
| ); |
| it("toggleMainBlockVisibility should update layout using toggleBlockVisibility", () => { | ||
| const { result } = renderHook(() => useCardSettings(true)); | ||
|
|
||
| act(() => { | ||
| result.current.toggleMainBlockVisibility("stats"); | ||
| }); | ||
|
|
||
| expect(cardLayoutLib.toggleBlockVisibility).toHaveBeenCalledWith( | ||
| mockLoadedSettings.layout, | ||
| "stats" | ||
| ); | ||
| expect(result.current.layout).toHaveProperty("toggled", "stats"); | ||
| }); |
There was a problem hiding this comment.
With the mock of toggleBlockVisibility removed, we can assert on the actual state change of the layout blocks. This makes the test much more robust and type-safe.
it("toggleMainBlockVisibility should update layout using toggleBlockVisibility", () => {
const { result } = renderHook(() => useCardSettings(true));
act(() => {
result.current.toggleMainBlockVisibility("stats");
});
const statsBlock = result.current.layout.blocks.find((b) => b.id === "stats");
expect(statsBlock?.visible).toBe(true);
});|
|
||
| expect(result.current.isBlockVisible("profile")).toBe(true); | ||
| expect(result.current.isBlockVisible("stats")).toBe(false); | ||
| expect(result.current.isBlockVisible("non-existent-block" as any)).toBe(false); |
There was a problem hiding this comment.
Instead of using as any to pass an invalid block ID, use as CardBlockId to maintain better type safety while testing the fallback behavior.
| expect(result.current.isBlockVisible("non-existent-block" as any)).toBe(false); | |
| expect(result.current.isBlockVisible("non-existent-block" as CardBlockId)).toBe(false); |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com>
|
Closing because this Jules-managed branch cannot maintain a stable review-loop head: after the target branch is incorporated or a review fix is pushed, the bot appends the original generated change again. The current head is therefore not reliably the commit that passed target-fresh CI and review. |
🎯 What: The testing gap addressed is the lack of unit tests for the
useCardSettingshook, which is responsible for state and localStorage synchronization of card configurations.📊 Coverage: Scenarios covered include hook initialization, delayed hydration behavior (avoiding hydration when not mounted), update logic triggered upon mounting, saving configurations to storage, layout structure updates via
toggleMainBlockVisibility, layout options updates viatoggleDisplayOption, and checking block visibility.✨ Result: Test coverage for this custom hook has been introduced, significantly improving codebase reliability and confirming expected syncing behaviors with localStorage.
PR created automatically by Jules for task 11644537351646032768 started by @is0692vs
Greptile Summary
このPRは
useCardSettingshook のテストを追加します。主な変更は以下です。Confidence Score: 5/5
このPRは小さなテスト改善後に安全にマージできそうです。
src/hooks/tests/useCardSettings.test.ts
Important Files Changed
useCardSettingsの主要フローをカバーする新規テストで、レイアウト切り替えのモックだけ実装形状とずれています。Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "test: add tests for useCardSettings hook" | Re-trigger Greptile