Skip to content

Commit 8acc275

Browse files
committed
tests
1 parent a7bf984 commit 8acc275

File tree

1 file changed

+114
-17
lines changed

1 file changed

+114
-17
lines changed

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

Lines changed: 114 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// npx jest src/components/settings/__tests__/ContextManagementSettings.test.ts
22

3+
import React from "react"
34
import { render, screen, fireEvent } from "@testing-library/react"
45

56
import { ContextManagementSettings } from "@src/components/settings/ContextManagementSettings"
@@ -12,16 +13,32 @@ class MockResizeObserver {
1213

1314
global.ResizeObserver = MockResizeObserver
1415

15-
jest.mock("@/components/ui", () => ({
16-
...jest.requireActual("@/components/ui"),
17-
Slider: ({ value, onValueChange, "data-testid": dataTestId }: any) => (
18-
<input
19-
type="range"
20-
value={value[0]}
21-
onChange={(e) => onValueChange([parseFloat(e.target.value)])}
22-
data-testid={dataTestId}
23-
/>
24-
),
16+
// Mock lucide-react icons - these don't work well in Jest/JSDOM environment
17+
jest.mock("lucide-react", () => {
18+
return {
19+
Database: React.forwardRef((props: any, ref: any) => <div ref={ref} data-testid="database-icon" {...props} />),
20+
ChevronDown: React.forwardRef((props: any, ref: any) => (
21+
<div ref={ref} data-testid="chevron-down-icon" {...props} />
22+
)),
23+
ChevronUp: React.forwardRef((props: any, ref: any) => (
24+
<div ref={ref} data-testid="chevron-up-icon" {...props} />
25+
)),
26+
Check: React.forwardRef((props: any, ref: any) => <div ref={ref} data-testid="check-icon" {...props} />),
27+
}
28+
})
29+
30+
// Mock translation hook to return the key as the translation
31+
jest.mock("@/i18n/TranslationContext", () => ({
32+
useAppTranslation: () => ({
33+
t: (key: string) => key,
34+
}),
35+
}))
36+
37+
// Mock vscode utilities - this is necessary since we're not in a VSCode environment
38+
jest.mock("@/utils/vscode", () => ({
39+
vscode: {
40+
postMessage: jest.fn(),
41+
},
2542
}))
2643

2744
describe("ContextManagementSettings", () => {
@@ -57,21 +74,41 @@ describe("ContextManagementSettings", () => {
5774
})
5875

5976
it("updates open tabs context limit", () => {
60-
render(<ContextManagementSettings {...defaultProps} />)
77+
const mockSetCachedStateField = jest.fn()
78+
const props = { ...defaultProps, setCachedStateField: mockSetCachedStateField }
79+
render(<ContextManagementSettings {...props} />)
6180

6281
const slider = screen.getByTestId("open-tabs-limit-slider")
63-
fireEvent.change(slider, { target: { value: "50" } })
82+
expect(slider).toBeInTheDocument()
6483

65-
expect(defaultProps.setCachedStateField).toHaveBeenCalledWith("maxOpenTabsContext", 50)
84+
// Check that the current value is displayed
85+
expect(screen.getByText("20")).toBeInTheDocument()
86+
87+
// Test slider interaction using keyboard events (ArrowRight increases value)
88+
slider.focus()
89+
fireEvent.keyDown(slider, { key: "ArrowRight" })
90+
91+
// The callback should have been called with the new value (20 + 1 = 21)
92+
expect(mockSetCachedStateField).toHaveBeenCalledWith("maxOpenTabsContext", 21)
6693
})
6794

68-
it("updates workspace files contextlimit", () => {
69-
render(<ContextManagementSettings {...defaultProps} />)
95+
it("updates workspace files limit", () => {
96+
const mockSetCachedStateField = jest.fn()
97+
const props = { ...defaultProps, setCachedStateField: mockSetCachedStateField }
98+
render(<ContextManagementSettings {...props} />)
7099

71100
const slider = screen.getByTestId("workspace-files-limit-slider")
72-
fireEvent.change(slider, { target: { value: "50" } })
101+
expect(slider).toBeInTheDocument()
73102

74-
expect(defaultProps.setCachedStateField).toHaveBeenCalledWith("maxWorkspaceFiles", 50)
103+
// Check that the current value is displayed
104+
expect(screen.getByText("200")).toBeInTheDocument()
105+
106+
// Test slider interaction using keyboard events (ArrowRight increases value)
107+
slider.focus()
108+
fireEvent.keyDown(slider, { key: "ArrowRight" })
109+
110+
// The callback should have been called with the new value (200 + 1 = 201)
111+
expect(mockSetCachedStateField).toHaveBeenCalledWith("maxWorkspaceFiles", 201)
75112
})
76113

77114
it("updates show rooignored files setting", () => {
@@ -82,4 +119,64 @@ describe("ContextManagementSettings", () => {
82119

83120
expect(defaultProps.setCachedStateField).toHaveBeenCalledWith("showRooIgnoredFiles", true)
84121
})
122+
123+
it("renders max read file line controls", () => {
124+
const propsWithMaxReadFileLine = {
125+
...defaultProps,
126+
maxReadFileLine: 500,
127+
}
128+
render(<ContextManagementSettings {...propsWithMaxReadFileLine} />)
129+
130+
// Max read file line input
131+
const maxReadFileInput = screen.getByTestId("max-read-file-line-input")
132+
expect(maxReadFileInput).toBeInTheDocument()
133+
expect(maxReadFileInput).toHaveValue(500)
134+
135+
// Always full read checkbox
136+
const alwaysFullReadCheckbox = screen.getByTestId("max-read-file-always-full-checkbox")
137+
expect(alwaysFullReadCheckbox).toBeInTheDocument()
138+
expect(alwaysFullReadCheckbox).not.toBeChecked()
139+
})
140+
141+
it("updates max read file line setting", () => {
142+
const propsWithMaxReadFileLine = {
143+
...defaultProps,
144+
maxReadFileLine: 500,
145+
}
146+
render(<ContextManagementSettings {...propsWithMaxReadFileLine} />)
147+
148+
const input = screen.getByTestId("max-read-file-line-input")
149+
fireEvent.change(input, { target: { value: "1000" } })
150+
151+
expect(defaultProps.setCachedStateField).toHaveBeenCalledWith("maxReadFileLine", 1000)
152+
})
153+
154+
it("toggles always full read setting", () => {
155+
const propsWithMaxReadFileLine = {
156+
...defaultProps,
157+
maxReadFileLine: 500,
158+
}
159+
render(<ContextManagementSettings {...propsWithMaxReadFileLine} />)
160+
161+
const checkbox = screen.getByTestId("max-read-file-always-full-checkbox")
162+
fireEvent.click(checkbox)
163+
164+
expect(defaultProps.setCachedStateField).toHaveBeenCalledWith("maxReadFileLine", -1)
165+
})
166+
167+
it("renders with autoCondenseContext enabled", () => {
168+
const propsWithAutoCondense = {
169+
...defaultProps,
170+
autoCondenseContext: true,
171+
autoCondenseContextPercent: 75,
172+
condensingApiConfigId: "test-config",
173+
customCondensingPrompt: "Test prompt",
174+
}
175+
render(<ContextManagementSettings {...propsWithAutoCondense} />)
176+
177+
// Should render the auto condense section
178+
expect(screen.getByText("settings:experimental.autoCondenseContextPercent.label")).toBeInTheDocument()
179+
expect(screen.getByText("settings:experimental.condensingApiConfiguration.label")).toBeInTheDocument()
180+
expect(screen.getByText("settings:experimental.customCondensingPrompt.label")).toBeInTheDocument()
181+
})
85182
})

0 commit comments

Comments
 (0)