var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import React from 'react';
import { debounce } from '@reltio/mdm-sdk';
import { render, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { DependentLookupEditor } from './DependentLookupEditor';
import { DependentLookupAutopopulationContext } from '../contexts/DependentLookupAutopopulationContext';
jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { debounce: jest.fn() })); });
var allLookups = {
    AR: { displayName: 'Argentina' },
    BG: { displayName: 'Bulgaria' },
    CN: { displayName: 'China' },
    PT: { displayName: 'Portugal' }
};
var defaultProps = {
    value: { value: 'Portugal', lookupCode: 'PT' },
    lookups: { COUNTRY_CD: { PT: { displayName: 'Portugal' } } },
    lookupCode: 'COUNTRY_CD',
    error: false,
    parents: [],
    disabled: false,
    placeholder: '',
    TextFieldProps: {},
    onChange: jest.fn(),
    resolveLookups: jest.fn(),
    getLookups: jest.fn().mockResolvedValue(allLookups)
};
var setUp = function (_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.props, props = _c === void 0 ? defaultProps : _c, _d = _b.CustomProviders, CustomProviders = _d === void 0 ? React.Fragment : _d;
    var user = userEvent.setup();
    var Providers = function (_a) {
        var children = _a.children;
        return React.createElement(CustomProviders, null, children);
    };
    var result = render(React.createElement(DependentLookupEditor, __assign({}, props)), { wrapper: Providers });
    var rerender = function (newProps) { return result.rerender(React.createElement(DependentLookupEditor, __assign({}, props, newProps))); };
    return __assign(__assign({ user: user }, result), { rerender: rerender });
};
describe('Dependent lookup editor tests', function () {
    beforeEach(function () {
        debounce.mockImplementation(function (val) { return val; });
    });
    afterEach(function () {
        jest.clearAllMocks();
    });
    it('should render correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user, options;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp().user;
                    expect(screen.getByRole('combobox')).toHaveValue('Portugal (PT)');
                    expect(defaultProps.getLookups).toHaveBeenCalledWith({
                        type: defaultProps.lookupCode,
                        parents: defaultProps.parents,
                        displayNamePrefix: '',
                        max: 51,
                        offset: 0
                    });
                    return [4 /*yield*/, user.click(screen.getByRole('combobox'))];
                case 1:
                    _a.sent();
                    options = screen.getAllByRole('option');
                    expect(options).toHaveLength(4);
                    expect(options[0]).toHaveTextContent('Argentina (AR)');
                    expect(options[1]).toHaveTextContent('Bulgaria (BG)');
                    expect(options[2]).toHaveTextContent('China (CN)');
                    expect(options[3]).toHaveTextContent('Portugal (PT)');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should render correctly in multiple mode', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, props;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    value = [
                        { value: 'Portugal', lookupCode: 'PT' },
                        { lookupCode: 'CN', value: 'China' }
                    ];
                    props = __assign(__assign({}, defaultProps), { value: value, multiple: true });
                    setUp({ props: props });
                    return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, Promise.resolve()];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(screen.getByRole('combobox')).toHaveValue('');
                    screen.getByText('Portugal (PT)');
                    screen.getByText('China (CN)');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should show input value when select is disabled', function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    setUp({ props: __assign(__assign({}, defaultProps), { disabled: true }) });
                    return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, Promise.resolve()];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(screen.getByRole('combobox')).toHaveValue('Portugal (PT)');
                    expect(screen.getByRole('combobox')).toHaveAttribute('disabled');
                    expect(screen.getByRole('combobox')).toBeVisible();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should reset inputValue and options on input blur in multiple mode', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, props, _a, user, container, input;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    value = [
                        { value: 'Portugal', lookupCode: 'PT' },
                        { lookupCode: 'CN', value: 'China' }
                    ];
                    props = __assign(__assign({}, defaultProps), { value: value, multiple: true });
                    _a = setUp({ props: props }), user = _a.user, container = _a.container;
                    input = screen.getByRole('combobox');
                    return [4 /*yield*/, user.type(input, 'u')];
                case 1:
                    _b.sent();
                    expect(input).toHaveValue('u');
                    expect(props.onChange).not.toHaveBeenCalled();
                    expect(props.getLookups).toHaveBeenCalledWith({
                        type: 'COUNTRY_CD',
                        parents: [],
                        displayNamePrefix: 'u',
                        max: 51,
                        offset: 0
                    });
                    return [4 /*yield*/, user.click(container)];
                case 2:
                    _b.sent();
                    expect(input).toHaveValue('');
                    expect(props.onChange).not.toHaveBeenCalled();
                    expect(props.getLookups).toHaveBeenCalledWith({
                        type: 'COUNTRY_CD',
                        parents: [],
                        displayNamePrefix: '',
                        max: 51,
                        offset: 0
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should not reset inputValue on menu-close in multiple mode', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, props, user, input;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    value = [
                        { value: 'Portugal', lookupCode: 'PT' },
                        { lookupCode: 'CN', value: 'China' }
                    ];
                    props = __assign(__assign({}, defaultProps), { value: value, multiple: true });
                    user = setUp({ props: props }).user;
                    input = screen.getByRole('combobox');
                    return [4 /*yield*/, user.type(input, 'u')];
                case 1:
                    _a.sent();
                    expect(input).toHaveValue('u');
                    expect(props.onChange).not.toHaveBeenCalled();
                    return [4 /*yield*/, user.keyboard('{Escape}')];
                case 2:
                    _a.sent();
                    expect(input).toHaveValue('u');
                    expect(props.onChange).not.toHaveBeenCalled();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should set inputValue as value property on input blur, if inputValue is not empty', function () { return __awaiter(void 0, void 0, void 0, function () {
        var _a, user, container, input;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    _a = setUp(), user = _a.user, container = _a.container;
                    input = screen.getByRole('combobox');
                    return [4 /*yield*/, user.type(input, 'u')];
                case 1:
                    _b.sent();
                    expect(input).toHaveValue('Portugal (PT)u');
                    expect(defaultProps.onChange).not.toHaveBeenCalled();
                    expect(defaultProps.getLookups).toHaveBeenCalledWith({
                        type: 'COUNTRY_CD',
                        parents: [],
                        displayNamePrefix: 'Portugal (PT)u',
                        max: 51,
                        offset: 0
                    });
                    return [4 /*yield*/, user.click(container)];
                case 2:
                    _b.sent();
                    expect(input).toHaveValue('Portugal (PT)');
                    expect(defaultProps.onChange).not.toHaveBeenCalled();
                    expect(defaultProps.getLookups).toHaveBeenLastCalledWith({
                        type: 'COUNTRY_CD',
                        parents: [],
                        displayNamePrefix: '',
                        max: 51,
                        offset: 0
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should set inputValue as value property on menu close, if inputValue is not empty', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user, input;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp().user;
                    input = screen.getByRole('combobox');
                    return [4 /*yield*/, user.type(input, 'u')];
                case 1:
                    _a.sent();
                    expect(input).toHaveValue('Portugal (PT)u');
                    expect(defaultProps.onChange).not.toHaveBeenCalled();
                    expect(defaultProps.getLookups).toHaveBeenCalledWith({
                        type: 'COUNTRY_CD',
                        parents: [],
                        displayNamePrefix: 'Portugal (PT)u',
                        max: 51,
                        offset: 0
                    });
                    return [4 /*yield*/, user.keyboard('{Escape}')];
                case 2:
                    _a.sent();
                    expect(input).toHaveValue('Portugal (PT)');
                    expect(defaultProps.onChange).not.toHaveBeenCalled();
                    expect(defaultProps.getLookups).toHaveBeenLastCalledWith({
                        type: 'COUNTRY_CD',
                        parents: [],
                        displayNamePrefix: '',
                        max: 51,
                        offset: 0
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should not render menu list footer if options number is less or equal to max value', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { max: 2, getLookups: jest.fn().mockResolvedValue({ CN: { displayName: 'China' }, PT: { displayName: 'Portugal' } }) });
                    user = setUp({ props: props }).user;
                    return [4 /*yield*/, user.click(screen.getByRole('combobox'))];
                case 1:
                    _a.sent();
                    expect(screen.queryByText('Load more')).not.toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should render menu list footer with load more button if there are more options available', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { max: 2 });
                    user = setUp({ props: props }).user;
                    return [4 /*yield*/, user.click(screen.getByRole('combobox'))];
                case 1:
                    _a.sent();
                    screen.getByText('Load more');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should load next options page on load more button click, button should exist if not all options are loaded', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, user, options;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { max: 2, getLookups: jest.fn().mockResolvedValue(allLookups) });
                    user = setUp({ props: props }).user;
                    return [4 /*yield*/, user.click(screen.getByRole('combobox'))];
                case 1:
                    _a.sent();
                    options = screen.getAllByRole('option');
                    expect(options).toHaveLength(2);
                    expect(options[0]).toHaveTextContent('Argentina (AR)');
                    expect(options[1]).toHaveTextContent('Bulgaria (BG)');
                    props.getLookups.mockResolvedValue({
                        RU: { displayName: 'Russia' },
                        USA: { displayName: 'USA' },
                        MEX: { displayName: 'MEXICO' }
                    });
                    return [4 /*yield*/, user.click(screen.getByText('Load more'))];
                case 2:
                    _a.sent();
                    expect(props.getLookups).toHaveBeenCalledWith({
                        type: props.lookupCode,
                        parents: props.parents,
                        displayNamePrefix: '',
                        max: 3,
                        offset: 2
                    });
                    options = screen.getAllByRole('option');
                    expect(options).toHaveLength(4);
                    expect(options[0]).toHaveTextContent('Argentina (AR)');
                    expect(options[1]).toHaveTextContent('Bulgaria (BG)');
                    expect(options[2]).toHaveTextContent('MEXICO (MEX)');
                    expect(options[3]).toHaveTextContent('Russia (RU)');
                    screen.getByText('Load more');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should load next options page on load more button click, load more button disappears if all options loaded', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, user, options;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { max: 2, getLookups: jest.fn().mockResolvedValue(allLookups) });
                    user = setUp({ props: props }).user;
                    return [4 /*yield*/, user.click(screen.getByRole('combobox'))];
                case 1:
                    _a.sent();
                    options = screen.getAllByRole('option');
                    expect(options).toHaveLength(2);
                    expect(options[0]).toHaveTextContent('Argentina (AR)');
                    expect(options[1]).toHaveTextContent('Bulgaria (BG)');
                    props.getLookups.mockResolvedValue({ RU: { displayName: 'Russia' }, USA: { displayName: 'USA' } });
                    return [4 /*yield*/, user.click(screen.getByText('Load more'))];
                case 2:
                    _a.sent();
                    options = screen.getAllByRole('option');
                    expect(options).toHaveLength(4);
                    expect(options[0]).toHaveTextContent('Argentina (AR)');
                    expect(options[1]).toHaveTextContent('Bulgaria (BG)');
                    expect(options[2]).toHaveTextContent('Russia (RU)');
                    expect(options[3]).toHaveTextContent('USA');
                    expect(screen.queryByText('Load more')).not.toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should request default options on props.lookupCode change', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, _a, user, rerender, options;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { getLookups: jest.fn().mockResolvedValue(allLookups) });
                    _a = setUp({ props: props }), user = _a.user, rerender = _a.rerender;
                    props.getLookups.mockClear();
                    props.getLookups
                        .mockClear()
                        .mockResolvedValue({ GERO: { displayName: 'Gerontology' }, IMMU: { displayName: 'Immunology' } });
                    rerender({ lookupCode: 'SPEC' });
                    expect(props.getLookups).toHaveBeenCalledWith({
                        type: 'SPEC',
                        parents: [],
                        displayNamePrefix: '',
                        max: 51,
                        offset: 0
                    });
                    return [4 /*yield*/, user.click(screen.getByRole('combobox'))];
                case 1:
                    _b.sent();
                    options = screen.getAllByRole('option');
                    expect(options).toHaveLength(2);
                    expect(options[0]).toHaveTextContent('Gerontology (GERO)');
                    expect(options[1]).toHaveTextContent('Immunology (IMMU)');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call props.onChange with correct value on option click', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp().user;
                    return [4 /*yield*/, user.click(screen.getByRole('combobox'))];
                case 1:
                    _a.sent();
                    return [4 /*yield*/, user.click(screen.getByText('Argentina (AR)'))];
                case 2:
                    _a.sent();
                    expect(defaultProps.onChange).toHaveBeenCalledWith({ value: 'Argentina', lookupCode: 'AR' });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call props.getLookups with correct values on select input change and set offset to default', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, user, input;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { max: 2, getLookups: jest.fn().mockResolvedValue(allLookups) });
                    user = setUp({ props: props }).user;
                    input = screen.getByRole('combobox');
                    return [4 /*yield*/, user.type(input, 'Ch')];
                case 1:
                    _a.sent();
                    expect(props.getLookups).toHaveBeenCalledWith({
                        type: props.lookupCode,
                        parents: props.parents,
                        displayNamePrefix: 'Portugal (PT)Ch',
                        max: 3,
                        offset: 0
                    });
                    props.getLookups.mockReturnValue(Promise.resolve({ RU: { displayName: 'Russia' }, USA: { displayName: 'USA' }, MEX: { displayName: 'MEXICO' } }));
                    return [4 /*yield*/, user.click(screen.getByText('Load more'))];
                case 2:
                    _a.sent();
                    // offset === max
                    expect(props.getLookups).toHaveBeenCalledWith({
                        type: props.lookupCode,
                        parents: props.parents,
                        displayNamePrefix: 'Portugal (PT)Ch',
                        max: 3,
                        offset: 2
                    });
                    return [4 /*yield*/, user.type(input, 'i')];
                case 3:
                    _a.sent();
                    // set offset to default: 0
                    expect(props.getLookups).toHaveBeenCalledWith({
                        type: props.lookupCode,
                        parents: props.parents,
                        displayNamePrefix: 'Portugal (PT)Chi',
                        max: 3,
                        offset: 0
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should render empty state if there is no options', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { getLookups: jest.fn().mockReturnValue(Promise.resolve({})) });
                    user = setUp({ props: props }).user;
                    return [4 /*yield*/, user.click(screen.getByRole('combobox'))];
                case 1:
                    _a.sent();
                    screen.getByText('No results found');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should save focus on select after changing disable props', function () { return __awaiter(void 0, void 0, void 0, function () {
        var _a, user, rerender, input;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    _a = setUp(), user = _a.user, rerender = _a.rerender;
                    input = screen.getByRole('combobox');
                    return [4 /*yield*/, user.click(input)];
                case 1:
                    _b.sent();
                    rerender({ disabled: true });
                    expect(input).toHaveFocus();
                    rerender({ disabled: false });
                    expect(input).toHaveFocus();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should change inputValue, call onChange property to remove value property and call getLookups as well on input clear', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user, input;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp().user;
                    input = screen.getByRole('combobox');
                    return [4 /*yield*/, user.clear(input)];
                case 1:
                    _a.sent();
                    expect(input).toHaveValue('');
                    expect(defaultProps.onChange).toHaveBeenCalledWith('');
                    expect(defaultProps.getLookups).toHaveBeenCalledWith({
                        type: 'COUNTRY_CD',
                        parents: [],
                        displayNamePrefix: '',
                        max: 51,
                        offset: 0
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call props.onChange if lookup is unresolved but has resolved value in lookups', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, props;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    value = { value: '', lookupCode: 'PT' };
                    props = __assign(__assign({}, defaultProps), { value: value });
                    setUp({ props: props });
                    return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, Promise.resolve()];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(props.onChange).toHaveBeenCalledWith({ lookupCode: 'PT', value: 'Portugal' });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call props.resolveLookups if lookup is unresolved and has no resolved value in lookups', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, props;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    value = { lookupCode: 'AR', value: '' };
                    props = __assign(__assign({}, defaultProps), { value: value });
                    setUp({ props: props });
                    return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, Promise.resolve()];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(props.resolveLookups).toHaveBeenCalledWith(props.lookupCode, ['AR']);
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call props.resolveLookups once if lookup is unresolved and has no resolved value in lookups and has no resolved value in response', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, props, rerender;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    value = { lookupCode: 'AR', value: '' };
                    props = __assign(__assign({}, defaultProps), { value: value });
                    rerender = setUp({ props: props }).rerender;
                    return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, Promise.resolve()];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })];
                case 1:
                    _a.sent();
                    rerender({ value: __assign({}, value) });
                    expect(props.resolveLookups).toHaveBeenCalledTimes(1);
                    expect(props.resolveLookups).toHaveBeenCalledWith(props.lookupCode, ['AR']);
                    return [2 /*return*/];
            }
        });
    }); });
    it('should disable editor according "disabled" prop', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { disabled: true });
                    setUp({ props: props });
                    return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, Promise.resolve()];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(screen.getByRole('combobox')).toBeDisabled();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should use "parents" prop for lookups request', function () { return __awaiter(void 0, void 0, void 0, function () {
        var parents, props;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    parents = ['parent1', 'parent2'];
                    props = __assign(__assign({}, defaultProps), { parents: parents });
                    setUp({ props: props });
                    return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, Promise.resolve()];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(props.getLookups).toHaveBeenCalledWith({
                        displayNamePrefix: '',
                        max: 51,
                        offset: 0,
                        type: props.lookupCode,
                        parents: parents
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should not send lookups request if "parents" prop is null', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { parents: null });
                    user = setUp({ props: props }).user;
                    return [4 /*yield*/, user.type(screen.getByRole('combobox'), 'Ch')];
                case 1:
                    _a.sent();
                    screen.getByText('No results found');
                    expect(props.getLookups).not.toHaveBeenCalled();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should send lookups request if "parents" is undefined', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { parents: undefined });
                    setUp({ props: props });
                    return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, Promise.resolve()];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(props.getLookups).toHaveBeenCalledWith({
                        displayNamePrefix: '',
                        max: 51,
                        offset: 0,
                        type: props.lookupCode
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    describe('Autopopulation behavior', function () {
        var defaultAutopopulationContextValue = { id: 'id', onTouch: jest.fn(), isTouched: false };
        it('should call onChange on component mount when there is only one possible option, value is empty and autopopulation is enabled', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, CustomProviders;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { value: { lookupCode: '', value: '' }, autopopulationId: defaultAutopopulationContextValue.id, getLookups: jest.fn().mockReturnValue(Promise.resolve({ AR: { displayName: 'Argentina' } })) });
                        CustomProviders = function (_a) {
                            var children = _a.children;
                            return (React.createElement(DependentLookupAutopopulationContext.Provider, { value: defaultAutopopulationContextValue }, children));
                        };
                        setUp({ props: props, CustomProviders: CustomProviders });
                        return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0: return [4 /*yield*/, Promise.resolve()];
                                        case 1:
                                            _a.sent();
                                            return [2 /*return*/];
                                    }
                                });
                            }); })];
                    case 1:
                        _a.sent();
                        expect(props.onChange).toHaveBeenCalledWith({ lookupCode: 'AR', value: 'Argentina' });
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not autopopulate editor if autopopulationId is not set', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, autopopulationContextValue, CustomProviders;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { value: { lookupCode: '', value: '' }, getLookups: jest.fn().mockReturnValue(Promise.resolve({ AR: { displayName: 'Argentina' } })) });
                        autopopulationContextValue = __assign(__assign({}, defaultAutopopulationContextValue), { id: undefined });
                        CustomProviders = function (_a) {
                            var children = _a.children;
                            return (React.createElement(DependentLookupAutopopulationContext.Provider, { value: autopopulationContextValue }, children));
                        };
                        setUp({ props: props, CustomProviders: CustomProviders });
                        return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0: return [4 /*yield*/, Promise.resolve()];
                                        case 1:
                                            _a.sent();
                                            return [2 /*return*/];
                                    }
                                });
                            }); })];
                    case 1:
                        _a.sent();
                        expect(props.onChange).not.toHaveBeenCalled();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not autopopulate editor if value is not empty', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, CustomProviders;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { value: { lookupCode: 'AR', value: '' }, autopopulationId: defaultAutopopulationContextValue.id, getLookups: jest.fn().mockReturnValue(Promise.resolve({ AR: { displayName: 'Argentina' } })) });
                        CustomProviders = function (_a) {
                            var children = _a.children;
                            return (React.createElement(DependentLookupAutopopulationContext.Provider, { value: defaultAutopopulationContextValue }, children));
                        };
                        setUp({ props: props, CustomProviders: CustomProviders });
                        return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0: return [4 /*yield*/, Promise.resolve()];
                                        case 1:
                                            _a.sent();
                                            return [2 /*return*/];
                                    }
                                });
                            }); })];
                    case 1:
                        _a.sent();
                        expect(props.onChange).not.toHaveBeenCalled();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not autopopulate editor if there are more then one option', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, CustomProviders;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { value: { lookupCode: '', value: '' }, autopopulationId: defaultAutopopulationContextValue.id });
                        CustomProviders = function (_a) {
                            var children = _a.children;
                            return (React.createElement(DependentLookupAutopopulationContext.Provider, { value: defaultAutopopulationContextValue }, children));
                        };
                        setUp({ props: props, CustomProviders: CustomProviders });
                        return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0: return [4 /*yield*/, Promise.resolve()];
                                        case 1:
                                            _a.sent();
                                            return [2 /*return*/];
                                    }
                                });
                            }); })];
                    case 1:
                        _a.sent();
                        expect(props.onChange).not.toHaveBeenCalled();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not autopopulate editor if there is only one option but not for first time', function () { return __awaiter(void 0, void 0, void 0, function () {
            var times, getLookups, props, CustomProviders, user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        times = 0;
                        getLookups = jest.fn().mockImplementation(function () {
                            if (times === 0) {
                                return Promise.resolve({ RU: { displayName: 'Russia' }, CN: { displayName: 'China' } });
                            }
                            else {
                                return Promise.resolve({ RU: { displayName: 'Russia' } });
                            }
                        });
                        props = __assign(__assign({}, defaultProps), { value: { lookupCode: '', value: '' }, autopopulationId: defaultAutopopulationContextValue.id, getLookups: getLookups });
                        CustomProviders = function (_a) {
                            var children = _a.children;
                            return (React.createElement(DependentLookupAutopopulationContext.Provider, { value: defaultAutopopulationContextValue }, children));
                        };
                        user = setUp({ props: props, CustomProviders: CustomProviders }).user;
                        return [4 /*yield*/, user.type(screen.getByRole('combobox'), 'u')];
                    case 1:
                        _a.sent();
                        expect(getLookups).toHaveBeenCalledTimes(2);
                        expect(props.onChange).not.toHaveBeenCalled();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should call onTouch on change value', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, CustomProviders, user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { value: { lookupCode: '', value: '' }, autopopulationId: defaultAutopopulationContextValue.id, getLookups: jest.fn().mockReturnValue(Promise.resolve({ AR: { displayName: 'Argentina' } })) });
                        CustomProviders = function (_a) {
                            var children = _a.children;
                            return (React.createElement(DependentLookupAutopopulationContext.Provider, { value: defaultAutopopulationContextValue }, children));
                        };
                        user = setUp({ props: props, CustomProviders: CustomProviders }).user;
                        return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0: return [4 /*yield*/, Promise.resolve()];
                                        case 1:
                                            _a.sent();
                                            return [2 /*return*/];
                                    }
                                });
                            }); })];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, user.type(screen.getByRole('combobox'), '{Backspace}{Enter}')];
                    case 2:
                        _a.sent();
                        expect(defaultAutopopulationContextValue.onTouch).toHaveBeenCalledWith(defaultAutopopulationContextValue.id);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not autopopulate editor if isTouched prop is true', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, autopopulationContextValue, CustomProviders;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { value: { lookupCode: '', value: '' }, autopopulationId: defaultAutopopulationContextValue.id, getLookups: jest.fn().mockReturnValue(Promise.resolve({ AR: { displayName: 'Argentina' } })) });
                        autopopulationContextValue = __assign(__assign({}, defaultAutopopulationContextValue), { isTouched: true });
                        CustomProviders = function (_a) {
                            var children = _a.children;
                            return (React.createElement(DependentLookupAutopopulationContext.Provider, { value: autopopulationContextValue }, children));
                        };
                        setUp({ props: props, CustomProviders: CustomProviders });
                        return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0: return [4 /*yield*/, Promise.resolve()];
                                        case 1:
                                            _a.sent();
                                            return [2 /*return*/];
                                    }
                                });
                            }); })];
                    case 1:
                        _a.sent();
                        expect(props.onChange).not.toHaveBeenCalled();
                        return [2 /*return*/];
                }
            });
        }); });
    });
});
