PictoPy icon indicating copy to clipboard operation
PictoPy copied to clipboard

fix: resolve act warnings and prevent runtime crashes on empty data

Open MayankSharma-ops opened this issue 5 months ago β€’ 1 comments

Fixes #1320

This pull request resolves multiple React act(...) console warning traces in the settings page test suite and introduces defensive fallbacks to prevent frontend runtime errors when backend fetch endpoints return empty or unmocked payloads. It includes component-level state mapping fixes, updated test mocks, and async wrapper adjustments in test suites.

Defensive state checks and test robustness:

  • Added safety fallbacks (?? []) when dispatching fetched images to Redux in Home.tsx, AITagging.tsx, and MyFav.tsx to prevent components from crashing when the API returns undefined or missing lists.
  • Standardized the default global fetch mock in jest.setup.ts to return { success: true, data: [] } (of type BackendRes) instead of an empty object {}.
  • Updated test blocks in SettingsPage.test.tsx, PageSanity.test.tsx, and allPages.test.tsx to run asynchronously and wait for mount-time fetch microtasks to resolve inside act(), eliminating all act(...) warnings.

Screenshots/Recordings: N/A (This PR contains non-visual testing fixes, mock updates, and defensive state fallbacks).

Additional Notes:

AI Usage Disclosure:

  • [ ] This PR does not contain AI-generated code at all.
  • [x] This PR contains AI-generated code. I have read the AI Usage Policy and this PR complies with this policy. I have tested the code locally and I am responsible for it. I have used the following AI models and tools: Gemini (Antigravity AI coding assistant)

Checklist

  • [x] My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • [x] My code follows the project's code style and conventions
  • [x] If applicable, I have made corresponding changes or additions to the documentation
  • [x] If applicable, I have made corresponding changes or additions to tests
  • [x] My changes generate no new warnings or errors
  • [ ] I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • [x] My read the Contribution Guidelines
  • [x] Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
  • [x] I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of missing or null data in image gallery pages to prevent errors and ensure stable behavior when loading gallery images.
  • Tests

    • Updated test suite to properly support asynchronous React component rendering checks.

MayankSharma-ops avatar Jun 16 '26 05:06 MayankSharma-ops

Review Change Stack

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 74a07fba-0681-43de-b06e-8ee0cfce2df2

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 3c1a34f1abe3fc44036fadda64c6eea2c747ca47 and 8cefc20813f573504d44928067e83f2144146b4b.

πŸ“’ Files selected for processing (1)
  • frontend/src/components/FolderPicker/DeleteImageDialog.tsx
βœ… Files skipped from review due to trivial changes (1)
  • frontend/src/components/FolderPicker/DeleteImageDialog.tsx

Walkthrough

Three page components (AITagging, Home, MyFav) now use ?? [] fallbacks before dispatching setImages, preventing undefined propagation. The /models/setup fetch mock is updated to return a valid payload. Three test files (PageSanity, SettingsPage, allPages) are converted to async tests using act-wrapped zero-delay timers to suppress React act() warnings. An accessibility attribute is added to DeleteImageDialog.

Changes

Defensive null-safe dispatch and async test stabilization

Layer / File(s) Summary
Null-safe image dispatch and fetch mock
frontend/jest.setup.ts, frontend/src/pages/AITagging/AITagging.tsx, frontend/src/pages/Home/Home.tsx, frontend/src/pages/Home/MyFav.tsx
AITagging, Home, and MyFav replace data?.data as Image[] with (data?.data ?? []) as Image[] in their useEffect dispatch calls. The /models/setup fetch mock is updated from {} to { success: true, data: [] }.
Async act() stabilization in tests
frontend/src/pages/__tests__/PageSanity.test.tsx, frontend/src/pages/__tests__/SettingsPage.test.tsx, frontend/src/pages/__tests__/allPages.test.tsx
All three test files add act to imports. setupTest in SettingsPage.test.tsx is converted to an async helper with an act-wrapped zero-timeout flush; all six call sites are updated to await setupTest(). PageSanity and allPages tests similarly become async and insert await act(async () => ...) deferred waits before assertions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

TypeScript/JavaScript

Suggested reviewers

  • rohan-pandeyy

Poem

πŸ‡ A rabbit checks the data flow, No undefined shall slip below! ?? [] keeps the list in shape, While act() seals each async escape. The tests all pass, the warnings gone β€” Hop hop hooray, the code hops on! 🌟

πŸš₯ Pre-merge checks | βœ… 4
βœ… Passed checks (4 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title accurately summarizes the main changes: resolving act warnings and adding defensive fallbacks for empty data.
Linked Issues check βœ… Passed All coding requirements from issue #1320 are addressed: act warnings fixed via async test patterns, and defensive fallbacks added for null/undefined data.
Out of Scope Changes check βœ… Passed All changes are directly related to the stated objectives: test fixes, defensive fallbacks, and mock standardization. No unrelated modifications detected.

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

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

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.

❀️ Share

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

coderabbitai[bot] avatar Jun 16 '26 05:06 coderabbitai[bot]

Thanks for your contribution!

rohan-pandeyy avatar Jun 17 '26 18:06 rohan-pandeyy