"use strict";
/**
 * Test case for GridGroupUi
 *
 *  Copyright (C) 2010-2020 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 expect = require('chai').expect, sinon = require('sinon');
var GridGroupUi_1 = require("../../../src/ui/group/GridGroupUi");
var CommonResources_1 = require("./CommonResources");
before(function () {
    this.jsdom = require('jsdom-global')();
});
after(function () {
    this.jsdom();
});
describe('GridGroup', function () {
    describe('gettXType', function () {
        it('detects x-type for a group', function () {
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: CommonResources_1.createBoxContent() });
            expect(sut.getXType()).to.be.null;
            sut.init(CommonResources_1.createQuote());
            sut.visit();
            expect(sut.getXType()).to.equal('foo');
        });
    });
    describe('getCategory', function () {
        it('detects categories for a group', function () {
            var expected = ['foo'];
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: CommonResources_1.createBoxContent() });
            expect(sut.getCategories()).to.deep.equal([]);
            sut.init(CommonResources_1.createQuote());
            sut.visit();
            expect(sut.getCategories()).to.deep.equal(expected);
        });
    });
    describe('isVisible', function () {
        it('detects when the group is visible', function () {
            var content = CommonResources_1.createBoxContent();
            content.classList.contains = sinon.stub().returns(true);
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: content });
            expect(sut.isVisible()).to.be.false;
            sut.init(CommonResources_1.createQuote());
            sut.visit();
            expect(sut.isVisible()).to.be.true;
        });
        it('detects when the group is not visible', function () {
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: CommonResources_1.createBoxContent() });
            expect(sut.isVisible()).to.be.false;
            sut.init(CommonResources_1.createQuote());
            sut.visit();
            expect(sut.isVisible()).to.be.false;
        });
    });
    describe('isSelected', function () {
        it('detects when the group is selected', function () {
            var content = CommonResources_1.createBoxContent();
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: content });
            expect(sut.isSelected()).to.be.false;
            sut.init(CommonResources_1.createQuote());
            sut.visit();
            sut.select();
            expect(sut.isSelected()).to.be.true;
        });
        it('detects when the group is deselected', function () {
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: CommonResources_1.createBoxContent() });
            expect(sut.isSelected()).to.be.false;
            sut.init(CommonResources_1.createQuote());
            sut.visit();
            expect(sut.isSelected()).to.be.false;
        });
    });
    describe('visit', function () {
        it('sets a group to selected based on bucket value', function () {
            var selected_list_key = 'foo';
            var selected_value = 'bar';
            var mock_data = [selected_value];
            var get_data_calls = 0;
            var content = CommonResources_1.createContent(true);
            var box_content = CommonResources_1.createContent(true);
            box_content.querySelector = function () { return content; };
            var quote = CommonResources_1.createQuote();
            quote.getDataByName = function (name) {
                get_data_calls++;
                expect(name).to.equal(selected_list_key);
                return mock_data;
            };
            content.getAttribute
                .withArgs('data-selected-list-key')
                .returns(selected_list_key);
            content.getAttribute
                .withArgs('data-selected-value')
                .returns(selected_value);
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: box_content });
            sut.init(quote);
            sut.visit();
            expect(get_data_calls).to.equal(1);
            expect(sut.isSelected()).to.be.true;
        });
        it('sets a group to deselected if already selected and disabled state is true', function () {
            var selected_current_key = 'cur';
            var selected_list_key = 'foo';
            var selected_value = 'bar';
            var mock_data = [selected_value];
            var expected_data = {
                foo: [null],
                cur: [],
            };
            var set_data_calls = 0;
            var group = CommonResources_1.createGroup('foo', [], [], false);
            var state_manager = CommonResources_1.createStateManager();
            var content = CommonResources_1.createContent();
            var box_content = CommonResources_1.createContent();
            box_content.querySelector = function () { return content; };
            var quote = CommonResources_1.createQuote();
            quote.getDataByName.withArgs(selected_list_key).returns(mock_data);
            quote.getDataByName.withArgs(selected_current_key).returns(mock_data);
            quote.setData = function (data) {
                set_data_calls++;
                expect(data).to.deep.equal(expected_data);
            };
            content.getAttribute
                .withArgs('data-selected-current-key')
                .returns(selected_current_key);
            content.getAttribute
                .withArgs('data-selected-list-key')
                .returns(selected_list_key);
            content.getAttribute
                .withArgs('data-selected-value')
                .returns(selected_value);
            // Mock state manager sets disabled state
            state_manager.observes = sinon.stub().returns(true);
            state_manager.is = sinon.stub().returns(true);
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, {
                content: box_content,
                state_manager: state_manager,
                group: group,
            });
            sut.init(quote);
            // Simulate calling select first
            sut.select();
            sut.visit();
            expect(set_data_calls).to.equal(1);
            expect(sut.isSelected()).to.be.false;
        });
        it('sets a group to deselected if it is not visible', function () {
            var selected_current_key = 'cur';
            var selected_list_key = 'foo';
            var selected_value = 'bar';
            var mock_data = [selected_value];
            var expected_data = {
                foo: [null],
                cur: [],
            };
            var set_data_calls = 0;
            var group = CommonResources_1.createGroup('foo', [], [], false);
            var state_manager = CommonResources_1.createStateManager();
            // Make these hidden
            var content = CommonResources_1.createContent(false);
            var box_content = CommonResources_1.createContent(false);
            box_content.querySelector = function () { return content; };
            var quote = CommonResources_1.createQuote();
            quote.getDataByName.withArgs(selected_list_key).returns(mock_data);
            quote.getDataByName.withArgs(selected_current_key).returns(mock_data);
            quote.setData = function (data) {
                set_data_calls++;
                expect(data).to.deep.equal(expected_data);
            };
            content.getAttribute
                .withArgs('data-selected-current-key')
                .returns(selected_current_key);
            content.getAttribute
                .withArgs('data-selected-list-key')
                .returns(selected_list_key);
            content.getAttribute
                .withArgs('data-selected-value')
                .returns(selected_value);
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, {
                content: box_content,
                state_manager: state_manager,
                group: group,
            });
            sut.init(quote);
            // Simulate calling select first
            sut.select();
            sut.visit();
            expect(set_data_calls).to.equal(1);
            expect(sut.isSelected()).to.be.false;
        });
        it('sets a group to deselected if its key is not in the list', function () {
            var selected_current_key = 'cur';
            var selected_list_key = 'foo';
            var selected_value = 'bar';
            var group = CommonResources_1.createGroup('foo', [], [], false);
            var state_manager = CommonResources_1.createStateManager();
            var content = CommonResources_1.createContent(true);
            var box_content = CommonResources_1.createContent(true);
            box_content.querySelector = function () { return content; };
            var quote = CommonResources_1.createQuote();
            quote.getDataByName.withArgs(selected_list_key).returns(['']);
            quote.getDataByName
                .withArgs(selected_current_key)
                .returns([selected_list_key]);
            content.getAttribute
                .withArgs('data-selected-current-key')
                .returns(selected_current_key);
            content.getAttribute
                .withArgs('data-selected-list-key')
                .returns(selected_list_key);
            content.getAttribute
                .withArgs('data-selected-value')
                .returns(selected_value);
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, {
                content: box_content,
                state_manager: state_manager,
                group: group,
            });
            sut.init(quote);
            sut.select();
            sut.visit();
            expect(sut.isSelected()).to.be.false;
        });
        it('does not set a group to deselected if disabled state is true as an internal user', function () {
            var selected_current_key = 'cur';
            var selected_list_key = 'foo';
            var selected_value = 'bar';
            var mock_data = [selected_value];
            var expected_data = {
                foo: [null],
                cur: [],
            };
            var set_data_calls = 0;
            var group = CommonResources_1.createGroup('foo', [], [], true);
            var state_manager = CommonResources_1.createStateManager();
            var content = CommonResources_1.createContent(true);
            var box_content = CommonResources_1.createContent(true);
            box_content.querySelector = function () { return content; };
            var quote = CommonResources_1.createQuote();
            quote.getDataByName.withArgs(selected_list_key).returns(mock_data);
            quote.getDataByName.withArgs(selected_current_key).returns(mock_data);
            quote.setData = function (data) {
                set_data_calls++;
                expect(data).to.deep.equal(expected_data);
            };
            content.getAttribute
                .withArgs('data-selected-current-key')
                .returns(selected_current_key);
            content.getAttribute
                .withArgs('data-selected-list-key')
                .returns(selected_list_key);
            content.getAttribute
                .withArgs('data-selected-value')
                .returns(selected_value);
            // Mock state manager sets disabled state
            state_manager.observes = sinon.stub().returns(true);
            state_manager.is = sinon.stub().returns(true);
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, {
                content: box_content,
                state_manager: state_manager,
                group: group,
            });
            sut.init(quote);
            // Simulate calling select first
            sut.select();
            sut.visit();
            expect(set_data_calls).to.equal(0);
        });
    });
    describe('select', function () {
        [
            {
                label: 'updates bucket data with selected value, multiple selected',
                selected_current_key: 'view',
                selected_list_key: 'name',
                selected_value: 'foo-bar',
                existing_list_value: ['baz'],
                expected_set_data_calls: 1,
                expected_data: {
                    name: ['baz', 'foo-bar', null],
                    view: ['foo-bar'],
                },
            },
            {
                label: 'updates bucket data with selected value, single selected',
                selected_current_key: 'view',
                selected_list_key: 'name',
                selected_value: 'bar',
                existing_list_value: [''],
                expected_set_data_calls: 1,
                expected_data: {
                    name: ['bar', null],
                    view: ['bar'],
                },
            },
            {
                label: 'does not update bucket data with missing data attributes',
                selected_current_key: '',
                selected_list_key: '',
                selected_value: '',
                existing_list_value: [''],
                expected_set_data_calls: 0,
                expected_data: {},
            },
        ].forEach(function (_a) {
            var label = _a.label, selected_current_key = _a.selected_current_key, selected_list_key = _a.selected_list_key, selected_value = _a.selected_value, existing_list_value = _a.existing_list_value, expected_data = _a.expected_data, expected_set_data_calls = _a.expected_set_data_calls;
            it(label, function () {
                var set_data_calls = 0;
                var content = CommonResources_1.createContent();
                var box_content = CommonResources_1.createContent();
                box_content.querySelector = function () { return content; };
                var quote = CommonResources_1.createQuote();
                content.getAttribute
                    .withArgs('data-selected-current-key')
                    .returns(selected_current_key);
                content.getAttribute
                    .withArgs('data-selected-list-key')
                    .returns(selected_list_key);
                content.getAttribute
                    .withArgs('data-selected-value')
                    .returns(selected_value);
                box_content.classList.contains = sinon.stub().returns(true);
                var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: box_content });
                sut.init(quote);
                sut.visit();
                quote.setData = function (data) {
                    set_data_calls++;
                    expect(data).to.deep.equal(expected_data);
                };
                sut.select(existing_list_value);
                expect(set_data_calls).to.equal(expected_set_data_calls);
            });
        });
    });
    describe('deselect', function () {
        [
            {
                label: 'updates bucket data to remove selected value, multiple selected',
                selected_current_key: 'view',
                selected_list_key: 'name',
                selected_value: 'foo-bar',
                existing_current_value: ['baz'],
                existing_list_value: ['baz'],
                expected_set_data_calls: 1,
                expected_data: {
                    name: ['baz', null],
                    view: ['baz'],
                },
            },
            {
                label: 'updates bucket data to remove selected value, single selected',
                selected_current_key: 'view',
                selected_list_key: 'name',
                selected_value: 'foo-bar',
                existing_current_value: [''],
                existing_list_value: [''],
                expected_set_data_calls: 1,
                expected_data: {
                    name: [null],
                    view: [''],
                },
            },
            {
                label: 'does not update bucket data with undefined selected values',
                selected_current_key: 'view',
                selected_list_key: 'name',
                selected_value: 'foo-bar',
                existing_current_value: [''],
                existing_list_value: undefined,
                expected_set_data_calls: 0,
                expected_data: {},
            },
            {
                label: 'does not update bucket data with missing data attributes',
                selected_current_key: '',
                selected_list_key: '',
                selected_value: '',
                existing_current_value: [''],
                existing_list_value: [''],
                expected_set_data_calls: 0,
                expected_data: {},
            },
        ].forEach(function (_a) {
            var label = _a.label, selected_current_key = _a.selected_current_key, selected_list_key = _a.selected_list_key, selected_value = _a.selected_value, existing_current_value = _a.existing_current_value, existing_list_value = _a.existing_list_value, expected_data = _a.expected_data, expected_set_data_calls = _a.expected_set_data_calls;
            it(label, function () {
                var set_data_calls = 0;
                var content = CommonResources_1.createContent();
                var box_content = CommonResources_1.createContent();
                box_content.querySelector = function () { return content; };
                var quote = CommonResources_1.createQuote();
                content.getAttribute
                    .withArgs('data-selected-current-key')
                    .returns(selected_current_key);
                content.getAttribute
                    .withArgs('data-selected-list-key')
                    .returns(selected_list_key);
                content.getAttribute
                    .withArgs('data-selected-value')
                    .returns(selected_value);
                var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: box_content });
                sut.init(quote);
                sut.visit();
                // Simulate calling select first
                sut.select();
                quote.getDataByName
                    .withArgs(selected_current_key)
                    .returns(existing_current_value);
                quote.setData = function (data) {
                    set_data_calls++;
                    expect(data).to.deep.equal(expected_data);
                };
                sut.deselect(existing_list_value);
                expect(set_data_calls).to.equal(expected_set_data_calls);
            });
        });
    });
    describe('areDetailsOpen', function () {
        it('detects when the details pane is open', function () {
            var content = CommonResources_1.createBoxContent();
            content.classList.contains = sinon.stub().returns(false);
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: content });
            expect(sut.areDetailsOpen()).to.be.false;
            sut.init(CommonResources_1.createQuote());
            sut.visit();
            content.classList.contains = sinon.stub().returns(true);
            expect(sut.areDetailsOpen()).to.be.true;
        });
        it('detects when the group is closed', function () {
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: CommonResources_1.createBoxContent() });
            expect(sut.areDetailsOpen()).to.be.false;
            sut.init(CommonResources_1.createQuote());
            sut.visit();
            expect(sut.areDetailsOpen()).to.be.false;
        });
    });
    describe('#onClassify', function () {
        it('processes classes when called', function () {
            var content = CommonResources_1.createBoxContent();
            var quote = CommonResources_1.createQuote();
            var sut = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: content });
            var contains_called = false;
            var remove_called = false;
            var quote_classify_cb = function (_) { };
            quote.onClassifyAndNow = function (cb) {
                quote_classify_cb = cb;
            };
            content.classList = {
                contains: function (class_str) {
                    contains_called = true;
                    if (class_str === 'is-visible') {
                        return true;
                    }
                    return false;
                },
                add: function () { },
                remove: function () {
                    remove_called = true;
                },
            };
            sut.init(quote);
            // Defaults to false
            expect(sut.isVisible()).to.be.false;
            quote_classify_cb({});
            // Classes were processed after classification
            expect(sut.isVisible()).to.be.true;
            expect(contains_called).to.be.true;
            expect(remove_called).to.be.true;
        });
    });
});
//# sourceMappingURL=data:application/json;base64, 