"use strict";
/**
 * Test case for GridCollection
 *
 *  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 sinon = require('sinon');
var jsdom = require('jsdom');
var JSDOM = jsdom.JSDOM;
var dom = new JSDOM('<!DOCTYPE html>');
var GridGroupUi_1 = require("../../../src/ui/group/GridGroupUi");
var GridCollection_1 = require("../../../src/ui/step/GridCollection");
var chai_1 = require("chai");
var CommonResources_1 = require("../group/CommonResources");
describe('ui.step.GridCollection', function () {
    describe('visit', function () {
        it('closes all groups details pane and passes AncestorAwareStyler', function () {
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            var styler_stub = getStylerStub();
            var close_calls = 0;
            groups[0].closeDetails = function (styler) {
                close_calls++;
                chai_1.expect(styler).to.equal(styler_stub);
            };
            groups[1].closeDetails = function (styler) {
                close_calls++;
                chai_1.expect(styler).to.equal(styler_stub);
            };
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), styler_stub, dom);
            sut.visit();
            chai_1.expect(close_calls).to.equal(2);
        });
        [
            {
                columns: 1,
                input: ['column1'],
                expected_class_added: 'col-1',
                expected_class_removed: 'col-99',
            },
            {
                columns: 2,
                input: ['column1', 'column2', 'column1', 'column2'],
                expected_class_added: 'col-2',
                expected_class_removed: 'col-0',
            },
            {
                columns: 3,
                input: ['column1', 'column2', 'column3'],
                expected_class_added: 'col-3',
                expected_class_removed: 'col-99',
            },
            {
                columns: 4,
                input: ['column1', 'column2', 'column3', 'column4'],
                expected_class_added: 'col-4',
                expected_class_removed: 'col-99',
            },
        ].forEach(function (data) {
            var columns = data.columns, input = data.input, expected_class_added = data.expected_class_added, expected_class_removed = data.expected_class_removed;
            it("sets the css class for " + columns + " columns", function () {
                var groups = input.map(function (xType, index) {
                    var boxContent = CommonResources_1.createBoxContent();
                    boxContent.classList.contains = sinon.stub().returns(true);
                    var group = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: boxContent });
                    group.init(CommonResources_1.createQuote());
                    group.getGroupId = function () { return index.toString(); };
                    group.isVisible = function () { return true; };
                    group.getXType = function () { return xType; };
                    return group;
                });
                var content = createHtmlElement();
                var add_column_class = '';
                var actual_column_removed = '';
                sinon.stub(content, 'querySelectorAll').callsFake(function () {
                    return groups.map(function (_child, index) {
                        var elem = createHtmlElement();
                        elem.getAttribute = function () { return index.toString(); };
                        return elem;
                    });
                });
                content.classList.contains = sinon.stub().returns(false);
                content.classList.add(expected_class_removed);
                content.classList.add = function (classname) {
                    add_column_class = classname;
                };
                content.classList.remove = function (class_name) {
                    actual_column_removed = class_name;
                };
                var sut = new GridCollection_1.GridCollection(content, convertToGroupList(groups), getStylerStub(), dom);
                sut.visit();
                chai_1.expect(add_column_class).to.equal(expected_class_added);
                chai_1.expect(actual_column_removed).to.equal(expected_class_removed);
            });
        });
        it("sets the css class when some columns aren't visible", function () {
            [
                // One column that is not visible
                {
                    input: ['column1'],
                    visibility: [false],
                    expected_class_added: 'col-0',
                },
                // Four columns but only two are visible
                {
                    input: ['column1', 'column2', 'column3', 'column4'],
                    visibility: [true, false, false, true],
                    expected_class_added: 'col-2',
                },
            ].forEach(function (data) {
                var input = data.input, visibility = data.visibility, expected_class_added = data.expected_class_added;
                var groups = input.map(function (xType, index) {
                    var boxContent = CommonResources_1.createBoxContent();
                    boxContent.classList.contains = sinon.stub().returns(true);
                    var group = CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: boxContent });
                    group.getGroupId = function () { return index.toString(); };
                    group.isVisible = function () { return visibility[index]; };
                    group.getXType = function () { return xType; };
                    return group;
                });
                var content = createHtmlElement();
                var add_column_class = '';
                sinon.stub(content, 'querySelectorAll').callsFake(function () {
                    return groups.map(function (_child, index) {
                        var elem = createHtmlElement();
                        elem.getAttribute = function () { return index.toString(); };
                        return elem;
                    });
                });
                content.classList.add = function (classname) {
                    add_column_class = classname;
                };
                var sut = new GridCollection_1.GridCollection(content, convertToGroupList(groups), getStylerStub(), dom);
                sut.visit();
                chai_1.expect(add_column_class).to.equal(expected_class_added);
            });
        });
    });
    describe('handleClick', function () {
        var actual = [];
        var overrideGroupSelection = function (groups) {
            ['select', 'deselect', 'openDetails', 'closeDetails'].forEach(function (event) {
                groups.forEach(function (group, index) {
                    sinon.stub(group, event).callsFake(function () {
                        actual.push(event + " grid group " + index);
                    });
                });
            });
            groups.forEach(function (group) {
                sinon.stub(group, 'isSelected').returns(false);
                sinon.stub(group, 'getCategories').returns([]);
                sinon.stub(group, 'areDetailsOpen').returns(false);
            });
        };
        beforeEach(function () {
            actual = [];
        });
        it('step lock disables click', function () {
            var expected = ['closeDetails grid group 0', 'closeDetails grid group 1'];
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            overrideGroupSelection(groups);
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), getStylerStub(), dom);
            sut.visit();
            sut.lock(true);
            var content = markup.querySelector('#grid1 > .content');
            if (content) {
                content.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            chai_1.expect(actual).to.deep.equal(expected);
        });
        it("causes selection when group's content is clicked", function () {
            var expected = [
                'closeDetails grid group 0',
                'closeDetails grid group 1',
                'select grid group 1',
            ];
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            overrideGroupSelection(groups);
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), getStylerStub(), dom);
            sut.visit();
            var content = markup.querySelector('#grid1 > .content');
            if (content) {
                content.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            chai_1.expect(actual).to.deep.equal(expected);
        });
        it('selected group passes selected values from previously selected groups', function () {
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            var styler_stub = getStylerStub();
            var selected_value = 'foo';
            var get_selected_calls = 0;
            var select_calls = 0;
            var expected_selected_siblings = [selected_value];
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), styler_stub, dom);
            sut.visit();
            var content = markup.querySelector('#grid1 > .content');
            groups[0].isSelected = function () {
                return true;
            };
            groups[0].getSelectedValue = function () {
                get_selected_calls++;
                return selected_value;
            };
            groups[1].isSelected = function () {
                return false;
            };
            groups[1].select = function (selected_siblings) {
                select_calls++;
                chai_1.expect(selected_siblings).to.deep.equal(expected_selected_siblings);
            };
            if (content) {
                content.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            chai_1.expect(get_selected_calls).to.equal(1);
            chai_1.expect(select_calls).to.equal(1);
        });
        it("doesn't cause selection when a disabled group is clicked", function () {
            var expected = [
                'closeDetails grid group 0',
                'closeDetails grid group 1',
            ];
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            overrideGroupSelection(groups);
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), getStylerStub(), dom);
            sut.visit();
            var group = markup.querySelector('#group_grid1');
            var content = markup.querySelector('#grid1 > .content');
            groups[1].group.isInternal = function () {
                return false;
            };
            if (group && content) {
                group.classList.add('disabled');
                content.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            chai_1.expect(actual).to.deep.equal(expected);
        });
        it("causes deselection when group's content is already selected", function () {
            var expected = [
                'closeDetails grid group 0',
                'closeDetails grid group 1',
                'select grid group 1',
                'deselect grid group 1',
            ];
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            overrideGroupSelection(groups);
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), getStylerStub(), dom);
            sut.visit();
            var content = markup.querySelector('#grid1 > .content');
            if (content) {
                content.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            groups[1].isSelected = sinon.stub().returns(true);
            if (content) {
                content.click();
            }
            chai_1.expect(actual).to.deep.equal(expected);
        });
        it('deselects a group that shares categories with the group that was clicked', function () {
            var expected = [
                'closeDetails grid group 0',
                'closeDetails grid group 1',
                'deselect grid group 0',
                'select grid group 1',
                'deselect grid group 1',
                'select grid group 0',
            ];
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            overrideGroupSelection(groups);
            groups[0].getCategories = sinon.stub().returns(['foo']);
            groups[1].getCategories = sinon.stub().returns(['foo']);
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), getStylerStub(), dom);
            sut.visit();
            var content1 = markup.querySelector('#grid1 > .content');
            if (content1) {
                content1.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            groups[1].isSelected = sinon.stub().returns(true);
            var content0 = markup.querySelector('#grid0 > .content');
            if (content0) {
                content0.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            chai_1.expect(actual).to.deep.equal(expected);
        });
        it('does not deselect a group when it does not share categories with a clicked group', function () {
            var expected = [
                'closeDetails grid group 0',
                'closeDetails grid group 1',
                'select grid group 1',
            ];
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            overrideGroupSelection(groups);
            groups[0].getCategories = sinon.stub().returns(['foo']);
            groups[1].getCategories = sinon.stub().returns(['bar']);
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), getStylerStub(), dom);
            sut.visit();
            var content1 = markup.querySelector('#grid1 > .content');
            if (content1) {
                content1.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            chai_1.expect(actual).to.deep.equal(expected);
        });
        it("opens group details when group's actions is clicked, closes other group", function () {
            var expected = [
                'closeDetails grid group 0',
                'closeDetails grid group 1',
                'closeDetails grid group 0',
                'openDetails grid group 1',
            ];
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            var styler_stub = getStylerStub();
            overrideGroupSelection(groups);
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), styler_stub, dom);
            sut.visit();
            var actions = markup.querySelector('#grid1 > .actions');
            if (actions) {
                actions.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            chai_1.expect(actual).to.deep.equal(expected);
        });
        it('clicking element with close class closes all groups', function () {
            var expected = [
                'closeDetails grid group 0',
                'closeDetails grid group 1',
                'closeDetails grid group 0',
                'closeDetails grid group 1',
            ];
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            var styler_stub = getStylerStub();
            overrideGroupSelection(groups);
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), styler_stub, dom);
            sut.visit();
            var actions = markup.querySelector('#grid1 .close');
            if (actions) {
                actions.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            chai_1.expect(actual).to.deep.equal(expected);
        });
        it('group details open and close methods pass AncestorAwareStyler', function () {
            var markup = createCollectionMarkup();
            var groups = createGroupsFromMarkup(markup);
            var styler_stub = getStylerStub();
            var open_calls = 0;
            var close_calls = 0;
            var sut = new GridCollection_1.GridCollection(markup, convertToGroupList(groups), styler_stub, dom);
            sut.visit();
            var actions = markup.querySelector('#grid0 > .actions');
            groups[0].areDetailsOpen = function () {
                return false;
            };
            groups[0].openDetails = function (styler) {
                open_calls++;
                chai_1.expect(styler).to.equal(styler_stub);
            };
            groups[1].areDetailsOpen = function () {
                return false;
            };
            groups[1].closeDetails = function (styler) {
                close_calls++;
                chai_1.expect(styler).to.equal(styler_stub);
            };
            if (actions) {
                actions.click();
            }
            else {
                throw new Error('Unable to find grid content');
            }
            chai_1.expect(open_calls).to.equal(1);
            chai_1.expect(close_calls).to.equal(1);
        });
    });
});
var createHtmlElement = function () {
    return document.createElement('div');
};
var createCollectionMarkup = function () {
    var collection = document.createElement('div');
    collection.setAttribute('class', 'collection');
    collection.setAttribute('data-collection-type', 'grid');
    collection.innerHTML = "<fieldset class=\"grid\" id=\"group_grid0\">\n            <div id=\"grid0\">\n                <section class=\"content\"></section>\n                <section class=\"actions\"></section>\n                <div class=\"details-pane\">\n                    <div class=\"buttons\">\n                        <input type=\"button\" value=\"Close\" class=\"close\">\n                    </div>\n                </div>\n            </div>\n        </fieldset>\n        <fieldset class=\"grid\" id=\"group_grid1\">\n            <div id=\"grid1\">\n                <section class=\"content\"></section>\n                <section class=\"actions\"></section>\n                <div class=\"details-pane\">\n                    <div class=\"buttons\">\n                        <input type=\"button\" value=\"Close\" class=\"close\">\n                    </div>\n                </div>\n            </div>\n        </fieldset>";
    return collection;
};
var createGroupsFromMarkup = function (markup) {
    var groups = [];
    markup.querySelectorAll('fieldset').forEach(function (fieldset) {
        groups.push(CommonResources_1.createSut(GridGroupUi_1.GridGroupUi, { content: fieldset }));
    });
    groups.forEach(function (group, index) {
        sinon.stub(group, 'getGroupId').returns('group_grid' + index);
    });
    return groups;
};
var convertToGroupList = function (groups) {
    var group_list = {};
    groups.forEach(function (group) {
        group_list['' + group.getGroupId()] = group;
    });
    return group_list;
};
var getStylerStub = function () {
    return [
        {
            style: function (_) { },
        },
    ];
};
//# sourceMappingURL=data:application/json;base64, 