"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 FieldContext_1 = require("../../../src/ui/context/FieldContext");
var chai_1 = require("chai");
before(function () {
    this.jsdom = require('jsdom-global')();
});
after(function () {
    this.jsdom();
});
describe('FieldContext', function () {
    it('sibling is null when label does not exist', function () {
        var element_id = 'qcontainer_checkbox_no_label';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var sut = new FieldContext_1.FieldContext(document, getStylerStub(), element_id, content);
        var expected_content = '<dd id="qcontainer_checkbox_no_label">' +
            '<input type="checkbox" id="q_checkbox_no_label_n_0">' +
            '</dd>';
        var given = sut.getFirstOfContentSet();
        chai_1.expect(given === null || given === void 0 ? void 0 : given.outerHTML).to.equal(expected_content);
    });
    it('gets element ID', function () {
        var element_id = 'qcontainer_checkbox_no_label';
        var group_content = getGroupContent();
        var content = group_content.querySelector('#' + element_id);
        var sut = new FieldContext_1.FieldContext(document, getStylerStub(), element_id, content);
        chai_1.expect(sut.getElementId()).to.equal(element_id);
    });
    [
        {
            label: 'attaches text field to DOM with next element',
            next_element: 'div',
            element_id: 'qcontainer_baz',
            sibling_id: 'qlabel_baz',
            expected: '<dl>' +
                '<dt id="qlabel_baz">Baz</dt>' +
                '<dd id="qcontainer_baz">' +
                '<input type="text" id="foo_baz_0">' +
                '</dd>' +
                '<div></div>' +
                '</dl>',
        },
        {
            label: 'attaches text field to DOM w/out next element',
            next_element: null,
            element_id: 'qcontainer_baz',
            sibling_id: 'qlabel_baz',
            expected: '<dl>' +
                '<dt id="qlabel_baz">Baz</dt>' +
                '<dd id="qcontainer_baz">' +
                '<input type="text" id="foo_baz_0">' +
                '</dd>' +
                '</dl>',
        },
        {
            label: 'attaches checkbox field to DOM with next element',
            next_element: 'div',
            element_id: 'qcontainer_checkbox_foo',
            sibling_id: 'qlabel_checkbox_foo',
            expected: '<dl>' +
                '<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>' +
                '<div></div>' +
                '</dl>',
        },
    ].forEach(function (_a) {
        var label = _a.label, next_element = _a.next_element, element_id = _a.element_id, sibling_id = _a.sibling_id, expected = _a.expected;
        it(label, function () {
            var group_content = getGroupContent();
            var content = group_content.querySelector('#' + element_id);
            var sibling = group_content.querySelector('#' + sibling_id);
            var sut = new FieldContext_1.FieldContext(document, getStylerStub(), element_id, content, sibling);
            var to_content = document.createElement('dl');
            var dummy_next_element = null;
            if (next_element !== null) {
                dummy_next_element = document.createElement(next_element);
                to_content.appendChild(dummy_next_element);
            }
            sut.show(to_content, dummy_next_element);
            chai_1.expect(to_content.outerHTML).to.equal(expected);
        });
    });
    it('detaches field and sibling from DOM', function () {
        var from_content = getGroupContent();
        var content = from_content.querySelector('#qcontainer_checkbox_foo');
        var sibling = from_content.querySelector('#qlabel_checkbox_foo');
        var sut = new FieldContext_1.FieldContext(document, getStylerStub(), 'q_checkbox_foo_n_0', content, sibling);
        chai_1.expect(from_content.contains(content)).to.be.true;
        chai_1.expect(from_content.contains(sibling)).to.be.true;
        sut.hide();
        chai_1.expect(from_content.contains(content)).to.be.false;
        chai_1.expect(from_content.contains(sibling)).to.be.false;
    });
    it('detaches field from DOM when attached', function () {
        var from_content = document.createElement('dl');
        var child = document.createElement('div');
        from_content.appendChild(child);
        var sut = new FieldContext_1.FieldContext(document, getStylerStub(), 'foo', child);
        sut.hide();
        chai_1.expect(from_content.contains(child)).to.be.false;
        chai_1.expect(from_content.outerHTML).to.equal('<dl></dl>');
    });
    it('does not detach field from DOM after already detached', function () {
        var from_content = document.createElement('dl');
        var child = document.createElement('div');
        from_content.appendChild(child);
        var sut = new FieldContext_1.FieldContext(document, getStylerStub(), 'foo', child);
        // now simulate detaching
        from_content.removeChild(child);
        sut.hide();
        chai_1.expect(from_content.contains(child)).to.be.false;
        chai_1.expect(from_content.outerHTML).to.equal('<dl></dl>');
    });
    it('isAttached and isVisible is false when not attached to DOM', function () {
        var from_content = document.createElement('dl');
        var child = document.createElement('div');
        from_content.appendChild(child);
        var sut = new FieldContext_1.FieldContext(document, getStylerStub(), 'foo', child);
        // now detach
        sut.hide();
        chai_1.expect(sut.isAttached()).to.be.false;
        chai_1.expect(sut.isVisible()).to.be.false;
    });
    it('isAttached and isVisible is true when attached to DOM', function () {
        var from_content = document.createElement('dl');
        var child = document.createElement('div');
        from_content.appendChild(child);
        var sut = new FieldContext_1.FieldContext(document, getStylerStub(), 'foo', child);
        var to_content = document.createElement('dl');
        sut.show(to_content, null);
        chai_1.expect(sut.isAttached()).to.be.true;
        chai_1.expect(sut.isVisible()).to.be.true;
    });
    it('getFirstOfContentSet returns the field content', function () {
        var from_content = document.createElement('dl');
        var child = document.createElement('div');
        from_content.appendChild(child);
        var sut = new FieldContext_1.FieldContext(document, getStylerStub(), 'foo', child);
        chai_1.expect(sut.getFirstOfContentSet()).to.equal(child);
    });
    it('getFirstOfContentSet returns the sibling if exists', function () {
        var group_content = getGroupContent();
        var content = group_content.querySelector('#qcontainer_checkbox_foo');
        var sibling = group_content.querySelector('#qlabel_checkbox_foo');
        var sut = new FieldContext_1.FieldContext(document, getStylerStub(), 'q_checkbox_foo_n_0', content, sibling);
        chai_1.expect(sut.getFirstOfContentSet()).to.equal(sibling);
    });
    [
        {
            label: 'setOptions sets new value',
            options: [
                { value: 0, label: 'Foo goes here', label_id: 'foo_label' },
                { value: 'bar', label: 'Bar goes here', label_id: 'bar_label' },
                { value: 'baz', label: 'Baz goes here', label_id: 'baz_label' },
            ],
            value: 'baz',
            expected: '<dd id="qcontainer_select_element">' +
                '<select id="q_select_element_0">' +
                '<option value="0">Foo goes here</option>' +
                '<option value="bar">Bar goes here</option>' +
                '<option value="baz">Baz goes here</option>' +
                '</select>' +
                '</dd>',
            expected_set_val_calls: 1,
            expected_val: 'baz',
        },
        {
            label: 'setOptions sets new value when empty string',
            options: [
                { value: '', label: 'Foo goes here', label_id: 'foo_label' },
                { value: 'bar', label: 'Bar goes here', label_id: 'bar_label' },
                { value: 'baz', label: 'Baz goes here', label_id: 'baz_label' },
            ],
            value: '',
            expected: '<dd id="qcontainer_select_element">' +
                '<select id="q_select_element_0">' +
                '<option value="">Foo goes here</option>' +
                '<option value="bar">Bar goes here</option>' +
                '<option value="baz">Baz goes here</option>' +
                '</select>' +
                '</dd>',
            expected_set_val_calls: 1,
            expected_val: '',
        },
        {
            label: 'setOptions sets previous value when new value is not provided and old value still exists',
            options: [
                { value: 'foo', label: 'Foo goes here', label_id: 'foo_label' },
                { value: 'bar', label: 'Bar goes here', label_id: 'bar_label' },
            ],
            value: undefined,
            expected: '<dd id="qcontainer_select_element">' +
                '<select id="q_select_element_0">' +
                '<option value="foo">Foo goes here</option>' +
                '<option value="bar">Bar goes here</option>' +
                '</select>' +
                '</dd>',
            expected_set_val_calls: 1,
            expected_val: 'bar',
        },
        {
            label: 'setOptions sets default value when requested value is not an option',
            options: [
                { value: '', label: 'I am the default', label_id: 'foo_label' },
                { value: 'bar', label: 'Bar goes here', label_id: 'bar_label' },
            ],
            value: 'baz',
            expected: '<dd id="qcontainer_select_element">' +
                '<select id="q_select_element_0">' +
                '<option value="">I am the default</option>' +
                '<option value="bar">Bar goes here</option>' +
                '</select>' +
                '</dd>',
            expected_set_val_calls: 0,
            expected_val: '',
        },
        {
            label: 'setOptions sets default value when new value is not provided and old value no longer exists',
            options: [
                { value: '', label: 'I am the default', label_id: 'foo_label' },
                { value: 'baz', label: 'Baz', label_id: 'baz_label' },
            ],
            value: undefined,
            expected: '<dd id="qcontainer_select_element">' +
                '<select id="q_select_element_0">' +
                '<option value="">I am the default</option>' +
                '<option value="baz">Baz</option>' +
                '</select>' +
                '</dd>',
            expected_set_val_calls: 0,
            expected_val: '',
        },
    ].forEach(function (_a) {
        var label = _a.label, options = _a.options, value = _a.value, expected = _a.expected, expected_set_val_calls = _a.expected_set_val_calls, expected_val = _a.expected_val;
        it(label, function () {
            var group_content = getGroupContent();
            var content = group_content.querySelector('#qcontainer_select_element');
            var styler = getStylerStub();
            var set_value_calls = 0;
            var given_set_value = '';
            styler.setValue = function (_content, value) {
                set_value_calls++;
                given_set_value = value;
            };
            var sut = new FieldContext_1.FieldContext(document, styler, 'q_select_element_0', content);
            sut.setOptions(options, value);
            var final_content = sut.getFirstOfContentSet();
            var selected_option = (final_content.querySelector('#q_select_element_0'));
            chai_1.expect(selected_option === null || selected_option === void 0 ? void 0 : selected_option.value).to.equal(expected_val);
            chai_1.expect(final_content.outerHTML).to.equal(expected);
            chai_1.expect(set_value_calls).to.equal(expected_set_val_calls);
            chai_1.expect(given_set_value).to.equal(expected_val);
        });
    });
});
function getStylerStub() {
    return {
        setValue: function (_, __) { },
    };
}
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">' +
            '<select id="q_bi_risk_type_0" 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>' +
            '<dd id="qcontainer_subfield_single">' +
            '<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>' +
            '<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, 