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 { sliceBy, mockResizeObserver } from 'valor-app-utils';
import { getCellsInfo } from '../helper';

mockResizeObserver();

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

describe('<InsertTreeRowCommand/>', () => {
  it('插平级/undo', () => {
    const wrapper = mount(<Story env="test" />);
    const sheetWrapper = wrapper.find(SpreadSheet).at(0);
    // 选中 cell11
    const spreadSheetInstance = sheetWrapper.instance() as any;
    // 选中前必须先计算行列宽度
    spreadSheetInstance.runtime.fsmService.send('MOUSE.DOWN', { selectedCell: 11 });

    setTimeout(() => {
      // 第一步: 插入平级
      wrapper
        .find('#insertSibling')
        .at(0)
        .simulate('click');
      // 注意: 在计算后, 必须重新从wrapper开始查找
      let cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([11, 100, 12]);
      expect(cellInfo.resultValues).toEqual([1, 100, 1]);

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

      wrapper
        .find('#redo')
        .at(0)
        .simulate('click');
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([11, 100, 12]);
      expect(cellInfo.resultValues).toEqual([1, 100, 1]);
    });
  });

  it('插下级/undo', () => {
    const wrapper = mount(<Story env="test" />);
    const sheetWrapper = wrapper.find(SpreadSheet).at(0);
    // 选中 cell11
    const spreadSheetInstance = sheetWrapper.instance() as any;
    // 选中前必须先计算行列宽度
    spreadSheetInstance.runtime.fsmService.send('MOUSE.DOWN', { selectedCell: 11 });

    setTimeout(() => {
      // 第一步: 插入平级
      wrapper
        .find('#insertChild')
        .at(0)
        .simulate('click');
      const expectedIds = [11, 100, 12];
      const expectedValues = [100, 100, 1];

      // 注意: 在计算后, 必须重新从wrapper开始查找
      let cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual(expectedIds);
      expect(cellInfo.resultValues).toEqual(expectedValues);

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

      wrapper
        .find('#redo')
        .at(0)
        .simulate('click');
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([11, 100, 12]);
      expect(cellInfo.resultValues).toEqual([100, 100, 1]);
    });
  });
});
