Skip to content

Commit 93ecb9e

Browse files
committed
test: Adding a unit test to test the MaxContextWindowControl component
1 parent d448d81 commit 93ecb9e

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const MaxContextWindowControl = ({ value, onChange, maxValue = 1000000 }:
5252
<div>
5353
<div className="flex items-center gap-2">
5454
<Slider
55-
min={200000}
55+
min={32000}
5656
max={maxValue}
5757
step={1}
5858
value={[inputValue ?? 1048576]}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { render, screen, fireEvent } from "@testing-library/react"
2+
import { MaxContextWindowControl } from "../MaxContextWindowControl"
3+
import "@testing-library/jest-dom"
4+
5+
class MockResizeObserver {
6+
observe() {}
7+
unobserve() {}
8+
disconnect() {}
9+
}
10+
global.ResizeObserver = MockResizeObserver
11+
12+
jest.mock("@/components/ui", () => ({
13+
...jest.requireActual("@/components/ui"),
14+
Slider: ({ value, onValueChange, min = 0, max = 100, "data-testid": dataTestId }: any) => (
15+
<input
16+
type="range"
17+
min={min}
18+
max={max}
19+
value={value[0]}
20+
onChange={(e) => onValueChange([parseInt(e.target.value, 10)])}
21+
data-testid={dataTestId}
22+
/>
23+
),
24+
}))
25+
26+
describe("MaxContextWindowControl", () => {
27+
it("updates when checkbox is toggled", async () => {
28+
const onChange = jest.fn()
29+
render(<MaxContextWindowControl value={123} onChange={onChange} />)
30+
31+
const checkbox = screen.getByRole("checkbox") as HTMLInputElement
32+
fireEvent.click(checkbox)
33+
34+
await new Promise((r) => setTimeout(r, 100))
35+
expect(onChange).toHaveBeenCalledWith(null)
36+
37+
fireEvent.click(checkbox)
38+
39+
await new Promise((r) => setTimeout(r, 100))
40+
expect(onChange).toHaveBeenCalledWith(123)
41+
})
42+
43+
it("calls onChange when slider is moved", async () => {
44+
const onChange = jest.fn()
45+
render(<MaxContextWindowControl value={35000} onChange={onChange} />)
46+
47+
const checkbox = screen.getByRole("checkbox") as HTMLInputElement
48+
expect(checkbox).toBeChecked()
49+
50+
const slider = screen.getByRole("slider")
51+
fireEvent.change(slider, { target: { value: "50000" } })
52+
53+
await new Promise((r) => setTimeout(r, 120))
54+
55+
expect(onChange).toHaveBeenCalledWith(50000)
56+
})
57+
})

0 commit comments

Comments
 (0)