test: unify i18next mocks into centralized helpers#7
Conversation
Consolidate scattered i18next mock implementations across test files into a single source of truth. This reduces duplication and ensures consistent mock behavior. - Create test/i18n-mock.ts with reusable factory functions - Update vitest.setup.ts to use the centralized helpers - Remove redundant mock definitions from 8 test files - Update testing.md documentation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…eateReactI18nextMock` and detail global mock provisions.
📝 WalkthroughWalkthroughThis change refactors i18n (internationalization) testing infrastructure by centralizing mock implementations. A new i18n mock utility module is introduced and existing inline react-i18next mocks across test files are either removed or replaced with the centralized helper, alongside updates to documentation and global test setup. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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 review |
✅ Actions performedReview triggered.
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@web/test/i18n-mock.ts`:
- Around line 39-47: The useTranslation mock in createUseTranslationMock
currently ignores namespace args; update the returned useTranslation function to
accept an optional namespace parameter (string | string[]) and forward it to
createTFunction as the defaultNs (if an array, use the first element), so that t
produced by createTFunction receives the namespace and keys are prefixed as in
react-i18next; adjust the signature in createUseTranslationMock and ensure
i18n.changeLanguage remains vi.fn() and the i18n.language stays 'en'.
| export function createUseTranslationMock(translations: TranslationMap = {}) { | ||
| return { | ||
| useTranslation: () => ({ | ||
| t: createTFunction(translations), | ||
| i18n: { | ||
| language: 'en', | ||
| changeLanguage: vi.fn(), | ||
| }, | ||
| }), |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# First, let's examine the i18n-mock.ts file to understand the current implementation
find . -name "i18n-mock.ts" -type fRepository: agentic-review-benchmarks/dify-combined-coderabbit
Length of output: 117
🏁 Script executed:
# Search for createTFunction definition to understand what parameters it accepts
rg "createTFunction" --type typescript -A 10 -B 2Repository: agentic-review-benchmarks/dify-combined-coderabbit
Length of output: 128
🏁 Script executed:
# Search for existing useTranslation usage patterns in tests to see if namespace is used
rg "useTranslation\s*\(" --type typescript -A 2 | head -100Repository: agentic-review-benchmarks/dify-combined-coderabbit
Length of output: 128
🌐 Web query:
react-i18next useTranslation hook API namespace parameter documentation
💡 Result:
In react-i18next, the useTranslation hook takes an optional namespace argument:
useTranslation(ns?: string | string[], options?: UseTranslationOptions)- No namespace passed →
tis bound todefaultNS(defaults to"translation"). (react.i18next.com) - Single namespace (
useTranslation('ns1')) →t('key')looks up keys in that namespace. (react.i18next.com) - Multiple namespaces (
useTranslation(['ns1','ns2'])) →tis bound to the first namespace by default, and you can target another witht('key', { ns: 'ns2' }). (react.i18next.com)
The docs for this are on the useTranslation (hook) page (see “Loading namespaces”). (react.i18next.com)
Citations:
- 1: https://react.i18next.com/latest/usetranslation-hook
- 2: https://react.i18next.com/latest/usetranslation-hook
- 3: https://react.i18next.com/latest/usetranslation-hook
- 4: https://react.i18next.com/latest/usetranslation-hook
🏁 Script executed:
cat -n web/test/i18n-mock.tsRepository: agentic-review-benchmarks/dify-combined-coderabbit
Length of output: 2912
🏁 Script executed:
rg "createTFunction" -A 15 -B 2Repository: agentic-review-benchmarks/dify-combined-coderabbit
Length of output: 2004
🏁 Script executed:
rg "useTranslation" -A 3 web/test --maxcount 20Repository: agentic-review-benchmarks/dify-combined-coderabbit
Length of output: 533
🏁 Script executed:
rg "useTranslation\(" web/test --max-count 20 -A 2Repository: agentic-review-benchmarks/dify-combined-coderabbit
Length of output: 76
🏁 Script executed:
# Search for any tests that might use namespace parameter
rg "useTranslation\s*\(\s*['\"]" web --max-count 20Repository: agentic-review-benchmarks/dify-combined-coderabbit
Length of output: 494
Add namespace parameter support to the useTranslation mock.
useTranslation in react-i18next accepts an optional namespace argument, but the mock currently ignores it. This causes t('key') to not be prefixed with the namespace, diverging from real library behavior. Existing code in the repository uses useTranslation('appDebug') and useTranslation('datasetSettings'), so this mock limitation can cause tests to behave incorrectly.
The infrastructure is already in place—createTFunction accepts a defaultNs parameter—but useTranslation needs to accept and forward the namespace argument.
Proposed fix
export function createUseTranslationMock(translations: TranslationMap = {}) {
return {
- useTranslation: () => ({
- t: createTFunction(translations),
+ useTranslation: (ns?: string | string[]) => ({
+ t: createTFunction(translations, Array.isArray(ns) ? ns[0] : ns),
i18n: {
language: 'en',
changeLanguage: vi.fn(),
},
}),
}
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| export function createUseTranslationMock(translations: TranslationMap = {}) { | |
| return { | |
| useTranslation: () => ({ | |
| t: createTFunction(translations), | |
| i18n: { | |
| language: 'en', | |
| changeLanguage: vi.fn(), | |
| }, | |
| }), | |
| export function createUseTranslationMock(translations: TranslationMap = {}) { | |
| return { | |
| useTranslation: (ns?: string | string[]) => ({ | |
| t: createTFunction(translations, Array.isArray(ns) ? ns[0] : ns), | |
| i18n: { | |
| language: 'en', | |
| changeLanguage: vi.fn(), | |
| }, | |
| }), | |
| } | |
| } |
🤖 Prompt for AI Agents
In `@web/test/i18n-mock.ts` around lines 39 - 47, The useTranslation mock in
createUseTranslationMock currently ignores namespace args; update the returned
useTranslation function to accept an optional namespace parameter (string |
string[]) and forward it to createTFunction as the defaultNs (if an array, use
the first element), so that t produced by createTFunction receives the namespace
and keys are prefixed as in react-i18next; adjust the signature in
createUseTranslationMock and ensure i18n.changeLanguage remains vi.fn() and the
i18n.language stays 'en'.
Benchmark PR from agentic-review-benchmarks#7
Summary by CodeRabbit
Tests
Documentation