"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 _rowDetail = require("../rowDetail");

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

var _enzyme = require("enzyme");

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

var _testUtils = require("react-dom/test-utils");

var dataSource = [{
  id: '1',
  prov: '湖北省',
  confirm: 54406,
  cure: 4793,
  dead: 1457,
  t: '2020-02-15 19:52:02'
}, {
  id: '2',
  prov: '广东省',
  confirm: 1294,
  cure: 409,
  dead: 2,
  t: '2020-02-15 19:52:02'
}, {
  id: '3',
  prov: '河南省',
  confirm: 1212,
  cure: 390,
  dead: 13,
  t: '2020-02-15 19:52:02'
}, {
  id: '4',
  prov: '浙江省',
  confirm: 1162,
  cure: 428,
  dead: 0,
  t: '2020-02-15 19:52:02'
}, {
  id: '5',
  prov: '湖南省',
  confirm: 1001,
  cure: 417,
  dead: 2,
  t: '2020-02-15 19:52:02'
}];
var columns = [{
  code: 'prov',
  name: '省份',
  width: 150,
  features: {
    sortable: true,
    filterable: true
  }
}, {
  code: 'confirm',
  name: '确诊',
  width: 100,
  align: 'right',
  features: {
    sortable: true,
    filterable: true
  }
}, {
  code: 'cure',
  name: '治愈',
  width: 100,
  align: 'right',
  features: {
    sortable: true,
    filterable: true
  }
}, {
  code: 'dead',
  name: '死亡',
  width: 100,
  align: 'right',
  features: {
    sortable: true,
    filterable: true
  }
}, {
  code: 't',
  name: '更新时间',
  width: 180,
  features: {
    sortable: true,
    filterable: true
  }
}];
describe('rowDetail 单元测试', function () {
  var opt = {
    defaultOpenKeys: ['2'],
    clickArea: 'content',
    hasDetail: function hasDetail(row) {
      var _context;

      return (0, _includes.default)(_context = ['1', '2', '3']).call(_context, row.id);
    },
    renderDetail: jest.fn(),
    onChangeOpenKeys: jest.fn()
  };
  it('初始化', function () {
    var _renderHook = (0, _reactHooks.renderHook)(function () {
      return (0, _pipeline.useTablePipeline)({
        primaryKey: 'id'
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook.result;

    var tablePipeline = result.current;
    var newPipeline = (0, _rowDetail.rowDetail)(opt)(tablePipeline); // 默认有1行展开，所以有6行

    expect(newPipeline.getDataSource().length).toBe(6);
  });
  it('primaryKey不传', function () {
    var _renderHook2 = (0, _reactHooks.renderHook)(function () {
      return (0, _pipeline.useTablePipeline)().input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook2.result;

    var tablePipeline = result.current;
    expect(function () {
      return (0, _rowDetail.rowDetail)(opt)(tablePipeline);
    }).toThrow(Error);
  });
  it('模拟行点击事件', function () {
    var _renderHook3 = (0, _reactHooks.renderHook)(function () {
      return (0, _pipeline.useTablePipeline)({
        primaryKey: 'id'
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook3.result;

    var tablePipeline = result.current;
    var newPipeline = (0, _rowDetail.rowDetail)(opt)(tablePipeline);
    var newDataSource = newPipeline.getDataSource();
    var col = newPipeline.getColumns();
    var wrapper = (0, _enzyme.mount)(col[0].render('1', newDataSource[0], 0));
    var div = (0, _find.default)(wrapper).call(wrapper, 'div').at(0);
    expect(opt.onChangeOpenKeys).toHaveBeenCalledTimes(0);
    (0, _testUtils.act)(function () {
      div.simulate('click');
    });
    expect(opt.onChangeOpenKeys).toHaveBeenCalledTimes(1); // 点击没有展开行的行，不会触发onChange回调

    var wrapper1 = (0, _enzyme.mount)(col[0].render('4', newDataSource[4], 4));
    var div1 = (0, _find.default)(wrapper1).call(wrapper1, 'div').at(0);
    (0, _testUtils.act)(function () {
      div1.simulate('click');
    });
    expect(opt.onChangeOpenKeys).toHaveBeenCalledTimes(1);
  });
  it('设置指定列展开', function () {
    var _renderHook4 = (0, _reactHooks.renderHook)(function () {
      return (0, _pipeline.useTablePipeline)({
        primaryKey: 'id'
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook4.result;

    var tablePipeline = result.current;
    opt.expandColumnCode = 'confirm';
    var newPipeline = (0, _rowDetail.rowDetail)(opt)(tablePipeline);
    var col = newPipeline.getColumns();
    var wrapper = (0, _enzyme.mount)(col[1].render('1', newPipeline.getDataSource()[0], 0)); // 不知道为啥是两个

    expect((0, _find.default)(wrapper).call(wrapper, '.expansion-cell').length).toBe(2);
    col[0].render('1', newPipeline.getDataSource()[2], 2);
    expect(opt.renderDetail).toBeCalled();
    var spanRect = col[0].getSpanRect('1', newPipeline.getDataSource()[2], 2);
    expect(spanRect).toEqual({
      top: 2,
      bottom: 3,
      left: 0,
      right: 6
    });
    delete opt.expandColumnCode;
  });
  it('getCellProps 和getSpanRect', function () {
    var _renderHook5 = (0, _reactHooks.renderHook)(function () {
      return (0, _pipeline.useTablePipeline)({
        primaryKey: 'id'
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook5.result;

    var tablePipeline = result.current;
    opt.clickArea = 'cell';
    var newPipeline = (0, _rowDetail.rowDetail)(opt)(tablePipeline);
    var col = newPipeline.getColumns();
    var cellProps = col[0].getCellProps('1', newPipeline.getDataSource()[0], 0);
    expect(cellProps.style).toEqual({
      cursor: 'pointer'
    });
    cellProps.onClick();
    expect(opt.onChangeOpenKeys).toBeCalled(); // detail 总是成一行

    var spanRect = col[0].getSpanRect('1', newPipeline.getDataSource()[2], 2);
    expect(spanRect).toEqual({
      top: 2,
      bottom: 3,
      left: 0,
      right: 6
    });
  });
});