|
| 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