Improve Test Coverage for `DashboardPage.tsx` to 100%
Current Behavior:
DashboardPage.tsx has partial test coverage with several uncovered lines and low function coverage.
Expected Behavior: Achieve 100% test coverage by covering all logic paths, UI states, and interactions.
Problem Statement
-
What is missing? Large portion of code (lines 506–698) is not covered by tests, including complex UI logic and event handling.
-
Who is affected? Developers maintaining dashboard features and QA relying on coverage.
-
Impact:
- Potential hidden bugs
- Low confidence in dashboard functionality
- Risk during future updates
-
Coverage Stats:
- Statements: 75.67%
- Branches: 90.69%
- Functions: 64%
❗ (needs major improvement) - Lines: 75%
-
Uncovered Lines:
506–698
Proposed Solution
-
Add unit tests using Vitest + React Testing Library
-
Focus on:
- Dashboard data rendering
- Conditional UI blocks
- Event handlers (filters, navigation, actions)
- API success, error, and empty states
Test-Driven Development
Acceptance Criteria (Given-When-Then)
Scenario 1: Dashboard Loads Successfully
Given dashboard API returns valid data
When the page is rendered
Then dashboard metrics and charts should be displayed
Scenario 2: Loading State
Given dashboard data is being fetched
When the page renders
Then a loading indicator should be visible
Scenario 3: Empty Data State
Given dashboard API returns no data
When the page renders
Then an empty state message should be shown
Scenario 4: API Error Handling
Given dashboard API fails
When the page renders
Then an error message should be displayed
Scenario 5: User Interaction (Filters / Actions)
Given dashboard is loaded
When user applies filters or clicks actions
Then UI should update accordingly
Scenario 6: Function Execution Coverage
Given dashboard helper functions exist
When triggered by UI or lifecycle
Then all functions should execute and return expected results
Test Cases
| Test ID | Test Description | Precondition | Test Steps | Expected Result | Priority |
|---|---|---|---|---|---|
| TC-001 | Render dashboard data | API success | 1. Mock data 2. Render |
Data visible | High |
| TC-002 | Loading state | API pending | 1. Render | Loader shown | High |
| TC-003 | Empty state | No data | 1. Mock empty | Empty message | High |
| TC-004 | API error | API fails | 1. Mock error | Error UI | High |
| TC-005 | Filter interaction | Dashboard loaded | 1. Apply filter | UI updates | Medium |
| TC-006 | Function execution | Component active | 1. Trigger logic | Functions covered | High |
Unit Test Requirements
Components/Functions to Test:
| Module/Component | Function/Hook | Test Cases |
|---|---|---|
DashboardPage.tsx |
Rendering logic | TC-001, TC-002, TC-003 |
| API handling | TC-004 | |
| Interaction handlers | TC-005 | |
| Internal functions | TC-006 |
Test Assertions Required:
-
Renders dashboard correctly -
Displays loading state -
Handles empty state -
Handles API errors -
Executes all functions -
Updates UI on user interaction
Implementation Details
Files to Change
| File Path | Action | Purpose |
|---|---|---|
src/pages/DashboardPage.tsx |
Modify | Improve testability if needed |
src/__tests__/DashboardPage.test.tsx |
Create/Modify | Add test cases |
Technical Considerations
- Use
vi.mock()for API mocking - Use
waitFor()for async behavior - Use
userEventfor interactions - Ensure full function coverage
UI/UX Requirements
-
Validate:
- Charts rendering
- Dashboard metrics display
- Filters and interactions
-
Ensure accessibility compliance
Testing Strategy
Manual Testing
Environment:
- Browser: Chrome
- Viewport: Desktop
- Role: Admin
Steps:
- Open dashboard page
- Verify data loads
- Apply filters
- Check error handling
Automated Testing
-
Component render tests -
Interaction tests -
API tests -
Function coverage tests
Test Data
const mockDashboardData = {
totalDoctors: 10,
totalPatients: 50,
totalAppointments: 20,
};
Definition of Done
-
Coverage reaches 100% -
All uncovered lines (506–698) tested -
Function coverage improved to 100% -
All tests pass -
No dummy tests
Additional Context
Notes
-
Focus heavily on:
- Lines 506–698
- Function coverage (currently 64%)
-
Avoid shallow testing
-
Ensure real user scenarios are covered