Skip to content

test: unify i18next mocks into centralized helpers#7

Open
tomerqodo wants to merge 5 commits intocoderabbit_full_base_test_unify_i18next_mocks_into_centralized_helpers_pr7from
coderabbit_full_head_test_unify_i18next_mocks_into_centralized_helpers_pr7
Open

test: unify i18next mocks into centralized helpers#7
tomerqodo wants to merge 5 commits intocoderabbit_full_base_test_unify_i18next_mocks_into_centralized_helpers_pr7from
coderabbit_full_head_test_unify_i18next_mocks_into_centralized_helpers_pr7

Conversation

@tomerqodo
Copy link

@tomerqodo tomerqodo commented Jan 30, 2026

Benchmark PR from agentic-review-benchmarks#7

Summary by CodeRabbit

  • Tests

    • Refactored internationalization (i18n) testing infrastructure to use a centralized mock utility, replacing redundant inline implementations across test files.
    • Streamlined translation mocking patterns to improve test maintainability and consistency.
  • Documentation

    • Updated testing guides and templates with the new i18n mocking approach and best practices.

hyoban and others added 5 commits January 25, 2026 12:05
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.
@coderabbitai
Copy link

coderabbitai bot commented Jan 30, 2026

📝 Walkthrough

Walkthrough

This 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

Cohort / File(s) Summary
New i18n Mock Utility
web/test/i18n-mock.ts
Introduces helper functions (createTFunction, createUseTranslationMock, createTransMock, createReactI18nextMock) for consistent i18n mocking in tests, supporting namespace-based key resolution and fallback behavior.
Simple Mock Removals
web/app/components/app/configuration/config/agent-setting-button.spec.tsx, web/app/components/app/configuration/config/config-audio.spec.tsx, web/app/components/billing/pricing/footer.spec.tsx, web/app/components/datasets/create/index.spec.tsx, web/app/components/datasets/documents/create-from-pipeline/processing/index.spec.tsx, web/app/components/plugins/install-plugin/install-from-local-package/steps/uploading.spec.tsx, web/app/components/plugins/plugin-detail-panel/subscription-list/create/common-modal.spec.tsx, web/app/components/plugins/plugin-detail-panel/subscription-list/create/oauth-client.spec.tsx, web/app/components/plugins/plugin-detail-panel/subscription-list/edit/index.spec.tsx
Removed inline react-i18next mock implementations, relying on global mock or centralized helper instead.
Multiple Mock Removals
web/app/components/plugins/card/index.spec.tsx, web/app/components/plugins/plugin-mutation-model/index.spec.tsx
Removed mocks for multiple i18n-related hooks (useTranslation, useMixedTranslation, useGetLanguage) from test setup.
Mock Replacement with Helper
web/app/components/base/inline-delete-confirm/index.spec.tsx, web/app/components/base/input-with-copy/index.spec.tsx, web/app/components/base/input/index.spec.tsx
Replaced inline react-i18next mocks with explicit createReactI18nextMock calls, providing translation maps for tested keys.
Async Mock Pattern
web/app/components/plugins/install-plugin/install-from-local-package/steps/install.spec.tsx
Refactored mock to use asynchronous importOriginal pattern, merging real module exports with centralized i18n mock and custom Trans component override.
Updated Expectations
web/app/components/plugins/marketplace/index.spec.tsx
Updated test expectations to reflect namespace-prefixed translation keys (e.g., plugin.category.all) and adjusted mock logic for getFixedT to prefix keys based on options.ns.
Documentation & Setup Updates
.claude/skills/frontend-testing/assets/component-test.template.tsx, .claude/skills/frontend-testing/references/mocking.md, web/testing/testing.md, web/vitest.setup.ts
Updated i18n mocking documentation, test template, and global test setup to use centralized createReactI18nextMock helper and document preferred patterns.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 The mocks, once scattered far and wide,
Now huddle in one test-file home with pride.
A helper born to tame the i18n fray—
Less duplication, more reliability, hooray!
✨ Translation tests just got their happy day.

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is a single brief line referencing an external benchmark PR, missing all required sections from the template including summary, screenshots, issue links, and checklist. Provide a comprehensive description following the template: summarize changes, link to associated issue(s), include before/after context, and confirm checklist items.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: consolidating scattered i18next mocks into centralized helpers to reduce duplication.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch coderabbit_full_head_test_unify_i18next_mocks_into_centralized_helpers_pr7

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@tomerqodo
Copy link
Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jan 30, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@tomerqodo
Copy link
Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jan 30, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@tomerqodo
Copy link
Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jan 30, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@ravidqodo
Copy link

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Feb 2, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'.

Comment on lines +39 to +47
export function createUseTranslationMock(translations: TranslationMap = {}) {
return {
useTranslation: () => ({
t: createTFunction(translations),
i18n: {
language: 'en',
changeLanguage: vi.fn(),
},
}),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# First, let's examine the i18n-mock.ts file to understand the current implementation
find . -name "i18n-mock.ts" -type f

Repository: 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 2

Repository: 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 -100

Repository: 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 passedt is bound to defaultNS (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'])) → t is bound to the first namespace by default, and you can target another with t('key', { ns: 'ns2' }). (react.i18next.com)

The docs for this are on the useTranslation (hook) page (see “Loading namespaces”). (react.i18next.com)

Citations:


🏁 Script executed:

cat -n web/test/i18n-mock.ts

Repository: agentic-review-benchmarks/dify-combined-coderabbit

Length of output: 2912


🏁 Script executed:

rg "createTFunction" -A 15 -B 2

Repository: agentic-review-benchmarks/dify-combined-coderabbit

Length of output: 2004


🏁 Script executed:

rg "useTranslation" -A 3 web/test --maxcount 20

Repository: agentic-review-benchmarks/dify-combined-coderabbit

Length of output: 533


🏁 Script executed:

rg "useTranslation\(" web/test --max-count 20 -A 2

Repository: 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 20

Repository: 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.

Suggested change
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'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants