Skip to content

Commit b0f72a2

Browse files
committed
fix: handle undefined state for Gemini URL context and grounding checkboxes
- Changed checkbox checked prop from !!value to value === true - This ensures undefined values are properly handled as unchecked - Fixes Save button not activating on first toggle for new installs - Applied fix to both Gemini and Vertex components - Added tests for undefined state handling Fixes #8245
1 parent 0e1b23d commit b0f72a2

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

webview-ui/src/components/settings/providers/Gemini.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const Gemini = ({ apiConfiguration, setApiConfigurationField, fromWelcome
7979
<Checkbox
8080
className="mt-6"
8181
data-testid="checkbox-url-context"
82-
checked={!!apiConfiguration.enableUrlContext}
82+
checked={apiConfiguration.enableUrlContext === true}
8383
onChange={(checked: boolean) => setApiConfigurationField("enableUrlContext", checked)}>
8484
{t("settings:providers.geminiParameters.urlContext.title")}
8585
</Checkbox>
@@ -89,7 +89,7 @@ export const Gemini = ({ apiConfiguration, setApiConfigurationField, fromWelcome
8989

9090
<Checkbox
9191
data-testid="checkbox-grounding-search"
92-
checked={!!apiConfiguration.enableGrounding}
92+
checked={apiConfiguration.enableGrounding === true}
9393
onChange={(checked: boolean) => setApiConfigurationField("enableGrounding", checked)}>
9494
{t("settings:providers.geminiParameters.groundingSearch.title")}
9595
</Checkbox>

webview-ui/src/components/settings/providers/Vertex.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const Vertex = ({ apiConfiguration, setApiConfigurationField, fromWelcome
9898
<div className="mt-6">
9999
<Checkbox
100100
data-testid="checkbox-url-context"
101-
checked={!!apiConfiguration.enableUrlContext}
101+
checked={apiConfiguration.enableUrlContext === true}
102102
onChange={(checked: boolean) => setApiConfigurationField("enableUrlContext", checked)}>
103103
{t("settings:providers.geminiParameters.urlContext.title")}
104104
</Checkbox>
@@ -108,7 +108,7 @@ export const Vertex = ({ apiConfiguration, setApiConfigurationField, fromWelcome
108108

109109
<Checkbox
110110
data-testid="checkbox-grounding-search"
111-
checked={!!apiConfiguration.enableGrounding}
111+
checked={apiConfiguration.enableGrounding === true}
112112
onChange={(checked: boolean) => setApiConfigurationField("enableGrounding", checked)}>
113113
{t("settings:providers.geminiParameters.groundingSearch.title")}
114114
</Checkbox>

webview-ui/src/components/settings/providers/__tests__/Gemini.spec.tsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ describe("Gemini", () => {
5656
expect(checkbox.checked).toBe(false)
5757
})
5858

59+
it("should render URL context checkbox unchecked when enableUrlContext is undefined", () => {
60+
const apiConfiguration: ProviderSettings = {
61+
geminiApiKey: "",
62+
// enableUrlContext is undefined
63+
}
64+
render(
65+
<Gemini apiConfiguration={apiConfiguration} setApiConfigurationField={mockSetApiConfigurationField} />,
66+
)
67+
68+
const urlContextCheckbox = screen.getByTestId("checkbox-url-context")
69+
const checkbox = urlContextCheckbox.querySelector("input[type='checkbox']") as HTMLInputElement
70+
expect(checkbox.checked).toBe(false)
71+
})
72+
5973
it("should render URL context checkbox checked when enableUrlContext is true", () => {
6074
const apiConfiguration = { ...defaultApiConfiguration, enableUrlContext: true }
6175
render(
@@ -83,6 +97,24 @@ describe("Gemini", () => {
8397

8498
expect(mockSetApiConfigurationField).toHaveBeenCalledWith("enableUrlContext", true)
8599
})
100+
101+
it("should call setApiConfigurationField when toggled from undefined state", async () => {
102+
const user = userEvent.setup()
103+
const apiConfiguration: ProviderSettings = {
104+
geminiApiKey: "",
105+
// enableUrlContext is undefined
106+
}
107+
render(
108+
<Gemini apiConfiguration={apiConfiguration} setApiConfigurationField={mockSetApiConfigurationField} />,
109+
)
110+
111+
const urlContextCheckbox = screen.getByTestId("checkbox-url-context")
112+
const checkbox = urlContextCheckbox.querySelector("input[type='checkbox']") as HTMLInputElement
113+
114+
await user.click(checkbox)
115+
116+
expect(mockSetApiConfigurationField).toHaveBeenCalledWith("enableUrlContext", true)
117+
})
86118
})
87119

88120
describe("Grounding with Google Search Checkbox", () => {
@@ -99,6 +131,20 @@ describe("Gemini", () => {
99131
expect(checkbox.checked).toBe(false)
100132
})
101133

134+
it("should render grounding search checkbox unchecked when enableGrounding is undefined", () => {
135+
const apiConfiguration: ProviderSettings = {
136+
geminiApiKey: "",
137+
// enableGrounding is undefined
138+
}
139+
render(
140+
<Gemini apiConfiguration={apiConfiguration} setApiConfigurationField={mockSetApiConfigurationField} />,
141+
)
142+
143+
const groundingCheckbox = screen.getByTestId("checkbox-grounding-search")
144+
const checkbox = groundingCheckbox.querySelector("input[type='checkbox']") as HTMLInputElement
145+
expect(checkbox.checked).toBe(false)
146+
})
147+
102148
it("should render grounding search checkbox checked when enableGrounding is true", () => {
103149
const apiConfiguration = { ...defaultApiConfiguration, enableGrounding: true }
104150
render(
@@ -126,6 +172,24 @@ describe("Gemini", () => {
126172

127173
expect(mockSetApiConfigurationField).toHaveBeenCalledWith("enableGrounding", true)
128174
})
175+
176+
it("should call setApiConfigurationField when toggled from undefined state", async () => {
177+
const user = userEvent.setup()
178+
const apiConfiguration: ProviderSettings = {
179+
geminiApiKey: "",
180+
// enableGrounding is undefined
181+
}
182+
render(
183+
<Gemini apiConfiguration={apiConfiguration} setApiConfigurationField={mockSetApiConfigurationField} />,
184+
)
185+
186+
const groundingCheckbox = screen.getByTestId("checkbox-grounding-search")
187+
const checkbox = groundingCheckbox.querySelector("input[type='checkbox']") as HTMLInputElement
188+
189+
await user.click(checkbox)
190+
191+
expect(mockSetApiConfigurationField).toHaveBeenCalledWith("enableGrounding", true)
192+
})
129193
})
130194

131195
describe("fromWelcomeView prop", () => {

0 commit comments

Comments
 (0)