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

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

describe('<DeleteRowCommand/>', () => {
  it('删除第1行', () => {
    const wrapper = mount(<Story env="test" />);
    const sheetWrapper = wrapper.find(SpreadSheet).at(0);
    // 选中第1个单元格
    const spreadSheetInstance = sheetWrapper.instance() as any;
    // 选中前必须先计算行列宽度
    spreadSheetInstance.runtime.fsmService.send('MOUSE.DOWN', { selectedCell: 1 });

    setTimeout(() => {
      wrapper
        .find('#deleteRow1')
        .at(0)
        .simulate('click');
      let cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([4]);
      expect(cellInfo.resultValues).toEqual([4]);

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

      wrapper
        .find('#redo')
        .at(0)
        .simulate('click');
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([4]);
      expect(cellInfo.resultValues).toEqual([4]);
    });
  });

  it('删除第2行', () => {
    const wrapper = mount(<Story env="test" />);
    const sheetWrapper = wrapper.find(SpreadSheet).at(0);
    // 选中第1个单元格
    const spreadSheetInstance = sheetWrapper.instance() as any;
    // 选中前必须先计算行列宽度
    spreadSheetInstance.runtime.fsmService.send('MOUSE.DOWN', { selectedCell: 2 });

    setTimeout(() => {
      wrapper
        .find('#deleteRow2')
        .at(0)
        .simulate('click');
      let cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([1, 3, 4]);
      expect(cellInfo.resultValues).toEqual([1, 3, 4]);
      wrapper
        .find('#undo')
        .at(0)
        .simulate('click');
      cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([1, 2, 3, 4]);
      expect(cellInfo.resultValues).toEqual([1, 2, 3, 4]);

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

  it('删除第4行', () => {
    const wrapper = mount(<Story env="test" />);
    const sheetWrapper = wrapper.find(SpreadSheet).at(0);
    // 选中第1个单元格
    const spreadSheetInstance = sheetWrapper.instance() as any;
    // 选中前必须先计算行列宽度
    spreadSheetInstance.runtime.fsmService.send('MOUSE.DOWN', { selectedCell: 4 });

    setTimeout(() => {
      wrapper
        .find('#deleteRow4')
        .at(0)
        .simulate('click');
      let cellInfo = getCellsInfo(wrapper);
      expect(cellInfo.resultIds).toEqual([1, 2, 3]);
      expect(cellInfo.resultValues).toEqual([1, 2, 3]);

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

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