|
| 1 | +import { render, fireEvent } from "@testing-library/react" |
| 2 | +import { RateLimitControl } from "../RateLimitControl" |
| 3 | + |
| 4 | +// Mock the translation hook |
| 5 | +jest.mock("@/i18n/TranslationContext", () => ({ |
| 6 | + useAppTranslation: () => ({ |
| 7 | + t: (key: string) => key, |
| 8 | + }), |
| 9 | +})) |
| 10 | + |
| 11 | +describe("RateLimitControl", () => { |
| 12 | + it("renders with the provided value", () => { |
| 13 | + const onChange = jest.fn() |
| 14 | + const { getByRole } = render(<RateLimitControl value={10} onChange={onChange} />) |
| 15 | + |
| 16 | + const slider = getByRole("slider") as HTMLInputElement |
| 17 | + expect(slider.value).toBe("10") |
| 18 | + }) |
| 19 | + |
| 20 | + it("calls onChange when the slider value changes", () => { |
| 21 | + const onChange = jest.fn() |
| 22 | + const { getByRole } = render(<RateLimitControl value={10} onChange={onChange} />) |
| 23 | + |
| 24 | + const slider = getByRole("slider") as HTMLInputElement |
| 25 | + fireEvent.change(slider, { target: { value: "20" } }) |
| 26 | + |
| 27 | + // The onChange is debounced, so we need to wait for it to be called |
| 28 | + setTimeout(() => { |
| 29 | + expect(onChange).toHaveBeenCalledWith(20) |
| 30 | + }, 100) |
| 31 | + }) |
| 32 | + |
| 33 | + it("updates the displayed value when the slider changes", () => { |
| 34 | + const onChange = jest.fn() |
| 35 | + const { getByRole, getByText } = render(<RateLimitControl value={10} onChange={onChange} />) |
| 36 | + |
| 37 | + const slider = getByRole("slider") as HTMLInputElement |
| 38 | + fireEvent.change(slider, { target: { value: "20" } }) |
| 39 | + |
| 40 | + expect(getByText("20s")).toBeInTheDocument() |
| 41 | + }) |
| 42 | + |
| 43 | + it("handles null or undefined values", () => { |
| 44 | + const onChange = jest.fn() |
| 45 | + const { getByRole } = render(<RateLimitControl value={null} onChange={onChange} />) |
| 46 | + |
| 47 | + const slider = getByRole("slider") as HTMLInputElement |
| 48 | + expect(slider.value).toBe("0") |
| 49 | + }) |
| 50 | +}) |
0 commit comments