|
| 1 | +import * as React from "react"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import { KindInput } from "../KindInput"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | + |
| 6 | +describe(KindInput.name, () => { |
| 7 | + const onChange = jest.fn(); |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + onChange.mockReset(); |
| 11 | + }); |
| 12 | + |
| 13 | + it("allows changing the kind", async () => { |
| 14 | + render( |
| 15 | + <KindInput |
| 16 | + kinds={["local", "remote"]} |
| 17 | + value="local" |
| 18 | + onChange={onChange} |
| 19 | + />, |
| 20 | + ); |
| 21 | + |
| 22 | + expect(screen.getByRole("combobox")).toHaveValue("local"); |
| 23 | + await userEvent.selectOptions(screen.getByRole("combobox"), "remote"); |
| 24 | + expect(onChange).toHaveBeenCalledWith("remote"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("resets the kind when changing the supported kinds", () => { |
| 28 | + const { rerender } = render( |
| 29 | + <KindInput |
| 30 | + kinds={["local", "remote"]} |
| 31 | + value={"local"} |
| 32 | + onChange={onChange} |
| 33 | + />, |
| 34 | + ); |
| 35 | + |
| 36 | + expect(screen.getByRole("combobox")).toHaveValue("local"); |
| 37 | + expect(onChange).not.toHaveBeenCalled(); |
| 38 | + |
| 39 | + rerender( |
| 40 | + <KindInput |
| 41 | + kinds={["sql-injection", "log-injection", "url-redirection"]} |
| 42 | + value="local" |
| 43 | + onChange={onChange} |
| 44 | + />, |
| 45 | + ); |
| 46 | + expect(screen.getByRole("combobox")).toHaveValue("sql-injection"); |
| 47 | + expect(onChange).toHaveBeenCalledWith("sql-injection"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("sets the kind when value is undefined", () => { |
| 51 | + render( |
| 52 | + <KindInput |
| 53 | + kinds={["local", "remote"]} |
| 54 | + value={undefined} |
| 55 | + onChange={onChange} |
| 56 | + />, |
| 57 | + ); |
| 58 | + |
| 59 | + expect(screen.getByRole("combobox")).toHaveValue("local"); |
| 60 | + expect(onChange).toHaveBeenCalledWith("local"); |
| 61 | + }); |
| 62 | +}); |
0 commit comments