/**
 * Test case for TableGroupUi
 *
 *  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/>.
 */

const expect = require('chai').expect,
  sinon = require('sinon');

import {TableGroupUi as Sut} from '../../../src/ui/group/TableGroupUi';
import {
  createSut,
  createJqueryContent,
  createGroup,
  createQuote,
} from './CommonResources';

describe('ui.group.TableGroupUi', () => {
  describe('#hideAddRemove', function () {
    [
      {
        label: 'value is true, hide add button',
        row_count: 3,
        max_rows: 3,
        value: true,
        add_row_hide_expected: true,
        add_row_show_expected: false,
        icon_close_hide_expected: true,
        icon_close_show_expected: false,
      },
      {
        label:
          'value is false, show add button when tab count is less than max count',
        row_count: 2,
        max_rows: 3,
        value: false,
        add_row_hide_expected: false,
        add_row_show_expected: true,
        icon_close_hide_expected: false,
        icon_close_show_expected: true,
      },
      {
        label: 'value is false, show add button when max is 0',
        row_count: 2,
        max_rows: 0,
        value: false,
        add_row_hide_expected: false,
        add_row_show_expected: true,
        icon_close_hide_expected: false,
        icon_close_show_expected: true,
      },
      {
        label:
          'value is false, do not show button when max is equal to tab count',
        row_count: 3,
        max_rows: 3,
        value: false,
        add_row_hide_expected: false,
        add_row_show_expected: false,
        icon_close_hide_expected: false,
        icon_close_show_expected: true,
      },
    ].forEach(
      ({
        label,
        row_count,
        max_rows,
        value,
        add_row_hide_expected,
        add_row_show_expected,
        icon_close_hide_expected,
        icon_close_show_expected,
      }) => {
        it(label, () => {
          let add_row_hide_called = false;
          let add_row_show_called = false;
          let icon_close_hide_called = false;
          let icon_close_show_called = false;

          const addRow = {
            click: sinon.stub(),
            hide: () => {
              add_row_hide_called = true;
              return;
            },
            show: () => {
              add_row_show_called = true;
              return;
            },
          };

          const del_row = {
            hide: () => {
              icon_close_hide_called = true;
              return;
            },
            show: () => {
              icon_close_show_called = true;
              return;
            },
          };

          const table = {find: sinon.stub()};
          table.find.withArgs('.delrow').returns(del_row);
          table.find.withArgs('tbody > tr').returns({length: row_count});

          const $content = createJqueryContent();
          $content.find.withArgs('.groupTable > tbody > tr:first').returns({
            each: sinon.stub(),
          });
          $content.find.withArgs('table.groupTable').returns(table);
          $content.find.withArgs('table').returns({hasClass: sinon.stub()});
          $content.find.withArgs('.delrow').returns(del_row);
          $content.find.withArgs('.action').returns({live: sinon.stub()});
          $content.find.withArgs('.addrow:first').returns(addRow);

          const group = createGroup('foo', [], [], false, '', max_rows);
          const sut = createSut(Sut, {
            $content: $content,
            group: group,
          });

          const quote = createQuote();
          sut.init(quote);

          sut.hideAddRemove(value);

          expect(add_row_hide_called).to.equal(add_row_hide_expected);
          expect(add_row_show_called).to.equal(add_row_show_expected);

          expect(icon_close_hide_called).to.equal(icon_close_hide_expected);
          expect(icon_close_show_called).to.equal(icon_close_show_expected);
        });
      }
    );
  });
});
