import { getTopRow, getNormalizedState } from './helper'; import { IRow, ICell, IStoreState } from './index.data'; import { idMap } from 'valor-app-utils'; describe('getTopRow', () => { const treeContext = { 1: { parentId: '-1', level: 1 }, 2: { parentId: 1, level: 2 }, 3: { parentId: 2, level: 3 }, }; const rows = [ { id: 1, cellIds: [1], type: 'body' }, { id: 2, cellIds: [2], type: 'body' }, { id: 3, cellIds: [3], type: 'body' }, ]; const cells = { 1: { id: 1, rowId: 1, i: 0 }, 2: { id: 2, rowId: 2, i: 1 }, 3: { id: 3, rowId: 3, i: 2 }, }; it('case0', () => { const state = { selectionType: 'row', selectedRowRange: [1, 1], treeContext, rows, cells, } as any; expect(getTopRow(state)!.id).toEqual(1); }); it('case1', () => { const state = { selectionType: 'row', selectedRowRange: [3, 3], treeContext, rows, cells, } as any; expect(getTopRow(state)!.id).toEqual(1); }); it('case2', () => { const state = { selectionType: 'row', selectedRowRange: [1, 3], treeContext, rows, cells, } as any; expect(getTopRow(state)).toEqual(null); }); it('case3', () => { const state = { selectionType: 'cell', selectedCellRange: [1, 3], treeContext, rows, cells, } as any; expect(getTopRow(state)).toEqual(null); }); it('case4', () => { const state = { selectionType: 'cell', selectedCellRange: [1, 1], treeContext, rows, cells, } as any; expect(getTopRow(state)!.id).toEqual(1); }); it('case4', () => { const state = { selectionType: 'cell', selectedCellRange: [3, 3], treeContext, rows, cells, } as any; expect(getTopRow(state)!.id).toEqual(1); }); }); function getState() { const rows: IRow[] = [ { id: 0, cellIds: [1, 2, null] }, { id: 1, cellIds: [3, null, null] }, { id: 2, cellIds: [null, null, null] }, { id: 3, cellIds: [4, 5, 6] }, ] as any; const cells: ICell[] = [ // 第1行 { id: 1, rowId: 0 }, { id: 2, rowId: 0, colspan: 2 }, // 第2行 { id: 3, rowId: 1, rowspan: 2, colspan: 3 }, // 第3行为空 // 第4行 { id: 4, rowId: 3 }, { id: 5, rowId: 3 }, { id: 6, rowId: 3 }, ]; return { cells: idMap(cells), columns: new Array(3), rows } as IStoreState; } describe('getNormalizedState', () => { const state = getState(); const newState = { ...state, rows: [ { ...state.rows[0], i: 0 }, { ...state.rows[1], i: 1 }, { ...state.rows[2], i: 2 }, { ...state.rows[3], i: 3 }, ], cells: idMap([ { ...state.cells[1], i: 0, j: 0 }, { ...state.cells[2], i: 0, j: 1 }, { ...state.cells[3], i: 1, j: 0 }, { ...state.cells[4], i: 3, j: 0 }, { ...state.cells[5], i: 3, j: 1 }, { ...state.cells[6], i: 3, j: 2 }, ]), }; it('case0', () => expect(getNormalizedState(state)).toEqual(newState)); });