"use strict";
/**
 * Test case for FieldContext
 *
 *  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 FieldContextStore_1 = require("../../../src/ui/context/FieldContextStore");
var chai_1 = require("chai");
describe('FieldContextStore', function () {
    [
        {
            element_id: 'qcontainer_checkbox_foo',
            sibling_id: 'qlabel_checkbox_foo',
            index: 10,
            expected_content: '<dd id="qcontainer_checkbox_foo" data-index="10">' +
                '<input type="checkbox" id="q_checkbox_foo_n_10" data-index="10">' +
                '<input type="checkbox" id="q_checkbox_foo_y_10" data-index="10">' +
                '</dd>',
            expected_sibling: '<dt id="qlabel_checkbox_foo" data-index="10">Foo</dt>',
        },
        {
            element_id: 'qcontainer_foo_bar_long_name',
            sibling_id: 'qlabel_foo_bar_long_name',
            index: 324,
            expected_content: '<dd id="qcontainer_foo_bar_long_name" data-index="324">' +
                '<input type="text" id="foo_bar_324" data-index="324">' +
                '</dd>',
            expected_sibling: '<dt id="qlabel_foo_bar_long_name" data-index="324">Bar</dt>',
        },
    ].forEach(function (_a) {
        var element_id = _a.element_id, sibling_id = _a.sibling_id, index = _a.index, expected_content = _a.expected_content, expected_sibling = _a.expected_sibling;
        it('creates a clone of the content and sets the element index for ' +
            element_id, function () {
            var group_content = getGroupContent();
            var content = group_content.querySelector('#' + element_id);
            var sibling = group_content.querySelector('#' + sibling_id);
            var sut = new FieldContextStore_1.FieldContextStore('foo', content, sibling);
            var content_modified = (sut.getContentClone(index));
            var sibling_modified = (sut.getSiblingContentClone(index));
            chai_1.expect(content_modified === null || content_modified === void 0 ? void 0 : content_modified.outerHTML).to.equal(expected_content);
            chai_1.expect(sibling_modified === null || sibling_modified === void 0 ? void 0 : sibling_modified.outerHTML).to.equal(expected_sibling);
            var content_original = sut.content;
            var sibling_original = sut.siblingContent;
            chai_1.expect(content_original === null || content_original === void 0 ? void 0 : content_original.outerHTML).to.equal(content === null || content === void 0 ? void 0 : content.outerHTML);
            chai_1.expect(sibling_original === null || sibling_original === void 0 ? void 0 : sibling_original.outerHTML).to.equal(sibling === null || sibling === void 0 ? void 0 : sibling.outerHTML);
        });
    });
    it('displays internal fields on cloned content if user is internal', function () {
        var index = 45;
        var element_id = 'qcontainer_internal_field';
        var sibling_id = 'qlabel_internal_field';
        var expected_content_clone = '<dd id="qcontainer_internal_field" class="i" data-index="45">' +
            '<input type="text" class="i" id="foo_bar_45" data-index="45">' +
            '</dd>';
        var expected_sibling_clone = '<dt id="qlabel_internal_field" class="i" data-index="45">Test Ind</dt>';
        var expected_content = '<dd id="qcontainer_internal_field" class="i">' +
            '<input type="text" class="i" id="foo_bar_0">' +
            '</dd>';
        var expected_sibling = '<dt id="qlabel_internal_field" class="i">Test Ind</dt>';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var sibling = group_content.querySelector('#' + sibling_id);
        var sut = new FieldContextStore_1.FieldContextStore('foo', content, sibling, true);
        var content_modified = (sut.getContentClone(index));
        var sibling_modified = (sut.getSiblingContentClone(index));
        chai_1.expect(content_modified === null || content_modified === void 0 ? void 0 : content_modified.outerHTML).to.equal(expected_content_clone);
        chai_1.expect(sibling_modified === null || sibling_modified === void 0 ? void 0 : sibling_modified.outerHTML).to.equal(expected_sibling_clone);
        var content_original = sut.content;
        var sibling_original = sut.siblingContent;
        chai_1.expect(content_original === null || content_original === void 0 ? void 0 : content_original.outerHTML).to.equal(expected_content);
        chai_1.expect(sibling_original === null || sibling_original === void 0 ? void 0 : sibling_original.outerHTML).to.equal(expected_sibling);
    });
    it('determines the position of the content', function () {
        var element_id = 'qcontainer_checkbox_foo';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var expected_position = 1;
        var sut = new FieldContextStore_1.FieldContextStore('foo', content, null);
        var given = sut.position;
        chai_1.expect(given).to.equal(expected_position);
    });
    it('detaches the content and sibling from the parent', function () {
        var element_id = 'qcontainer_foo_bar_long_name';
        var sibling_id = 'qlabel_foo_bar_long_name';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var sibling = group_content.querySelector('#' + sibling_id);
        var sut = new FieldContextStore_1.FieldContextStore('foo', content, sibling);
        sut.detach();
        chai_1.expect(content === null || content === void 0 ? void 0 : content.parentElement).to.equal(null);
        chai_1.expect(sibling === null || sibling === void 0 ? void 0 : sibling.parentElement).to.equal(null);
    });
    it('determines the position of the sibling when not null', function () {
        var element_id = 'qlabel_foo_bar_long_name';
        var sibling_id = 'qcontainer_foo_bar_long_name';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var sibling = group_content.querySelector('#' + sibling_id);
        var expected_position = 5;
        var sut = new FieldContextStore_1.FieldContextStore('foo', content, sibling);
        var given = sut.position;
        chai_1.expect(given).to.equal(expected_position);
    });
    [
        {
            label: 'isSubField is true when parent is a widget',
            element_id: 'q_subfield_single_0',
            content_id: 'qcontainer_subfield_single',
            expected: true,
        },
        {
            label: 'isSubField is true when parent is a widget',
            element_id: 'q_baz_subfield_0',
            content_id: 'qcontainer_subfield',
            expected: true,
        },
        {
            label: 'isSubField is false when parent is not a widget',
            element_id: 'q_foo_bar_0',
            content_id: 'qcontainer_foo_bar',
            expected: false,
        },
    ].forEach(function (_a) {
        var label = _a.label, element_id = _a.element_id, content_id = _a.content_id, expected = _a.expected;
        it(label, function () {
            var group_content = getGroupContent();
            var content = group_content.querySelector('#' + content_id);
            var sut = new FieldContextStore_1.FieldContextStore(element_id, content, null);
            var given = sut.isSubField();
            chai_1.expect(given).to.equal(expected);
        });
    });
    it('returns the subfield parent field name', function () {
        var expected_parent = 'bi_risk_type';
        var element_id = 'q_baz_subfield_0';
        var content_id = 'qcontainer_subfield';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + content_id);
        var sut = new FieldContextStore_1.FieldContextStore(element_id, content, null);
        sut.isSubField();
        var given = sut.subFieldParentName;
        chai_1.expect(given).to.equal(expected_parent);
    });
    it('sets and returns the field value by index', function () {
        var element_id = 'qlabel_foo_bar_long_name';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var value = 'foo';
        var index = 1;
        var sut = new FieldContextStore_1.FieldContextStore(element_id, content, null);
        sut.setValueByIndex(index, value);
        var first_result = sut.getValueByIndex(index);
        // call it again, and it should be an empty string
        var second_result = sut.getValueByIndex(index);
        chai_1.expect(first_result).to.equal(value);
        chai_1.expect(second_result).to.equal('');
    });
    it('returns empty string when field value not set', function () {
        var element_id = 'qlabel_foo_bar_long_name';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var index = 1;
        var sut = new FieldContextStore_1.FieldContextStore(element_id, content, null);
        var given = sut.getValueByIndex(index);
        chai_1.expect(given).to.equal('');
    });
    it('sets and returns the field options by index', function () {
        var element_id = 'qlabel_foo_bar_long_name';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var options = [
            { value: 'foo', label: 'Foo goes here', label_id: 'foo_label' },
            { value: 'bar', label: 'Bar goes here', label_id: 'bar_label' },
        ];
        var index = 1;
        var sut = new FieldContextStore_1.FieldContextStore(element_id, content, null);
        sut.setOptionsByIndex(index, options);
        var first_result = sut.getOptionsByIndex(index);
        // call it again, and it should be an empty array
        var second_result = sut.getOptionsByIndex(index);
        chai_1.expect(first_result).to.equal(options);
        chai_1.expect(second_result).to.deep.equal([]);
    });
    it('returns empty array when field options not set', function () {
        var element_id = 'qlabel_foo_bar_long_name';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var index = 1;
        var sut = new FieldContextStore_1.FieldContextStore(element_id, content, null);
        var given = sut.getOptionsByIndex(index);
        chai_1.expect(given).to.deep.equal([]);
    });
});
function getGroupContent() {
    // Mock group content
    var group = document.createElement('dl');
    group.innerHTML =
        '<dt id="qlabel_checkbox_foo">Foo</dt>' +
            '<dd id="qcontainer_checkbox_foo">' +
            '<input type="checkbox" id="q_checkbox_foo_n_0">' +
            '<input type="checkbox" id="q_checkbox_foo_y_0">' +
            '</dd>' +
            '<dd id="qcontainer_checkbox_no_label">' +
            '<input type="checkbox" id="q_checkbox_no_label_n_0">' +
            '</dd>' +
            '<dd id="qcontainer_subfield" class="foo widget">' +
            '<select id="q_bi_risk_type_0" data-field-name="bi_risk_type" class="foo widget">' +
            '<option id="q_bar_subfield_0" value="Bar">Bar</option>' +
            '<option id="q_baz_subfield_0" value="Baz">Baz</option>' +
            '<option id="q_qux_subfield_0" value="Qux">Qux</option>' +
            '</select>' +
            '</dd>' +
            '<dt id="qlabel_foo_bar_long_name">Bar</dt>' +
            '<dd id="qcontainer_foo_bar_long_name">' +
            '<input type="text" id="foo_bar_0" >' +
            '</dd>' +
            '<dt id="qlabel_internal_field" class="hidden i">Test Ind</dt>' +
            '<dd id="qcontainer_internal_field" class="hidden i">' +
            '<input type="text" class="hidden i" id="foo_bar_0" >' +
            '</dd>' +
            '<dd id="qcontainer_subfield_single" class="foo widget">' +
            '<select id="q_subfield_single_type_0" class="foo widget">' +
            '<option id="q_subfield_single_0" value="Foo">Foo</option>' +
            '</select>' +
            '</dd>' +
            '<dt id="qlabel_baz">Baz</dt>' +
            '<dd id="qcontainer_baz">' +
            '<input type="text" id="foo_baz_0">' +
            '</dd>' +
            '<dt id="qlabel_foo_bar">Baz</dt>' +
            '<dd id="qcontainer_foo_bar">' +
            '<input type="text" id="q_foo_bar_0">' +
            '</dd>' +
            '<dd id="qcontainer_select_element">' +
            '<select id="q_select_element_0">' +
            '<option value="bar" default="true">Existing option bar</option>';
    '</select>' + '</dd>';
    return group;
}
//# sourceMappingURL=data:application/json;base64, 