test : Add comprehensive unit test coverage for Skeleton, Slider, Sonner, and Switch UI with 100%
Issue Summary
Current Behavior: The Skeleton, Slider, Sonner, and Switch components currently lack comprehensive unit test coverage. This increases the risk of regressions and reduces confidence when making future changes.
Expected Behavior: All four components should have complete unit test coverage including rendering, props validation, events, accessibility checks, edge cases, and user interactions with 100% coverage.
Problem Statement
What is missing:
Unit tests for the following UI components are missing or incomplete:
- Skeleton component
- Slider component
- Sonner toaster component
- Switch component
Who is affected:
- Developers working on UI components
- QA team (increased manual testing effort)
- Future contributors modifying these components
Impact:
- Risk of regressions
- No automated verification of UI behavior
- Reduced confidence during refactoring
- Incomplete coverage reports
- Increased maintenance risk
Reproduction Steps:
Run:
npm run test:coverage
Result:
Coverage for these components is missing or incomplete.
Proposed Solution
Approach:
- Create comprehensive unit test files using Vitest and React Testing Library
- Cover rendering, props, events, accessibility, and edge cases
- Add required polyfills for Radix UI components
- Ensure tests follow project conventions
- Achieve 100% coverage without modifying production code
Components/files to change:
| File | Action |
|---|---|
| tests/src/components/ui/skeleton.test.tsx | Create |
| tests/src/components/ui/slider.test.tsx | Create |
| tests/src/components/ui/sonner.test.tsx | Create |
| tests/src/components/ui/switch.test.tsx | Create |
| tests/setup.ts | Modify (polyfills) |
| tsconfig.tests.json | Create |
| vitest.config.ts | Modify |
Dependencies:
None
Test-Driven Development
Acceptance Criteria (Given-When-Then)
Scenario 1: Skeleton renders correctly
Given the Skeleton component is rendered
When the component loads
Then it should render with default classes
And accept custom className props
Scenario 2: Slider handles values and interactions
Given the Slider component is rendered with value props
When the slider value changes
Then the component should update correctly
And call event handlers
Scenario 3: Sonner toaster renders and handles toast events
Given the Sonner toaster component is rendered
When toast notifications are triggered
Then notifications should display correctly
And support toast options
Scenario 4: Switch handles checked and disabled states
Given the Switch component is rendered
When the switch is toggled
Then checked state should update
And disabled state should prevent interaction
Scenario 5: Accessibility validation
Given UI components are rendered
When accessibility attributes are inspected
Then ARIA attributes should be present
And keyboard navigation should work
Test Cases
| Test ID | Test Description | Precondition | Test Steps | Expected Result | Priority |
|---|---|---|---|---|---|
| TC-001 | Skeleton renders | None | Render component | Default classes present | High |
| TC-002 | Skeleton custom class | None | Render with className | Classes merged | High |
| TC-003 | Slider value handling | None | Render with value | Value displayed correctly | High |
| TC-004 | Slider disabled state | None | Render disabled | Interaction blocked | High |
| TC-005 | Sonner toast render | None | Trigger toast | Toast displayed | Medium |
| TC-006 | Switch toggle | None | Click switch | State changes | High |
| TC-007 | Switch disabled | None | Disable switch | No toggle allowed | High |
| TC-008 | Accessibility checks | None | Inspect ARIA | Attributes present | Medium |
Unit Test Requirements
Components/Functions to Test:
| Module/Component | Function | Test Cases |
|---|---|---|
| src/components/ui/skeleton.tsx | Skeleton | TC-001, TC-002 |
| src/components/ui/slider.tsx | Slider | TC-003, TC-004 |
| src/components/ui/sonner.tsx | Toaster | TC-005 |
| src/components/ui/switch.tsx | Switch | TC-006, TC-007 |
Test Assertions Required:
-
Renders correctly with initial state -
Handles user interaction correctly -
Validates props and classes -
Handles disabled state -
Accessibility validation -
Ref forwarding -
Event handler verification -
UI state updates correctly
Implementation Details
Files to Change
| File Path | Action | Purpose |
|---|---|---|
| tests/src/components/ui/skeleton.test.tsx | Create | Skeleton tests |
| tests/src/components/ui/slider.test.tsx | Create | Slider tests |
| tests/src/components/ui/sonner.test.tsx | Create | Sonner tests |
| tests/src/components/ui/switch.test.tsx | Create | Switch tests |
| tests/setup.ts | Modify | Add polyfills |
| tsconfig.tests.json | Create | Test configuration |
| vitest.config.ts | Modify | Path alias support |
Technical Considerations
- Radix UI requires jsdom polyfills
- ResizeObserver polyfill required
- Pointer events polyfill required
- matchMedia polyfill required
- ForwardRef testing required
- Tailwind class merging validation required
UI/UX Requirements
Component behavior:
Skeleton:
- Loading animation
- Default styling
- Accepts HTML attributes
Slider:
- Track and thumb interaction
- Value updates
- Keyboard navigation
Sonner:
- Toast display
- Theme support
- Toast configuration
Switch:
- Toggle interaction
- Disabled state styling
- Accessibility support
Accessibility Requirements:
- Proper ARIA attributes
- Keyboard navigation support
- Focus indicators
Testing Strategy
Manual Testing
Test Environment:
- Browser: Chrome, Firefox
- Viewport: Desktop
- User Role: All users
Manual Test Steps:
- Render components
- Verify interactions
- Verify disabled states
- Verify accessibility behavior
Automated Testing
Unit Tests:
-
Component render tests -
User interaction tests -
Error handling tests -
Accessibility tests
Integration Tests:
-
Component interaction tests
Coverage Target:
100%
Test Data Requirements
Example mock data:
const mockTestData = { sliderValue: [50], rangeValue: [25,75], switchState: true, toastMessage: "Test notification" }
Definition of Done
-
Acceptance criteria met -
All test cases passing -
Unit tests written (100% coverage) -
Manual testing completed -
Code reviewed and approved -
Accessibility checks passed -
No console errors -
TypeScript checks pass -
ESLint passes -
Prettier passes -
Documentation updated
Additional Context
Related Issues
Related to test coverage improvements for UI components.
References
Testing tools used:
- Vitest
- React Testing Library
- Radix UI documentation
Notes
- Only test files added
- No production code modified
- Improves maintainability
- Reduces regression risks
- Follows project testing standards