fix: resolve act warnings and prevent runtime crashes on empty data#1319
fix: resolve act warnings and prevent runtime crashes on empty data#1319MayankSharma-ops wants to merge 1 commit into
Conversation
|
|
WalkthroughThis PR normalizes image data handling across the frontend by updating mock responses to a consistent shape, adding defensive null-coalescing fallbacks in page components, and migrating tests to properly handle async effects using React Testing Library's ChangesImage Data Normalization and Test Async Migration
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
|
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
frontend/src/pages/Home/Home.tsx (1)
43-43: ⚡ Quick winConsider extracting the repeated image data normalization pattern.
All three image-displaying components use the same defensive fallback pattern
(data?.data ?? []) as Image[]when preparing fetched images for Redux dispatch. This duplication could be reduced with a shared helper function.
frontend/src/pages/Home/Home.tsx#L43: Extract to a sharednormalizeImagesResponsehelper in@/utilsor@/api/api-functions.frontend/src/pages/AITagging/AITagging.tsx#L38: Replace with the shared helper.frontend/src/pages/Home/MyFav.tsx#L45: Replace with the shared helper.♻️ Example shared helper
// In `@/api/api-functions.ts` or `@/utils/imageHelpers.ts` export function normalizeImagesResponse(response?: { data?: Image[] }): Image[] { return (response?.data ?? []) as Image[]; }Then in each component:
-const images = (data?.data ?? []) as Image[]; +const images = normalizeImagesResponse(data);🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@frontend/src/pages/Home/Home.tsx` at line 43, Extract the repeated normalization logic into a single exported helper normalizeImagesResponse in a shared module (e.g., add export function normalizeImagesResponse(response?: { data?: Image[] }): Image[] { return (response?.data ?? []) as Image[] } in `@/api/api-functions.ts` or `@/utils/imageHelpers.ts`), then replace the inline pattern in each site: frontend/src/pages/Home/Home.tsx (lines 43-43) — replace const images = (data?.data ?? []) as Image[] with const images = normalizeImagesResponse(data); frontend/src/pages/AITagging/AITagging.tsx (lines 38-38) — replace its (data?.data ?? []) as Image[] usage with normalizeImagesResponse(data); frontend/src/pages/Home/MyFav.tsx (lines 45-45) — replace its (data?.data ?? []) as Image[] usage with normalizeImagesResponse(data); ensure the helper is imported where used and exported from the shared module.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@frontend/src/pages/Home/Home.tsx`:
- Line 43: Extract the repeated normalization logic into a single exported
helper normalizeImagesResponse in a shared module (e.g., add export function
normalizeImagesResponse(response?: { data?: Image[] }): Image[] { return
(response?.data ?? []) as Image[] } in `@/api/api-functions.ts` or
`@/utils/imageHelpers.ts`), then replace the inline pattern in each site:
frontend/src/pages/Home/Home.tsx (lines 43-43) — replace const images =
(data?.data ?? []) as Image[] with const images = normalizeImagesResponse(data);
frontend/src/pages/AITagging/AITagging.tsx (lines 38-38) — replace its
(data?.data ?? []) as Image[] usage with normalizeImagesResponse(data);
frontend/src/pages/Home/MyFav.tsx (lines 45-45) — replace its (data?.data ?? [])
as Image[] usage with normalizeImagesResponse(data); ensure the helper is
imported where used and exported from the shared module.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: acea33d5-98b2-4a52-9e34-cc7f3736d52c
⛔ Files ignored due to path filters (1)
frontend/package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (7)
frontend/jest.setup.tsfrontend/src/pages/AITagging/AITagging.tsxfrontend/src/pages/Home/Home.tsxfrontend/src/pages/Home/MyFav.tsxfrontend/src/pages/__tests__/PageSanity.test.tsxfrontend/src/pages/__tests__/SettingsPage.test.tsxfrontend/src/pages/__tests__/allPages.test.tsx
Fixes #1320
Addressed Issues:
act(...)warning traces during Settings Page testing.TypeError: Cannot read properties of undefined (reading 'length')) when backend endpoints resolve empty/unmocked payloads.Screenshots/Recordings:
N/A (This PR contains non-visual testing fixes, mock updates, and defensive state fallbacks).
Additional Notes:
This PR addresses two key issues in the testing suite and frontend components:
?? []) when dispatching fetched images to Redux (Home.tsx,AITagging.tsx,MyFav.tsx). This prevents components from crashing when the API returns undefined or missing lists.fetchmock injest.setup.tsto return{ success: true, data: [] }(of typeBackendRes) instead of{}.SettingsPage.test.tsx,PageSanity.test.tsx, andallPages.test.tsxsettings test blocks to run asynchronously and wait for the mount-time fetch microtasks to resolve insideact(). This completely resolves the Reactact(...)warnings.All 131 unit tests pass cleanly with zero console warnings/errors.
AI Usage Disclosure:
Check one of the checkboxes below:
I have used the following AI models and tools: Gemini (Antigravity AI coding assistant)
Checklist
Summary by CodeRabbit
Bug Fixes
Tests