"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("@testing-library/react");
require("@testing-library/jest-dom");
const user_event_1 = __importDefault(require("@testing-library/user-event"));
const AttachButton_1 = require("./AttachButton");
describe('Attach button', () => {
    it('should render button correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, {}));
        expect(react_1.screen.getByRole('button', { name: 'Attach' })).toBeTruthy();
    });
    it('should handle isDisabled prop', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { isDisabled: true }));
        expect(react_1.screen.getByRole('button', { name: 'Attach' })).toBeDisabled();
    });
    it('should handle spread props, including aria-label', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { "aria-label": "test" }));
        expect(react_1.screen.getByRole('button', { name: 'test' }));
    });
    it('should handle onClick', () => __awaiter(void 0, void 0, void 0, function* () {
        const spy = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { onClick: spy }));
        yield user_event_1.default.click(react_1.screen.getByRole('button', { name: 'Attach' }));
        expect(react_1.screen.getByRole('tooltip', { name: 'Attach' })).toBeTruthy();
        expect(spy).toHaveBeenCalledTimes(1);
    }));
    it('should handle className prop', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { className: "test" }));
        expect(react_1.screen.getByRole('button', { name: 'Attach' })).toHaveClass('test');
    });
    it('should handle custom tooltip correctly', () => __awaiter(void 0, void 0, void 0, function* () {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { onClick: jest.fn, tooltipContent: "Test" }));
        yield user_event_1.default.click(react_1.screen.getByRole('button', { name: 'Attach' }));
        expect(react_1.screen.getByRole('tooltip', { name: 'Test' })).toBeTruthy();
    }));
    it('should handle tooltipProps prop', () => __awaiter(void 0, void 0, void 0, function* () {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { tooltipProps: { id: 'test' } }));
        yield user_event_1.default.click(react_1.screen.getByRole('button', { name: 'Attach' }));
        expect(react_1.screen.getByRole('tooltip', { name: 'Attach' })).toHaveAttribute('id', 'test');
    }));
    // Based on this because I had no idea how to do this and was looking around: https://stackoverflow.com/a/75562651
    // See also https://developer.mozilla.org/en-US/docs/Web/API/File/File for what that file variable is doing
    it('should handle onAttachAccepted prop', () => __awaiter(void 0, void 0, void 0, function* () {
        const spy = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { onAttachAccepted: spy, inputTestId: "input" }));
        yield user_event_1.default.click(react_1.screen.getByRole('button', { name: 'Attach' }));
        const file = new File(['test'], 'test.json');
        const input = react_1.screen.getByTestId('input');
        yield user_event_1.default.upload(input, file);
        expect(input.files).toHaveLength(1);
        expect(spy).toHaveBeenCalledTimes(1);
    }));
    it('should handle isCompact', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { isCompact: true, "data-testid": "button" }));
        expect(react_1.screen.getByTestId('button')).toHaveClass('pf-m-compact');
    });
    it('should set correct accept attribute on file input', () => __awaiter(void 0, void 0, void 0, function* () {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { inputTestId: "input", allowedFileTypes: { 'text/plain': ['.txt'] } }));
        yield user_event_1.default.click(react_1.screen.getByRole('button', { name: 'Attach' }));
        const input = react_1.screen.getByTestId('input');
        expect(input).toHaveAttribute('accept', 'text/plain,.txt');
    }));
    it('should call onAttachAccepted when file type is accepted', () => __awaiter(void 0, void 0, void 0, function* () {
        const onAttachAccepted = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { inputTestId: "input", allowedFileTypes: { 'text/plain': ['.txt'] }, onAttachAccepted: onAttachAccepted }));
        const file = new File(['hello'], 'example.txt', { type: 'text/plain' });
        const input = react_1.screen.getByTestId('input');
        yield user_event_1.default.upload(input, file);
        expect(onAttachAccepted).toHaveBeenCalled();
        const [attachedFile] = onAttachAccepted.mock.calls[0][0];
        expect(attachedFile).toEqual(file);
    }));
    it('should not call onAttachAccepted when file type is not accepted', () => __awaiter(void 0, void 0, void 0, function* () {
        const onAttachAccepted = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { inputTestId: "input", allowedFileTypes: { 'text/plain': ['.txt'] }, onAttachAccepted: onAttachAccepted }));
        const file = new File(['[]'], 'example.json', { type: 'application/json' });
        const input = react_1.screen.getByTestId('input');
        yield user_event_1.default.upload(input, file);
        expect(onAttachAccepted).not.toHaveBeenCalled();
    }));
    it('should respect minSize restriction', () => __awaiter(void 0, void 0, void 0, function* () {
        const onAttachRejected = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { inputTestId: "input", minSize: 1000, onAttachRejected: onAttachRejected }));
        const file = new File(['Test'], 'example.txt', { type: 'text/plain' });
        const input = react_1.screen.getByTestId('input');
        yield user_event_1.default.upload(input, file);
        expect(onAttachRejected).toHaveBeenCalled();
    }));
    it('should respect maxSize restriction', () => __awaiter(void 0, void 0, void 0, function* () {
        const onAttachRejected = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { inputTestId: "input", maxSize: 100, onAttachRejected: onAttachRejected }));
        const largeContent = 'x'.repeat(200);
        const file = new File([largeContent], 'example.txt', { type: 'text/plain' });
        const input = react_1.screen.getByTestId('input');
        yield user_event_1.default.upload(input, file);
        expect(onAttachRejected).toHaveBeenCalled();
    }));
    it('should respect maxFiles restriction', () => __awaiter(void 0, void 0, void 0, function* () {
        const onAttachRejected = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { inputTestId: "input", maxFiles: 1, onAttachRejected: onAttachRejected }));
        const files = [
            new File(['Test1'], 'example1.txt', { type: 'text/plain' }),
            new File(['Test2'], 'example2.txt', { type: 'text/plain' })
        ];
        const input = react_1.screen.getByTestId('input');
        yield user_event_1.default.upload(input, files);
        expect(onAttachRejected).toHaveBeenCalled();
    }));
    it('should be disabled when isAttachmentDisabled is true', () => __awaiter(void 0, void 0, void 0, function* () {
        const onFileDrop = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { inputTestId: "input", isAttachmentDisabled: true }));
        const file = new File(['Test'], 'example.text', { type: 'text/plain' });
        const input = react_1.screen.getByTestId('input');
        yield user_event_1.default.upload(input, file);
        expect(onFileDrop).not.toHaveBeenCalled();
    }));
    it('should call onAttach when files are attached', () => __awaiter(void 0, void 0, void 0, function* () {
        const onAttach = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { inputTestId: "input", onAttach: onAttach }));
        const file = new File(['Test'], 'example.txt', { type: 'text/plain' });
        const input = react_1.screen.getByTestId('input');
        yield user_event_1.default.upload(input, file);
        expect(onAttach).toHaveBeenCalled();
    }));
    it('should use custom validator when provided', () => __awaiter(void 0, void 0, void 0, function* () {
        const validator = jest.fn().mockReturnValue({ message: 'Custom error' });
        const onAttachRejected = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { inputTestId: "input", validator: validator, onAttachRejected: onAttachRejected }));
        const file = new File(['Test'], 'example.txt', { type: 'text/plain' });
        const input = react_1.screen.getByTestId('input');
        yield user_event_1.default.upload(input, file);
        expect(validator).toHaveBeenCalledWith(file);
        expect(onAttachRejected).toHaveBeenCalled();
    }));
    it('should handle icon prop', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(AttachButton_1.AttachButton, { icon: (0, jsx_runtime_1.jsx)("img", { alt: "", src: "" }) }));
        expect(react_1.screen.getByRole('img')).toBeVisible();
    });
});
