test: improve test coverage for usePatientStatus and useQueueSocket hooks (branches & edge cases)
📝 Overview
This MR improves unit test coverage for the custom hooks usePatientStatus.ts and useQueueSocket.ts by covering previously untested branches and edge cases.
The goal is to increase overall coverage to ≥95%, ensuring better reliability of patient status handling and real-time queue updates. This change enhances confidence during refactoring and reduces the risk of runtime issues in asynchronous and socket-based logic.
💡 What does this MR do and why?
The motivation behind this change was insufficient branch coverage in both hooks:
- usePatientStatus.ts: ~77% branch coverage
- useQueueSocket.ts: ~64% branch coverage
Critical logic such as conditional branches, socket events, and cleanup behavior were not fully tested.
Approach:
- Added comprehensive unit tests using Jest and React Testing Library (renderHook)
- Mocked socket connections and API responses
- Explicitly triggered uncovered branches and edge cases
- Tested lifecycle behavior including initialization and cleanup
Trade-offs:
- Increased test complexity and file size, but significantly improved reliability and maintainability
## 🔧 **Changes Made**
- Added unit tests for usePatientStatus.ts and useQueueSocket.ts
- Covered previously uncovered lines:
- usePatientStatus.ts: 152–153, 163
- useQueueSocket.ts: 56–57, 114
- Tested conditional branches and edge cases
- Mocked socket.io-client for event handling
- Added tests for:
- Hook initialization
- State updates
- Invalid/edge input handling
- Socket connection and events
- Cleanup logic (unsubscribe on unmount)
- Error handling scenarios
- Improved branch, function, and line coverage to ≥95%
## 🔍 **Technical Details**
Root Cause:
Low branch coverage due to missing test cases for conditional logic, socket lifecycle, and edge cases.
Fix:
- Used renderHook for testing custom hooks
- Used act() for state updates
- Mocked socket connections using jest.mock("socket.io-client")
- Simulated socket events (on, emit, off)
- Verified cleanup using unmount()
Key Areas Covered:
- Conditional branches (if/else, early returns)
- Socket lifecycle (connect → event → cleanup)
- Error scenarios and fallback handling
## 🏷️ **Type of Change**
- [ ] 🐛 Bug fix
- [ ] ✨ New feature
- [ ] 💥 Breaking change
- [ ] 📝 Documentation update
- [ ] 🎨 UI/UX improvement
- [ ] ♻️ Refactor
- [x] ⚡ Performance improvement
- [x] 🧪 Test update
- [ ] 🔧 Configuration change
- [ ] 🚨 Security fix
## 🔗 **Related Issues / References**
#274
Below shows the improvement in test coverage for both hooks.
| Before | After |
|---|---|
| Low branch coverage (~77% / ~64%) | Coverage improved to ≥95% |
| Uncovered conditional logic and edge cases | All branches and edge cases covered |
| Missing socket lifecycle tests | Full socket lifecycle tested |
🧪 How to Set Up and Validate Locally
# 1. Checkout branch
git checkout <branch-name>
# 2. Install dependencies
bun install
# 3. Run tests
npm run test
# 4. Run coverage
npm run test -- --coverage
Expected:
- All tests pass
✅ - Coverage ≥95% for both hooks
🧪 Testing Done
- [x] Manual testing completed
- [x] Unit tests added/updated
Test Cases Covered
| Scenario | Expected Result | Status |
|---|---|---|
| Initialize usePatientStatus hook | Default state set correctly | |
| Update patient status | State updates based on input data | |
| Handle invalid patient data | No crash, fallback handled | |
| Cover conditional branches (152–153) | Branch logic executed | |
| Cover edge case (163) | Expected behavior achieved | |
| Initialize socket connection | Socket connects successfully | |
| Receive queue updates | State updates on socket event | |
| Cover socket branch (56–57) | Branch executed correctly | |
| Cleanup on unmount | Socket listeners removed | |
| Handle socket error (114) | Error handled gracefully |
⚠ ️ Known Limitations / Technical Debt
- Tests focus on unit-level coverage; integration tests can be added for full workflow validation
- Socket mocking may not fully replicate real-world network conditions
📝 Additional Notes
- This MR does not introduce any functional changes
- Focus is purely on improving test reliability and coverage
- Ensures safer future enhancements for real-time features
Edited by Sai Jeevana Jyothi Chesetti