import { IStoreState } from '../../index.data'; import { setSelection, selectCellAbove } from './selection'; describe('setSelection', () => { const state = {} as IStoreState; it('选行1', () => { setSelection(state, 'row', 1, true); expect(setSelection(state, 'row', 1, true)).toEqual({ selectionType: 'row', selectedRowRange: [1, 1], mode: 'view', }); }); it('选行1, 之后再选行1', () => { const state1 = setSelection(state, 'row', 1, true) as IStoreState; expect(setSelection(state1, 'row', 1)).toEqual({}); }); it('选行1, 再选行2', () => { const state1 = setSelection(state, 'row', 1, true) as IStoreState; expect(setSelection(state1, 'row', 2)).toEqual({ selectedRowRange: [1, 2], }); }); it('选单元格1', () => { expect(setSelection(state, 'cell', 1, true)).toEqual({ selectionType: 'cell', selectedCellRange: [1, 1], mode: 'view', }); }); it('选单元格1, 再选1', () => { const state1 = setSelection(state, 'cell', 1, true) as IStoreState; expect(setSelection(state1, 'cell', 1)).toEqual({}); }); it('选单元格1, 再选2', () => { const state1 = setSelection(state, 'cell', 1, true) as IStoreState; expect(setSelection(state1, 'cell', 2)).toEqual({ selectedCellRange: [1, 2], }); }); it('选行1, 再选单元格2, 将强制clear', () => { const state1 = setSelection( { selectedCellRange: [5, 5] } as IStoreState, 'row', 1, ) as IStoreState; expect(setSelection(state1, 'cell', 2)).toEqual({ selectionType: 'cell', selectedCellRange: [2, 2], }); }); }); describe('selectCellAbove', () => { const origin = { rows: [{ id: 1, cellIds: [1] }, { id: 2, cellIds: [2] }, { id: 3, cellIds: [3] }], cells: { 1: { id: 1, rowId: 1, i: 0, j: 0 }, 2: { id: 2, rowId: 2, i: 1, j: 0 }, 3: { id: 3, rowId: 3, i: 2, j: 0 }, }, }; it('case0: 首行单元格, 选择不变', () => { const state = { ...origin, selectionType: 'row' }; expect(selectCellAbove(state as any)).toEqual(undefined); }); it('case1: 第2个单元格', () => { const state = { ...origin, columns: new Array(1), selectionType: 'cell', selectedCellRange: [2, 2], }; const expected = { selectedCellRange: [1, 1], mode: 'view', }; expect(selectCellAbove(state as any)).toEqual(expected); }); it('case2: 选择集为多个单元格', () => { const state = { ...origin, selectionType: 'cell', selectedCellRange: [1, 2], }; expect(selectCellAbove(state as any)).toEqual(undefined); }); });