"use strict";
/**
 * Test case for GeneralStepUi
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework
 *
 *  Liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var GeneralStepUi_1 = require("../../../src/ui/step/GeneralStepUi");
var expect = require('chai').expect, sinon = require('sinon');
describe('ui.GeneralStepUi', function () {
    describe('on answer data change', function () {
        it('will attempt pre-style with formatter', function (done) {
            var orig_data = {
                foo: ['orig'],
            }, fmt_data = {
                foo: ['formatted'],
            };
            var formatter = createFormatter(), mock_fmt = sinon.mock(formatter);
            mock_fmt
                .expects('format')
                .once()
                .withExactArgs(orig_data)
                .returns(fmt_data);
            var sut = createSut(formatter, createElementStyler(function (name, value) {
                // by the time the answer data makes its way to the
                // element styler, it should have already been
                // formatted
                expect(name).to.equal('foo');
                expect(value).to.equal(fmt_data.foo[0]);
                mock_fmt.verify();
                done();
            }));
            sut.setActive(true);
            sut.answerDataUpdate(orig_data);
        });
        [
            {
                label: 'group will style answer when feature flag is on',
                field_refs: ['foo_d1022e47903'],
                data: { foo: ['bar'] },
                expected_names: ['foo_d1022e47903'],
                expected_indexes: [0],
                expected_values: ['bar'],
                expected_calls: 1,
            },
            {
                label: 'group will style multiple answers when feature flag is on',
                field_refs: ['foo_d1022e47903', 'foo_d222222222'],
                data: { foo: ['bar'] },
                expected_names: ['foo_d1022e47903', 'foo_d222222222'],
                expected_indexes: [0, 0],
                expected_values: ['bar', 'bar'],
                expected_calls: 2,
            },
        ].forEach(function (_a) {
            var label = _a.label, field_refs = _a.field_refs, data = _a.data, expected_names = _a.expected_names, expected_indexes = _a.expected_indexes, expected_values = _a.expected_values, expected_calls = _a.expected_calls;
            it(label, function (done) {
                var formatter = createFormatter(data);
                var feature_flag = createFeatureFlag(true);
                var group = createGroupUi();
                var group_set_value_calls = 0;
                var given_values = [];
                var given_names = [];
                var given_indexes = [];
                var answer_field_ref_calls = 0;
                var async_flag = false;
                var styler = createElementStyler(function () { });
                styler.styleAnswer = function (_) {
                    done(new Error('ElementStyler should not be called with feature flag on'));
                };
                group.setValueByName = function (name, index, value, _) {
                    group_set_value_calls++;
                    given_values.push(value);
                    given_names.push(name);
                    given_indexes.push(index);
                    if (async_flag && group_set_value_calls === expected_calls) {
                        // value is set w/answer ref
                        expect(given_names).to.deep.equal(expected_names);
                        expect(given_indexes).to.deep.equal(expected_indexes);
                        expect(given_values).to.deep.equal(expected_values);
                        expect(answer_field_ref_calls).to.equal(1);
                        done();
                    }
                    return group;
                };
                var sut = createSut(formatter, styler, {}, group, feature_flag);
                sut.getAnswerFieldRefs = function (_) {
                    answer_field_ref_calls++;
                    return field_refs;
                };
                var get_group_call_count = 0;
                sut.getElementGroup = function (name) {
                    expect(name).to.equal(field_refs[get_group_call_count]);
                    get_group_call_count++;
                    return group;
                };
                sut.setActive(true);
                sut.answerDataUpdate(data);
                async_flag = true;
            });
        });
    });
    describe('#scrollTo', function () {
        [
            {
                field: '',
                index: 0,
                cause: 'foocause',
            },
            {
                field: undefined,
                index: 0,
                cause: 'foocause2',
            },
            {
                field: '',
                index: 0,
                cause: '',
            },
            {
                field: '',
                index: 0,
                cause: undefined,
            },
            {
                field: 'foo',
                index: -1,
                cause: undefined,
            },
            {
                field: 'foo',
                index: undefined,
                cause: undefined,
            },
            {
                field: 'foo',
                index: undefined,
                cause: 'index cause',
            },
        ].forEach(function (args, i) {
            it('emits error given invalid field (' + i + ')', function (done) {
                var step = {
                    getValidCause: function () {
                        return args.cause;
                    },
                };
                var sut = createSut({}, {}, step);
                // should only throw a single error
                sut.once('error', function (error) {
                    expect(error).to.be.instanceof(Error);
                    expect(error.message).to.have.string('Could not scroll');
                    if (args.cause) {
                        expect(error.message).to.have.string('cause: ');
                        expect(error.message).to.have.string(args.cause);
                    }
                    else {
                        expect(error.message).to.not.have.string('cause:');
                    }
                    done();
                });
                sut.scrollTo(args.field, args.index, false, args.cause);
            });
        });
        it('emits error when element is not found', function (done) {
            var field = 'foo', index = 5;
            var styler = {
                getProperIndex: function () { },
                getWidgetByName: function () {
                    // no element
                    return [];
                },
            };
            var sut = createSut({}, styler, {});
            sut.once('error', function (error) {
                expect(error).to.be.instanceof(Error);
                expect(error.message).to.have.string('Could not scroll');
                expect(error.message).to.have.string('could not locate');
                expect(error.message).to.have.string(field + '[' + index + ']');
                done();
            });
            sut.scrollTo(field, index, false, '');
        });
        it('emits error when element is not visible', function (done) {
            var field = 'foo', index = 5;
            var styler = {
                getProperIndex: function () { },
                getWidgetByName: function () {
                    var element = {
                        is: function (selector) {
                            expect(selector).to.equal(':visible');
                            return false;
                        },
                    };
                    return element;
                },
            };
            var sut = createSut({}, styler, {});
            sut.once('error', function (error) {
                expect(error).to.be.instanceof(Error);
                expect(error.message).to.have.string('Could not scroll');
                expect(error.message).to.have.string('not visible');
                expect(error.message).to.have.string(field + '[' + index + ']');
                done();
            });
            sut.scrollTo(field, index, false, '');
        });
        it('scrolls to the failed element', function (done) {
            var field = 'foo', index = 5, showm = true, message = 'whatacluster', element = {
                0: {
                    attributes: [],
                },
                is: function (selector) {
                    expect(selector).to.equal(':visible');
                    return true;
                },
            };
            var styler = {
                getProperIndex: function () { },
                getWidgetByName: function () { return element; },
                focus: function (_, __, ___) { },
            };
            var content = {
                parent: function () {
                    return {
                        scrollTo: scroll_mock,
                    };
                },
            };
            function scroll_mock(given_element, duration, options) {
                expect(given_element).to.equal(element);
                expect(duration).to.equal(100);
                expect(options.offset.top).to.equal(-150);
                styler.focus = function (given_element, given_showm, given_msg) {
                    expect(given_element).to.equal(element);
                    expect(given_showm).to.equal(showm);
                    expect(given_msg).to.equal(message);
                    done();
                };
                options.onAfter();
            }
            var sut = createSut({}, styler, {});
            // XXX: SUT needs refactoring!
            sut.$content = content;
            var result = sut.scrollTo(field, index, true, message);
            expect(result).to.equal(sut);
        });
    });
    describe('#lock', function () {
        it('locks all collections', function () {
            var set_lock_calls = 0;
            var collection_0 = createCollection();
            var collection_1 = createCollection();
            var step = {
                getExclusiveFieldNames: function () {
                    return [];
                },
            };
            var sut = createSut({}, {}, step);
            sut.collections = [collection_0, collection_1];
            collection_0.lock = function (lock) {
                set_lock_calls++;
                expect(lock).to.equal(true);
            };
            collection_1.lock = function (lock) {
                set_lock_calls++;
                expect(lock).to.equal(true);
            };
            sut.lock(true);
            expect(set_lock_calls).to.equal(2);
        });
    });
});
/**
 * Create new SUT with formatter FORMATTER and generated element
 * styler
 *
 * @param {Object} formatter    validator/formatter mock
 * @param {Object} styler       mock ElementStyler
 * @param {Object} step         mock step
 * @param {Object} group        mocked Group Ui
 * @param {Object} feature_flag mocked feature flag
 *
 * @return {Sut}
 */
