import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find";
import { sort } from '../sort';
import { useTablePipeline } from '../../pipeline';
import { mount } from 'enzyme';
import { renderHook } from '@testing-library/react-hooks';
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: 1000,
  cure: 428,
  dead: 0,
  t: '2020-02-15 19:52:02'
}, {
  id: '5',
  prov: '湖南省',
  confirm: 1000,
  cure: 417,
  dead: 2,
  t: '2020-02-15 19:52:02'
}];
var columns = [{
  code: 'prov',
  name: '省份',
  width: 150,
  features: {
    sortable: true
  }
}, {
  code: 'confirm',
  name: '确诊',
  width: 100,
  align: 'right',
  features: {
    sortable: true
  }
}, {
  code: 'cure',
  name: '治愈',
  width: 100,
  align: 'right',
  features: {
    sortable: true
  }
}, {
  code: 'dead',
  name: '死亡',
  width: 100,
  align: 'right',
  features: {
    sortable: true
  }
}, {
  code: 't',
  name: '更新时间',
  width: 180,
  features: {
    sortable: true
  }
}];
describe('sort 单元测试', function () {
  var opts = {
    defaultSorts: [{
      code: 'cure',
      order: 'asc'
    }],
    highlightColumnWhenActive: true,
    mode: 'single',
    keepDataSource: false,
    onChangeSorts: 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 = sort(opts)(tablePipeline);
  });
  afterEach(function () {
    newPipeline = null;
    opts.onChangeSorts.mockClear();
  });
  it('初始化数据', function () {
    // 默认cure列升序排序
    expect(newPipeline.getDataSource()[0]).toEqual({
      id: '3',
      prov: '河南省',
      confirm: 1212,
      cure: 390,
      dead: 13,
      t: '2020-02-15 19:52:02'
    });
  });
  it('列头单元格渲染', function () {
    var col = newPipeline.getColumns();
    var wrapper = mount(col[0].title);
    expect(_findInstanceProperty(wrapper).call(wrapper, 'SortIcon').length).toBe(1); // 列头单元格

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

    div.simulate('click');
    expect(opts.onChangeSorts).toBeCalledWith([{
      code: 'prov',
      order: 'desc'
    }], {
      code: 'prov',
      order: 'desc'
    });
    expect(1).toBe(1);
  });
  it('getCellProps', function () {
    var col = newPipeline.getColumns();
    var cellProps = col[2].getCellProps(4793, newPipeline.getDataSource()[0], 0);
    expect(cellProps.style).toEqual({
      background: 'var(--highlight-bgcolor)'
    });
  });
  it('keepDataSource为true,数据不变', function () {
    var _renderHook2 = renderHook(function () {
      return useTablePipeline({
        primaryKey: 'id'
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook2.result;

    var tablePipeline = result.current;
    opts.keepDataSource = true;
    var newPipeline = sort(opts)(tablePipeline);
    expect(newPipeline.getDataSource()[0].id).toBe('1');
    opts.keepDataSource = false;
  });
  it('多列排序', function () {
    var _renderHook3 = renderHook(function () {
      return useTablePipeline({
        primaryKey: 'id'
      }).input({
        dataSource: dataSource,
        columns: columns
      });
    }),
        result = _renderHook3.result;

    var tablePipeline = result.current;
    opts.defaultSorts = [{
      code: 'confirm',
      order: 'desc'
    }, {
      code: 'cure',
      order: 'asc'
    }];
    opts.mode = 'multiple';
    var newPipeline = sort(opts)(tablePipeline); // confirm 降序，cure升序

    expect(newPipeline.getDataSource()[3].confirm).toBe(1000);
    expect(newPipeline.getDataSource()[3].cure).toBe(417);
    expect(newPipeline.getDataSource()[4].confirm).toBe(1000);
    expect(newPipeline.getDataSource()[4].cure).toBe(428);
    var col = newPipeline.getColumns();
    var confirmWrapper = mount(col[1].title); // 列头单元格

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

    div.simulate('click'); // confirm 列初始是降序，点击后是升序
    // calls[0][1]表示第1次调用的第二个参数

    expect(opts.onChangeSorts.mock.calls[0][1]).toEqual({
      code: 'confirm',
      order: 'asc'
    });
    opts.onChangeSorts.mockClear();
    var cureWrapper = mount(col[2].title);

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

    div1.simulate('click'); // confirm 列初始是升序，点击后没有排序

    expect(opts.onChangeSorts.mock.calls[0][1]).toEqual({
      code: 'cure',
      order: 'none'
    });
  });
});