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

mockResizeObserver();

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

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

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

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

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

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

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

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

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

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

  it('j=4处插入, 强制向左合并', () => {
    const wrapper = mount(<Story env="test" />);

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

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

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

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