fix: resolve act warnings and prevent runtime crashes on empty data
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 inHome.tsx,AITagging.tsx, andMyFav.tsxto prevent components from crashing when the API returns undefined or missing lists. - Standardized the default global
fetchmock injest.setup.tsto return{ success: true, data: [] }(of typeBackendRes) instead of an empty object{}. - Updated test blocks in
SettingsPage.test.tsx,PageSanity.test.tsx, andallPages.test.tsxto run asynchronously and wait for mount-time fetch microtasks to resolve insideact(), eliminating allact(...)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.
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, Whileact()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.
Comment @coderabbitai help to get the list of available commands and usage tips.
Thanks for your contribution!