Skip to content

Commit 008ab8c

Browse files
author
feifei
committed
fix: mock react-i18next for unit tests
1 parent e114350 commit 008ab8c

File tree

9 files changed

+136
-3
lines changed

9 files changed

+136
-3
lines changed

src/shared/support-prompt.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const supportPromptConfigs: Record<string, SupportPromptConfig> = {
3434
ENHANCE: {
3535
label: "supportPrompt.ENHANCE.label",
3636
description: "supportPrompt.ENHANCE.description",
37-
template: `Generate an enhanced version of this prompt (reply with only the enhanced prompt - no conversation, explanations, lead-in, bullet points, or surrounding quotes):
37+
template: `Generate an enhanced version of this prompt (reply with only the enhanced prompt - no conversation, explanations, lead-in, bullet points, placeholders, or surrounding quotes):
38+
3839
\${userInput}`,
3940
},
4041
EXPLAIN: {
@@ -84,6 +85,7 @@ Please suggest improvements for:
8485
2. Performance optimization
8586
3. Best practices and patterns
8687
4. Error handling and edge cases
88+
8789
Provide the improved code along with explanations for each enhancement.`,
8890
},
8991
ADD_TO_CONTEXT: {
@@ -95,8 +97,9 @@ Provide the improved code along with explanations for each enhancement.`,
9597
\`\`\``,
9698
},
9799
TERMINAL_ADD_TO_CONTEXT: {
98-
label: "supportPrompt.TERMINAL_ADD_TO_CONTEXT.label",
99-
description: "supportPrompt.TERMINAL_ADD_TO_CONTEXT.description",
100+
label: "Add Terminal Content to Context",
101+
description:
102+
"Add terminal output to your current task or conversation. Useful for providing command outputs or logs. Available in the terminal context menu (right-click on selected terminal content).",
100103
template: `\${userInput}
101104
Terminal output:
102105
\`\`\`
@@ -111,6 +114,7 @@ Fix this terminal command:
111114
\`\`\`
112115
\${terminalContent}
113116
\`\`\`
117+
114118
Please:
115119
1. Identify any issues in the command
116120
2. Provide the corrected command

webview-ui/src/components/chat/__tests__/ChatView.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@ import { render, waitFor } from "@testing-library/react"
33
import ChatView from "../ChatView"
44
import { ExtensionStateContextProvider } from "../../../context/ExtensionStateContext"
55
import { vscode } from "../../../utils/vscode"
6+
import en from "../../../i18n/locales/en.json"
7+
8+
// Mock react-i18next
9+
jest.mock("react-i18next", () => ({
10+
useTranslation: () => ({
11+
t: (key: string) => {
12+
// 通过点号分割 key 来支持嵌套的翻译
13+
const keys = key.split(".")
14+
let value: any = en
15+
for (const k of keys) {
16+
value = value?.[k]
17+
}
18+
return value || key // 如果找不到翻译,返回 key 本身
19+
},
20+
i18n: { changeLanguage: jest.fn() },
21+
}),
22+
Trans: ({ children }: { children: React.ReactNode }) => <span>{children}</span>, // Mock Trans 组件
23+
}))
624

725
// Define minimal types needed for testing
826
interface ClineMessage {

webview-ui/src/components/history/HistoryPreview.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ import { vscode } from "../../utils/vscode"
55
import { memo } from "react"
66
import { formatLargeNumber } from "../../utils/format"
77
import { useCopyToClipboard } from "../../utils/clipboard"
8+
import en from "../../i18n/locales/en.json"
9+
10+
// Mock react-i18next
11+
jest.mock("react-i18next", () => ({
12+
useTranslation: () => ({
13+
t: (key: string) => {
14+
// 通过点号分割 key 来支持嵌套的翻译
15+
const keys = key.split(".")
16+
let value: any = en
17+
for (const k of keys) {
18+
value = value?.[k]
19+
}
20+
return value || key // 如果找不到翻译,返回 key 本身
21+
},
22+
i18n: { changeLanguage: jest.fn() },
23+
}),
24+
Trans: ({ children }: { children: React.ReactNode }) => <span>{children}</span>, // Mock Trans 组件
25+
}))
826

927
type HistoryPreviewProps = {
1028
showHistoryView: () => void

webview-ui/src/components/history/HistoryView.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ import { highlightFzfMatch } from "../../utils/highlight"
1111
import { useCopyToClipboard } from "../../utils/clipboard"
1212
import { Button } from "../ui"
1313
import { useTranslation } from "react-i18next"
14+
import en from "../../i18n/locales/en.json"
15+
// Mock react-i18next
16+
jest.mock("react-i18next", () => ({
17+
useTranslation: () => ({
18+
t: (key: string) => {
19+
// 通过点号分割 key 来支持嵌套的翻译
20+
const keys = key.split(".")
21+
let value: any = en
22+
for (const k of keys) {
23+
value = value?.[k]
24+
}
25+
return value || key // 如果找不到翻译,返回 key 本身
26+
},
27+
i18n: { changeLanguage: jest.fn() },
28+
}),
29+
Trans: ({ children }: { children: React.ReactNode }) => <span>{children}</span>, // Mock Trans 组件
30+
}))
1431

1532
type HistoryViewProps = {
1633
onDone: () => void

webview-ui/src/components/history/__tests__/HistoryView.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ import { render, screen, fireEvent, within, act } from "@testing-library/react"
44
import HistoryView from "../HistoryView"
55
import { useExtensionState } from "../../../context/ExtensionStateContext"
66
import { vscode } from "../../../utils/vscode"
7+
import en from "../../../i18n/locales/en.json"
8+
9+
// Mock react-i18next
10+
jest.mock("react-i18next", () => ({
11+
useTranslation: () => ({
12+
t: (key: string) => {
13+
// 通过点号分割 key 来支持嵌套的翻译
14+
const keys = key.split(".")
15+
let value: any = en
16+
for (const k of keys) {
17+
value = value?.[k]
18+
}
19+
return value || key // 如果找不到翻译,返回 key 本身
20+
},
21+
i18n: { changeLanguage: jest.fn() },
22+
}),
23+
Trans: ({ children }: { children: React.ReactNode }) => <span>{children}</span>, // Mock Trans 组件
24+
}))
725

826
jest.mock("../../../context/ExtensionStateContext")
927
jest.mock("../../../utils/vscode")

webview-ui/src/components/mcp/__tests__/McpToolRow.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ import React from "react"
22
import { render, fireEvent, screen } from "@testing-library/react"
33
import McpToolRow from "../McpToolRow"
44
import { vscode } from "../../../utils/vscode"
5+
import en from "../../../i18n/locales/en.json"
6+
7+
// Mock react-i18next
8+
jest.mock("react-i18next", () => ({
9+
useTranslation: () => ({
10+
t: (key: string) => {
11+
// 通过点号分割 key 来支持嵌套的翻译
12+
const keys = key.split(".")
13+
let value: any = en
14+
for (const k of keys) {
15+
value = value?.[k]
16+
}
17+
return value || key // 如果找不到翻译,返回 key 本身
18+
},
19+
i18n: { changeLanguage: jest.fn() },
20+
}),
21+
Trans: ({ children }: { children: React.ReactNode }) => <span>{children}</span>, // Mock Trans 组件
22+
}))
523

624
jest.mock("../../../utils/vscode", () => ({
725
vscode: {

webview-ui/src/components/settings/SettingsView.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {
211211
onChange={(value: unknown) => {
212212
const lang = (value as DropdownOption).value
213213
i18n.changeLanguage(lang)
214+
vscode.postMessage({
215+
type: "preferredLanguage",
216+
text: lang,
217+
})
214218
}}
215219
style={{ width: "100%" }}
216220
options={[

webview-ui/src/components/settings/__tests__/ApiConfigManager.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import { render, screen, fireEvent, within } from "@testing-library/react"
22
import ApiConfigManager from "../ApiConfigManager"
3+
import en from "../../../i18n/locales/en.json"
4+
5+
// Mock react-i18next
6+
jest.mock("react-i18next", () => ({
7+
useTranslation: () => ({
8+
t: (key: string) => {
9+
// 通过点号分割 key 来支持嵌套的翻译
10+
const keys = key.split(".")
11+
let value: any = en
12+
for (const k of keys) {
13+
value = value?.[k]
14+
}
15+
return value || key // 如果找不到翻译,返回 key 本身
16+
},
17+
i18n: { changeLanguage: jest.fn() },
18+
}),
19+
Trans: ({ children }: { children: React.ReactNode }) => <span>{children}</span>, // Mock Trans 组件
20+
}))
321

422
// Mock VSCode components
523
jest.mock("@vscode/webview-ui-toolkit/react", () => ({

webview-ui/src/components/settings/__tests__/SettingsView.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { render, screen, fireEvent, waitFor } from "@testing-library/react"
22
import SettingsView from "../SettingsView"
33
import { ExtensionStateContextProvider } from "../../../context/ExtensionStateContext"
44
import { vscode } from "../../../utils/vscode"
5+
import en from "../../../i18n/locales/en.json"
56

67
// Mock vscode API
78
jest.mock("../../../utils/vscode", () => ({
@@ -10,6 +11,23 @@ jest.mock("../../../utils/vscode", () => ({
1011
},
1112
}))
1213

14+
// Mock react-i18next
15+
jest.mock("react-i18next", () => ({
16+
useTranslation: () => ({
17+
t: (key: string) => {
18+
// 通过点号分割 key 来支持嵌套的翻译
19+
const keys = key.split(".")
20+
let value: any = en
21+
for (const k of keys) {
22+
value = value?.[k]
23+
}
24+
return value || key // 如果找不到翻译,返回 key 本身
25+
},
26+
i18n: { changeLanguage: jest.fn() },
27+
}),
28+
Trans: ({ children }: { children: React.ReactNode }) => <span>{children}</span>, // Mock Trans 组件
29+
}))
30+
1331
// Mock ApiConfigManager component
1432
jest.mock("../ApiConfigManager", () => ({
1533
__esModule: true,

0 commit comments

Comments
 (0)