"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes"));

var _find = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/find"));

var _singleSelect = require("../singleSelect");

var _pipeline = require("../../pipeline");

var _enzyme = require("enzyme");

var _reactHooks = require("@testing-library/react-hooks");

var _react = _interopRequireDefault(require("react"));

var dataSource = [{
  id: '1',
  name: '阿里巴巴网络技术有限公司',
  amount: '600,000.00(CNY)',
  dept: '招商银行丨杭州分行',
  applier: 'James Collier'
}, {
  id: '2',
  name: '阿里巴巴网络技术有限公司',
  amount: '600,000.00(CNY)',
  dept: '建设银行丨未来科技城',
  applier: 'Philip Burke'
}, {
  id: '3',
  name: '阿里巴巴网络技术有限公司',
  amount: '600,000.00(CNY)',
  dept: '交通银行丨浙大路支行',
  applier: 'Wesley Cruz'
}, {
  id: '4',
  name: '阿里巴巴网络技术有限公司',
  amount: '600,000.00(CNY)',
  dept: '招商银行丨庆春路支行',
  applier: 'Billy Horton'
}, {
  id: '5',
  name: '阿里巴巴网络技术有限公司',
  amount: '600,000.00(CNY)',
  dept: '招商银行丨文一路分行',
  applier: 'Paul Tran'
}, {
  id: '6',
  name: '阿里巴巴网络技术有限公司',
  amount: '600,000.00(CNY)',
  dept: '农业银行丨杭州分行',
  applier: 'Anna Poole'
}];
var columns = [{
  code: 'name',
  width: 220,
  name: '公司名称'
}, {
  code: 'amount',
  width: 160,
  align: 'right',
  name: '金额'
}, {
  code: 'dept',
  width: 160,
  name: '金融机构'
}, {
  code: 'applier',
  width: 120,
  name: '申请人'
}];

function Radio(_ref) {
  var checked = _ref.checked,
      disabled = _ref.disabled,
      onChange = _ref.onChange;
  return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("input", {
    type: 'radio',
    disabled: disabled,
    checked: checked,
    onChange: onChange
  }));
}

describe('singleSelect 单元测试', function () {
  var opts = {
    value: '1',
    clickArea: 'cell',
    highlightRowWhenSelected: true,
    isDisabled: function isDisabled(row, rowIndex) {
      var _context;

      return (0, _includes.default)(_context = ['4', '5', '6']).call(_context, row.id);
    },
    onChange: jest.fn()
  };
  var newPipeline = null;
  beforeEach(function () {
    var _renderHook = (0, _reactHooks.renderHook)(function () {
      return (0, _pipeline.useTablePipeline)({
        primaryKey: 'id',
        components: {
          Radio: Radio
        }
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook.result;

    var tablePipeline = result.current;
    newPipeline = (0, _singleSelect.singleSelect)(opts)(tablePipeline);
  });
  afterEach(function () {
    newPipeline = null;
    opts.onChange.mockClear();
  });
  it('初始渲染', function () {
    var col = newPipeline.getColumns(); // 表格会在最后补一个空白列，加上单选列所以有6列

    expect(col.length).toBe(6);
    expect(col[0].code).toBe('__SELECT');
  });
  it('getCellProps', function () {
    var col = newPipeline.getColumns();
    var cellProps = col[0].getCellProps('', newPipeline.getDataSource()[0], 0);
    expect(cellProps.style).toEqual({
      cursor: 'pointer'
    });
    expect(opts.onChange).not.toHaveBeenCalled();
    cellProps.onClick();
    expect(opts.onChange).toHaveBeenCalled();
  });
  it('单元格渲染', function () {
    var col = newPipeline.getColumns();
    var wrapper = (0, _enzyme.mount)(col[0].render('', newPipeline.getDataSource()[0], 0));
    expect((0, _find.default)(wrapper).call(wrapper, 'Radio').length).toBe(1);
  });
  it('触发区域为radio', function () {
    var _renderHook2 = (0, _reactHooks.renderHook)(function () {
      return (0, _pipeline.useTablePipeline)({
        primaryKey: 'id',
        components: {
          Radio: Radio
        }
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook2.result;

    var tablePipeline = result.current;
    opts.clickArea = 'radio';
    var newPipeline = (0, _singleSelect.singleSelect)(opts)(tablePipeline);
    var col = newPipeline.getColumns();
    var wrapper = (0, _enzyme.mount)(col[0].render('', newPipeline.getDataSource()[0], 0)); // Radio 触发onChange

    var input = (0, _find.default)(wrapper).call(wrapper, 'input');
    input.simulate('change');
    expect(opts.onChange).toHaveBeenCalled();
    opts.clickArea = 'cell';
  });
  it('appendRowPropsGetter', function () {
    var props = newPipeline.getProps();
    var rowProps = props.getRowProps(newPipeline.getDataSource()[0], 0);
    expect(rowProps.className).toBe('highlight');
    expect(rowProps.style).toEqual({});
  });
  it('触发区域为row', function () {
    var _renderHook3 = (0, _reactHooks.renderHook)(function () {
      return (0, _pipeline.useTablePipeline)({
        primaryKey: 'id',
        components: {
          Radio: Radio
        }
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook3.result;

    var tablePipeline = result.current;
    opts.clickArea = 'row';
    var newPipeline = (0, _singleSelect.singleSelect)(opts)(tablePipeline);
    var props = newPipeline.getProps();
    var rowProps = props.getRowProps(newPipeline.getDataSource()[0], 0);
    expect(rowProps.style).toEqual({
      cursor: 'pointer'
    });
    rowProps.onClick();
    expect(opts.onChange).toHaveBeenCalled(); // disable的行没有样式

    var rowProps1 = props.getRowProps(newPipeline.getDataSource()[4], 4);
    expect(rowProps1.style).toEqual({});
  });
});