import * as React from 'react';
import { configure, mount, ReactWrapper } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import Story from './Story';
import SpreadSheet from '../../../../SpreadSheetProvider';
import { getCellsInfo } from '../../helper';
import { sliceBy, mockResizeObserver } from 'valor-app-utils';

mockResizeObserver();

configure({ adapter: new Adapter() });

describe('<InsertColumnCommand/>', () => {
  it('j=1处插入', () => {
    const wrapper = mount(<Story env="test" />);

    let cellInfo = getCellsInfo(wrapper);
    expect(cellInfo.resultIds).toEqual([...[11, 13], ...[21]]);
    expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);

    wrapper
      .find('#insertColumnAt1')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      // 因为id是动态生成的, 所以无法判断了, 直接使用equals
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });

    wrapper
      .find('#undo')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([...[11, 13], ...[21]]);
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });

    wrapper
      .find('#redo')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });
  });
});

describe('<InsertColumnCommand/>', () => {
  it('j=2处插入', () => {
    const wrapper = mount(<Story env="test" />);

    let cellInfo = getCellsInfo(wrapper);
    expect(cellInfo.resultIds).toEqual([...[11, 13], ...[21]]);
    expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);

    wrapper
      .find('#insertColumnAt2')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      // 因为id是动态生成的, 所以无法判断了, 直接使用equals
      expect(cellInfo.resultValues).toEqual([...[1, 10, 1], ...[1, 10]]);
    });

    wrapper
      .find('#undo')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([...[11, 13], ...[21]]);
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });

    wrapper
      .find('#redo')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultValues).toEqual([...[1, 10, 1], ...[1, 10]]);
    });
  });
});

describe('<InsertColumnCommand/>', () => {
  it('j=2处插入, forceMerge', () => {
    const wrapper = mount(<Story env="test" />);

    let cellInfo = getCellsInfo(wrapper);
    expect(cellInfo.resultIds).toEqual([...[11, 13], ...[21]]);
    expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);

    wrapper
      .find('#insertColumnAt2_forceMerge')
      .at(0)
      .simulate('click');

    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      // 因为id是动态生成的, 所以无法判断了, 直接使用equals
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });

    wrapper
      .find('#undo')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([...[11, 13], ...[21]]);
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });

    wrapper
      .find('#redo')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });
  });
});

describe('<InsertColumnCommand/>', () => {
  it('j=3处插入', () => {
    const wrapper = mount(<Story env="test" />);

    let cellInfo = getCellsInfo(wrapper);
    expect(cellInfo.resultIds).toEqual([...[11, 13], ...[21]]);
    expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);

    wrapper
      .find('#insertColumnAt3')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      // 因为id是动态生成的, 所以无法判断了, 直接使用equals
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });

    wrapper
      .find('#undo')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([...[11, 13], ...[21]]);
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });

    wrapper
      .find('#redo')
      .at(0)
      .simulate('click');
    setTimeout(() => {
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultValues).toEqual([...[1, 1], ...[1]]);
    });
  });
});