function createSut(formatter, styler, step, _group, feature_flag) {
    var sut = new GeneralStepUi_1.GeneralStepUi(step || {}, styler, formatter, feature_flag || createFeatureFlag(), undefined);
    sut.getAnswerContext = function (_) {
        return {};
    };
    sut.getAnswerFieldRefs = function (_) {
        return [];
    };
    sut.getElementGroup = function (_) { return ({}); };
    return sut;
}
/**
 * Create mock Collection
 *
 * @return {Object} Collection
 */
function createCollection() {
    return {
        visit: function () { },
        lock: function (_) { },
    };
}
/**
 * Create mock FeatureFlag
 *
 * @param {boolean} flag_ind
 *
 * @return {Object} FeatureFlag
 */
function createFeatureFlag(flag_ind) {
    if (flag_ind === void 0) { flag_ind = false; }
    return {
        isEnabled: function (_) {
            return !!flag_ind;
        },
    };
}
/**
 * Create mock GroupUi
 *
 * @return {Object} GroupUi
 */
function createGroupUi() {
    return {
        setValueByName: function (_, __, ___, ____) { },
    };
}
/**
 * Create mock ElementStyler
 *
 * styleAnswer method is defined by STYLE_CALLBACK
 *
 * @param {function(string,*)} style_callback styler styleAnswer method dfn
 *
 * @return {Object} ElementStyler mock
 */
function createElementStyler(style_callback) {
    return {
        getAnswerElementByName: function () {
            // jQuery element
            var $element = {
                0: {
                    attributes: [],
                },
                length: 1,
                text: function () { },
            };
            return $element;
        },
        getProperIndex: function () { },
        getWidgetByName: function () {
            return [];
        },
        styleAnswer: style_callback,
    };
}
/**
 * Create mock validator/formatter
 *
 * The only method provided is `format', which contains no definition;
 * it is expected to be mocked by the caller.
 *
 * @param {Object}
 */
function createFormatter(return_data) {
    return {
        format: function (_) {
            return return_data;
        },
    };
}
//# sourceMappingURL=data:application/json;base64, 