test: improve 100% test coverage for i18n and campaign signup page
## Issue Summary
Implemented a comprehensive i18n test suite ensuring 100% test coverage for internationalization configuration, language handling, and fallback behavior.
## Problem Statement
The i18n module previously lacked complete test coverage, which could lead to:
- Undetected issues in language switching
- Missing validation for fallback behavior
- No verification of missing translation handling
- Risk of breaking interpolation and formatting logic
- Lack of confidence in RTL support and namespace handling
This affected overall application reliability and maintainability, especially in multilingual scenarios.
## Proposed Solution
-
Developed a full test suite using Vitest for i18n module
-
Covered all critical areas:
- Initialization config
- Language switching (
changeLanguage) - Language retrieval (
getLanguage) - RTL detection (
isRTL) - Missing key handling
- Interpolation formatting
- Namespace handling
- Resource fallback logic
-
Implemented:
- Mocking for
localStorage - Mocking for
console.warn - Environment-based behavior (DEV vs PROD)
- Module isolation using
vi.resetModules()
- Mocking for
Test-Driven Development
Acceptance Criteria (Given-When-Then)
Scenario 1: Language Switching
Given the application is initialized with default language
When the user changes language to Telugu (te)
Then the application language should update to "te"
And the selected language should be stored in localStorage
Scenario 2: Fallback for Invalid Language
Given an unsupported language is provided
When changeLanguage is called
Then the language should fallback to "en"
Scenario 3: Missing Translation Key Handling
Given a translation key does not exist
When i18n.t() is called
Then a warning should be logged in DEV mode
And a fallback string should be returned
Scenario 4: RTL Language Detection
Given the current language is Arabic
When isRTL is called
Then it should return true
Scenario 5: Interpolation Formatting
Given a translation uses interpolation formatting
When the value is passed with uppercase format
Then the result should be transformed to uppercase
### Test Cases
| Test ID | Test Description | Precondition | Test Steps | Expected Result | Priority |
|---|---|---|---|---|---|
| TC-001 | Change language to Telugu | i18n initialized | Call changeLanguage('te') | Language updated & stored | High |
| TC-002 | Invalid language fallback | i18n initialized | Call changeLanguage('fr') | Falls back to "en" | High |
| TC-003 | Missing key handling | DEV mode | Call i18n.t('missing.key') | Warning logged | High |
| TC-004 | RTL detection | Language = 'ar' | Call isRTL() | Returns true | Medium |
| TC-005 | Interpolation uppercase | Value provided | Call i18n.t with format | Returns uppercase | Medium |
### Unit Test Requirements
Components/Functions to Test:
| Module/Component | Function | Test Cases |
|---|---|---|
i18n.ts |
changeLanguage |
TC-001, TC-002 |
i18n.ts |
getLanguage |
Default + fallback |
i18n.ts |
isRTL |
RTL + non-RTL |
i18n.ts |
missingKeyHandler |
DEV + PROD |
i18n.ts |
Interpolation | Formatting cases |
## Implementation Details
Files to Change
| File Path | Action | Purpose |
|---|---|---|
src/i18n.ts |
Existing | i18n configuration |
src/__tests__/i18n.test.ts |
Created | Full test coverage |
### Technical Considerations
-
Used Vitest for unit testing
-
Mocked:
localStorageconsole.warn
-
Used
vi.resetModules()for isolated module testing -
Ensured no dependency on real browser APIs
-
Handled DEV vs PROD behavior explicitly
## Testing Strategy
Manual Testing
- Change language from UI
- Refresh page
- Verify persisted language
- Check fallback behavior for unsupported language
### Automated Testing
Unit Tests:
-
✅ Initialization tests -
✅ Language switching tests -
✅ Missing key handling -
✅ Interpolation formatting -
✅ RTL detection -
✅ Resource fallback coverage
### Test Data
const mockTestData = {
language: 'te',
invalidLanguage: 'fr',
rtlLanguage: 'ar',
text: 'hello'
};
## Definition of Done
-
Acceptance criteria met -
All test cases passing -
Unit tests written (100% coverage achieved) -
Manual testing completed -
Code reviewed -
No console errors -
Type-safe implementation -
ESLint/Prettier passed -
i18n logic fully validated
## Additional Context
Notes
-
Achieved true 100% coverage including:
- Edge cases
- Error handling
- Fallback scenarios
-
Tests ensure high confidence in multilingual support
-
Improves maintainability and prevents regression issues