/* * Copyright 2017 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, expect, it, vi } from "@blueprintjs/test-commons/vitest"; import { Classes } from "../../common"; import { FileInput } from "./fileInput"; describe("", () => { it(`supports className, fill, & size="large"`, () => { const CUSTOM_CLASS = "foo"; render(); const input = screen.getByLabelText("Choose file..."); const label = input.closest("label"); expect(label).toBeInTheDocument(); expect(label).toHaveClass(Classes.FILE_INPUT); expect(label).toHaveClass(CUSTOM_CLASS); expect(label).toHaveClass(Classes.FILL); expect(label).toHaveClass(Classes.LARGE); }); it("supports custom input props", () => { render( , ); const input = screen.getByLabelText("Choose file..."); expect(input).toHaveClass("bar"); expect(input).toBeRequired(); expect(input).toHaveAttribute("type", "file"); }); describe("applies top-level disabled prop to the root and input (overriding inputProps.disabled)", () => { it("disabled=true overrides inputProps.disabled=false", () => { render(); const input = screen.getByLabelText("Choose file..."); const label = input.closest("label"); expect(label).toBeInTheDocument(); expect(label).toHaveClass(Classes.DISABLED); expect(input).toBeDisabled(); }); it("disabled=false does not override inputProps.disabled=true", () => { render(); const input = screen.getByLabelText("Choose file..."); const label = input.closest("label"); expect(label).toBeInTheDocument(); expect(label).not.toHaveClass(Classes.DISABLED); expect(input).not.toBeDisabled(); }); }); it("renders custom text", () => { render(); expect(screen.getByLabelText("Input file...")).toBeInTheDocument(); }); it("invokes change callbacks", async () => { const user = userEvent.setup(); const inputPropsOnChange = vi.fn(); const onInputChange = vi.fn(); render(); const input = screen.getByLabelText("Choose file..."); const file = new File(["test"], "test.png", { type: "image/png" }); await user.upload(input, file); expect(onInputChange).toHaveBeenCalledOnce(); expect(inputPropsOnChange).toHaveBeenCalledOnce(); }); });