"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const faker_1 = __importDefault(require("faker"));
const test_utilities_1 = require("../../test-utilities");
const TextField_1 = require("../TextField");
const Labelled_1 = require("../Labelled");
const Autocomplete_1 = require("./Autocomplete");
jest.mock('../../utilities/focus', () => ({
    isFocused: jest.fn(),
}));
const { isFocused } = jest.requireMock('../../utilities/focus');
const defaultProps = {
    id: 'address',
    label: 'Address',
    name: 'address',
    options: ['address 1', 'address 2'],
    value: '',
    title: 'Suggestions',
    ariaLabel: 'Close Predictions',
};
const autocompleteOptions = (options) => {
    return (<Autocomplete_1.AutocompleteOptions>
      {options &&
        options.map((option) => (<Autocomplete_1.AutocompleteOption key={option}>{option}</Autocomplete_1.AutocompleteOption>))}
    </Autocomplete_1.AutocompleteOptions>);
};
describe('<Autocomplete />', () => {
    beforeEach(() => {
        isFocused.mockReset();
        isFocused.mockImplementation(() => true);
    });
    describe('<Labelled />', () => {
        it('renders a <Labelled />', () => {
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps} options={[]}/>);
            expect(autocomplete).toContainReactComponent(Labelled_1.Labelled);
        });
    });
    describe('<Field />', () => {
        it('renders a <Field />', () => {
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps} options={[]}/>);
            expect(autocomplete).toContainReactComponent(TextField_1.Field, {
                id: 'address',
            });
        });
        it('renders a disabled <Field /> when the disabled prop is true', () => {
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps} disabled options={[]}/>);
            expect(autocomplete).toContainReactComponent(TextField_1.Field, {
                disabled: true,
            });
        });
        it('renders a readonly <Field /> when the readonly prop is true', () => {
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps} readonly options={[]}/>);
            expect(autocomplete).toContainReactComponent(TextField_1.Field, {
                readonly: true,
            });
        });
        it('sets the default aria-controls and aria-expanded properties', () => {
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps} options={[]}/>);
            expect(autocomplete).toContainReactComponent(TextField_1.Field, {
                id: 'address',
                ariaControls: 'address-options',
                ariaExpanded: false,
            });
        });
        it('sets the aria-controls, aria-activedescendant and aria-expanded when the input is focused', () => {
            var _a;
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps}/>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
            expect(autocomplete).toContainReactComponent(TextField_1.Field, {
                id: 'address',
                ariaControls: 'address-options',
                ariaActiveDescendant: 'address-option-0',
                ariaExpanded: true,
            });
        });
        it('calls the onChange callback when the Field onChange is called', () => {
            var _a;
            const onChangeSpy = jest.fn();
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps} onChange={onChangeSpy}/>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onChange', 'address');
            expect(onChangeSpy.mock.calls).toMatchObject([['address']]);
            expect(onChangeSpy).toHaveBeenCalledTimes(1);
        });
        it('calls the onChange callback when the enter key is pressed and AutocompleteOptions is closed', () => {
            const onChangeSpy = jest.fn();
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps} onChange={onChangeSpy}/>);
            const value = faker_1.default.random.alphaNumeric();
            autocomplete
                .find('input')
                .trigger('oninput', { currentTarget: { value } });
            autocomplete.find('input').trigger('onKeyDown', { key: 'Escape' });
            autocomplete
                .find('input')
                .trigger('onKeyDown', { key: 'Enter', currentTarget: { value } });
            expect(onChangeSpy).toHaveBeenCalledWith(value);
        });
    });
    describe('<AutocompleteOptions />', () => {
        it('renders when the input is focused', () => {
            var _a;
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps}>
          {autocompleteOptions(defaultProps.options)}
        </Autocomplete_1.Autocomplete>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
            expect(autocomplete).toContainReactComponent(Autocomplete_1.AutocompleteOptions, {
                children: expect.any(Array),
            });
        });
        it("doesn't render when no options", () => {
            var _a;
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps} options={[]}>
          {autocompleteOptions(defaultProps.options)}
        </Autocomplete_1.Autocomplete>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
            expect(autocomplete).not.toContainReactComponent(Autocomplete_1.AutocompleteOptions);
        });
        it('renders when the options change', () => {
            var _a;
            function Options() {
                const [options, setOptions] = react_1.useState([]);
                const search = () => {
                    setOptions(['address 1', 'address 2']);
                };
                return (<>
            <Autocomplete_1.Autocomplete {...defaultProps} options={options}>
              {autocompleteOptions(defaultProps.options)}
            </Autocomplete_1.Autocomplete>
            <button onClick={search}/>
          </>);
            }
            const options = test_utilities_1.mountWithContext(<Options />);
            expect(options).not.toContainReactComponent(Autocomplete_1.AutocompleteOptions);
            (_a = options.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onClick');
            expect(options).toContainReactComponent(Autocomplete_1.AutocompleteOptions, {
                children: expect.any(Array),
            });
        });
        it("doesn't render when the value is empty", () => {
            var _a;
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps}>
          {autocompleteOptions(defaultProps.options)}
        </Autocomplete_1.Autocomplete>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onInput', '');
            expect(autocomplete).not.toContainReactComponent(Autocomplete_1.AutocompleteOptions);
        });
        it('renders an <ul> with unique id', () => {
            var _a;
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps}>
          {autocompleteOptions(defaultProps.options)}
        </Autocomplete_1.Autocomplete>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
            expect(autocomplete).toContainReactComponent('ul', {
                id: 'address-options',
            });
        });
        it('hides when the close button is clicked', () => {
            var _a, _b;
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps}>
          {autocompleteOptions(defaultProps.options)}
        </Autocomplete_1.Autocomplete>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
            (_b = autocomplete.find('button')) === null || _b === void 0 ? void 0 : _b.trigger('onClick');
            expect(autocomplete).not.toContainReactComponent(Autocomplete_1.AutocompleteOptions);
        });
    });
    describe('<AutocompleteOption />', () => {
        it('selects the first option when focus is on field', () => {
            var _a;
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps}>
          {autocompleteOptions(defaultProps.options)}
        </Autocomplete_1.Autocomplete>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
            const [firstOption, secondOption] = autocomplete.findAll(Autocomplete_1.AutocompleteOption);
            expect(firstOption.prop('isSelected')).toStrictEqual(true);
            expect(secondOption.prop('isSelected')).toStrictEqual(false);
        });
        it('renders a <li> with unique id', () => {
            var _a;
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps}>
          {autocompleteOptions(defaultProps.options)}
        </Autocomplete_1.Autocomplete>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
            const [firstOption, secondOption] = autocomplete.findAll(Autocomplete_1.AutocompleteOption);
            expect(firstOption).toContainReactComponent('li', {
                id: 'address-option-0',
            });
            expect(secondOption).toContainReactComponent('li', {
                id: 'address-option-1',
            });
        });
    });
    describe('<AutocompleteFooter />', () => {
        it('renders when the input is focused', () => {
            var _a;
            const autocomplete = test_utilities_1.mountWithContext(<Autocomplete_1.Autocomplete {...defaultProps}>
          <Autocomplete_1.AutocompleteFooter>This is a test footer</Autocomplete_1.AutocompleteFooter>;
        </Autocomplete_1.Autocomplete>);
            (_a = autocomplete.find(TextField_1.Field)) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
            expect(autocomplete).toContainReactComponent(Autocomplete_1.AutocompleteFooter, {
                children: 'This is a test footer',
            });
        });
    });
});
