import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find";
import { treeMode } from '../treeMode';
import { useTablePipeline } from '../../pipeline';
import { mount } from 'enzyme';
import { renderHook } from '@testing-library/react-hooks';

function makeChildren(prefix) {
  return [{
    id: "".concat(prefix, "-1"),
    title: '二级标题',
    dept: '应用部',
    dest: '云南大理',
    guide: 'Douglas Lee',
    children: [{
      id: "".concat(prefix, "-1-1"),
      title: '三级标题',
      dept: '平台大前端-UED',
      dest: '云南大理',
      guide: 'Douglas Lee'
    }, {
      id: "".concat(prefix, "-1-2"),
      title: '三级标题',
      dept: '平台大前端-前端',
      dest: '云南大理',
      guide: 'Douglas Lee'
    }]
  }, {
    id: "".concat(prefix, "-2"),
    title: '二级标题',
    dept: '应用部',
    dest: '云南大理',
    guide: 'Douglas Lee',
    children: [{
      id: "".concat(prefix, "-2-1"),
      title: '三级标题',
      dept: '平台大前端-UED',
      dest: '云南大理',
      guide: 'Douglas Lee'
    }, {
      id: "".concat(prefix, "-2-2"),
      title: '三级标题',
      dept: '平台大前端-前端',
      dest: '云南大理',
      guide: 'Douglas Lee'
    }]
  }, {
    id: "".concat(prefix, "-3"),
    title: '二级标题',
    dept: '应用部',
    dest: '云南大理',
    guide: 'Douglas Lee'
  }];
}

var dataSource = [{
  id: '1',
  title: '一级标题',
  dept: '云苍穹-前端',
  dest: 'South Maddison',
  guide: 'Don Moreno',
  children: makeChildren('1')
}, {
  id: '2',
  title: '一级标题',
  dept: '云苍穹-模型',
  dest: 'Emilhaven',
  guide: 'Douglas Richards',
  children: makeChildren('2')
}, {
  id: '3',
  title: '一级标题',
  dept: '云苍穹-基础',
  dest: '云南大理',
  guide: 'Douglas Lee',
  children: makeChildren('3')
}, {
  id: '4',
  title: '一级标题',
  dept: '云苍穹-体验',
  dest: '杭州千岛湖',
  guide: 'Eric Castillo',
  children: makeChildren('4')
}, {
  id: '5',
  title: '一级标题',
  dept: '云苍穹-运营',
  dest: 'East Karl',
  guide: 'Herbert Patton'
}];
var columns = [{
  code: 'title',
  name: '标题',
  width: 200,
  getCellProps: function getCellProps() {
    return {
      style: {
        background: 'red'
      }
    };
  }
}, {
  code: 'dept',
  name: '部门名称',
  width: 180
}, {
  code: 'dest',
  name: '团建目的地',
  width: 160
}, {
  code: 'guide',
  name: '当地导游',
  width: 160
}];
describe('treeMode 单元测试', function () {
  var opts = {
    openKeys: ['4', '4-2'],
    clickArea: 'content',
    onChangeOpenKeys: jest.fn()
  };
  var newPipeline = null;
  beforeEach(function () {
    var _renderHook = renderHook(function () {
      return useTablePipeline({
        primaryKey: 'id'
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook.result;

    var tablePipeline = result.current;
    newPipeline = treeMode(opts)(tablePipeline);
  });
  afterEach(function () {
    newPipeline = null;
    opts.onChangeOpenKeys.mockClear();
  });
  it('初始化展示', function () {
    // 5 + 3 + 2
    expect(newPipeline.getDataSource().length).toBe(10);
  });
  it('展开图标单元格渲染', function () {
    var col = newPipeline.getColumns();
    var wrapper = mount(col[0].render('一级标题', newPipeline.getDataSource()[0], 0));
    expect(_findInstanceProperty(wrapper).call(wrapper, 'CaretRightIcon').length).toBe(1);

    var div = _findInstanceProperty(wrapper).call(wrapper, 'div').at(0);

    div.simulate('click');
    expect(opts.onChangeOpenKeys).toHaveBeenCalled();
    expect(opts.onChangeOpenKeys).toBeCalledWith(['4', '4-2', '1'], '1', 'expand');
    opts.onChangeOpenKeys.mockClear(); // 展开列点击

    var expandColWrapper = mount(col[0].render('一级标题', newPipeline.getDataSource()[3], 3));

    var div1 = _findInstanceProperty(expandColWrapper).call(expandColWrapper, 'div').at(0);

    div1.simulate('click');
    expect(opts.onChangeOpenKeys).toBeCalledWith(['4-2'], '4', 'collapse'); // 叶子节点渲染

    var leafWrapper = mount(col[0].render('一级标题', newPipeline.getDataSource()[9], 9));
    expect(_findInstanceProperty(leafWrapper).call(leafWrapper, '.expansion-cell.leaf').length).toBe(2);
  });
  it('getCellProps', function () {
    var col = newPipeline.getColumns(); // clickArea: 'content' 取默认的cellProps

    expect(col[0].getCellProps()).toEqual({
      style: {
        background: 'red'
      }
    });
  });
  it('clickArea 为cell', function () {
    var _renderHook2 = renderHook(function () {
      return useTablePipeline({
        primaryKey: 'id'
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook2.result;

    var tablePipeline = result.current;
    opts.clickArea = 'cell';
    var newPipeline = treeMode(opts)(tablePipeline);
    var col = newPipeline.getColumns();
    var cellProps = col[0].getCellProps('一级标题', newPipeline.getDataSource()[0], 0);
    expect(cellProps.style).toEqual({
      background: 'red',
      cursor: 'pointer'
    });
    cellProps.onClick();
    expect(opts.onChangeOpenKeys).toHaveBeenCalled(); // 叶子节点cellProps

    var leafCellProps = col[0].getCellProps('一级标题', newPipeline.getDataSource()[9], 9);
    expect(leafCellProps.style).toEqual({
      background: 'red'
    });
  });
});