Skip to content

Commit 6745500

Browse files
committed
fix(terminal): correct data-testid placement, use nullish coalescing, and harden test patterns (Zoo-Code-Org#119)
- TerminalSettings.tsx: move data-testid from Radix <Select> root (no DOM node) to <SelectTrigger> button so the attribute survives in production; replace || with ?? for the terminalProfile sentinel fallback so empty string is not incorrectly replaced; export DEFAULT_PROFILE_VALUE for reuse in tests. - TerminalSettings.profile.spec.tsx: import DEFAULT_PROFILE_VALUE, replace hardcoded sentinel strings; adopt rerender() over cleanup()+render() so onTerminalProfilePickerOpened is preserved across re-renders; remove unused cleanup import. - Remove PR screenshots from docs/ (after1.png, after2.png, before.png).
1 parent 301b6ac commit 6745500

5 files changed

Lines changed: 16 additions & 18 deletions

File tree

‎docs/after1.png‎

-104 KB
Binary file not shown.

‎docs/after2.png‎

-93.8 KB
Binary file not shown.

‎docs/before.png‎

-77.5 KB
Binary file not shown.

‎webview-ui/src/components/settings/TerminalSettings.tsx‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type TerminalSettingsProps = HTMLAttributes<HTMLDivElement> & {
4545

4646
// Sentinel value that maps to `undefined` (use VS Code's default shell).
4747
// The Select component cannot accept empty-string item values.
48-
const DEFAULT_PROFILE_VALUE = "__zoo_code_follow_vscode_sentinel__"
48+
export const DEFAULT_PROFILE_VALUE = "__zoo_code_follow_vscode_sentinel__"
4949

5050
export const TerminalSettings = ({
5151
terminalOutputPreviewSize,
@@ -203,15 +203,14 @@ export const TerminalSettings = ({
203203
</label>
204204

205205
<Select
206-
value={terminalProfile || DEFAULT_PROFILE_VALUE}
206+
value={terminalProfile ?? DEFAULT_PROFILE_VALUE}
207207
onValueChange={(value) =>
208208
setCachedStateField(
209209
"terminalProfile",
210210
value === DEFAULT_PROFILE_VALUE ? undefined : value,
211211
)
212-
}
213-
data-testid="terminal-profile-dropdown">
214-
<SelectTrigger className="w-full">
212+
}>
213+
<SelectTrigger className="w-full" data-testid="terminal-profile-dropdown">
215214
<SelectValue placeholder={t("settings:common.select")} />
216215
</SelectTrigger>
217216
<SelectContent>

‎webview-ui/src/components/settings/__tests__/TerminalSettings.profile.spec.tsx‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import * as React from "react"
44

5-
import { render, screen, fireEvent, act, cleanup } from "@/utils/test-utils"
5+
import { render, screen, fireEvent, act } from "@/utils/test-utils"
66

7-
import { TerminalSettings } from "../TerminalSettings"
7+
import { TerminalSettings, DEFAULT_PROFILE_VALUE } from "../TerminalSettings"
88

99
// Mock translation hook to echo keys
1010
vi.mock("@/i18n/TranslationContext", () => ({
@@ -56,16 +56,16 @@ function renderSelectChildren(children: any, onValueChange: (value: string) => v
5656
return React.Children.map(children, (child: any) => {
5757
if (!child || typeof child !== "object") return child
5858
const itemValue = child.props?.value ?? child.props?.["data-item-value"]
59-
if (child.props?.children && itemValue === undefined) {
60-
return renderSelectChildren(child.props.children, onValueChange)
61-
}
6259
if (itemValue !== undefined) {
6360
return (
6461
<button data-testid={`option-${itemValue}`} onClick={() => onValueChange(itemValue)}>
6562
{child.props.children}
6663
</button>
6764
)
6865
}
66+
if (child.props?.children) {
67+
return React.cloneElement(child, {}, renderSelectChildren(child.props.children, onValueChange))
68+
}
6969
return child
7070
})
7171
}
@@ -78,15 +78,15 @@ describe("TerminalSettings unified profile dropdown", () => {
7878
const setup = (terminalProfile?: string) => {
7979
const setCachedStateField = vi.fn()
8080
const onTerminalProfilePickerOpened = vi.fn()
81-
render(
81+
const { rerender } = render(
8282
<TerminalSettings
8383
terminalShellIntegrationDisabled={false}
8484
terminalProfile={terminalProfile}
8585
onTerminalProfilePickerOpened={onTerminalProfilePickerOpened}
8686
setCachedStateField={setCachedStateField}
8787
/>,
8888
)
89-
return { onTerminalProfilePickerOpened, setCachedStateField }
89+
return { onTerminalProfilePickerOpened, setCachedStateField, rerender }
9090
}
9191

9292
it("requests the terminal profile names on mount via the allowlisted message", () => {
@@ -99,13 +99,13 @@ describe("TerminalSettings unified profile dropdown", () => {
9999
setup()
100100
const dropdown = screen.getByTestId("terminal-profile-dropdown")
101101
expect(dropdown).toBeInTheDocument()
102-
expect(dropdown.getAttribute("data-value")).toBe("__zoo_code_follow_vscode_sentinel__")
102+
expect(dropdown.closest('[data-testid="select"]')?.getAttribute("data-value")).toBe(DEFAULT_PROFILE_VALUE)
103103
// Configure button visible because "Following VS Code profile" is selected
104104
expect(screen.getByTestId("terminal-profile-configure-button")).toBeInTheDocument()
105105
})
106106

107107
it("selects a specific profile and hides the Configure button", () => {
108-
const { setCachedStateField } = setup()
108+
const { setCachedStateField, onTerminalProfilePickerOpened, rerender } = setup()
109109

110110
act(() => {
111111
window.dispatchEvent(
@@ -119,12 +119,11 @@ describe("TerminalSettings unified profile dropdown", () => {
119119
fireEvent.click(screen.getByTestId("option-Git Bash"))
120120
expect(setCachedStateField).toHaveBeenCalledWith("terminalProfile", "Git Bash")
121121

122-
// Re-render with profile selected (cleanup first to avoid stale DOM from first render)
123-
cleanup()
124-
render(
122+
rerender(
125123
<TerminalSettings
126124
terminalShellIntegrationDisabled={false}
127125
terminalProfile="Git Bash"
126+
onTerminalProfilePickerOpened={onTerminalProfilePickerOpened}
128127
setCachedStateField={setCachedStateField}
129128
/>,
130129
)
@@ -144,7 +143,7 @@ describe("TerminalSettings unified profile dropdown", () => {
144143
)
145144
})
146145

147-
fireEvent.click(screen.getByTestId("option-__zoo_code_follow_vscode_sentinel__"))
146+
fireEvent.click(screen.getByTestId(`option-${DEFAULT_PROFILE_VALUE}`))
148147
expect(setCachedStateField).toHaveBeenCalledWith("terminalProfile", undefined)
149148
})
150149

0 commit comments

Comments
 (0)