import { getCellRangeDimensions, getCellDimensions, getRowRangeDimensions } from './selection'; import { IStoreState } from '../../index.data'; import { idMap } from 'valor-app-utils'; import { ROW_INDICATOR_WIDTH } from '../../constants'; const originState: IStoreState = { cells: idMap([ { id: 'cell_1_1', rowId: 'row_1', i: 0, j: 0 }, { id: 'cell_1_2', rowId: 'row_1', i: 0, j: 1, }, { id: 'cell_1_3', rowId: 'row_1', i: 0, j: 2 }, { id: 'cell_2_1', rowId: 'row_2', i: 1, j: 0 }, { id: 'cell_2_2', rowId: 'row_2', i: 1, j: 1 }, { id: 'cell_2_3', rowId: 'row_2', i: 1, j: 2 }, { id: 'cell_3_1', rowId: 'row_3', i: 2, j: 0 }, { id: 'cell_3_2', colspan: 2, rowspan: 2, rowId: 'row_3', i: 2, j: 1 }, { id: 'cell_4_1', rowId: 'row_4', i: 3, j: 0 }, ]), rows: [ { id: 'row_1', cellIds: ['cell_1_1', 'cell_1_2', 'cell_1_3'], }, { id: 'row_2', cellIds: ['cell_2_1', 'cell_2_2', 'cell_2_3'], }, { id: 'row_3', cellIds: ['cell_3_1', 'cell_3_2', null], }, { id: 'row_4', cellIds: ['cell_4_1', null, null] }, ], columns: [{ id: 0, width: 10, j: 0 }, { id: 1, width: 10, j: 1 }, { id: 2, width: 10, j: 2 }], rowDimensions: { row_1: { top: 0, height: 10, offsetTop: 0, offsetHeight: 10 }, row_2: { top: 10, height: 10, offsetTop: 10, offsetHeight: 10 }, row_3: { top: 20, height: 10, offsetTop: 20, offsetHeight: 10 }, row_4: { top: 30, height: 10, offsetTop: 30, offsetHeight: 10 }, }, colDimensions: { 0: { left: 0, width: 10, offsetLeft: 0, offsetWidth: 10 }, 1: { left: 10, width: 10, offsetLeft: 10, offsetWidth: 10 }, 2: { left: 20, width: 10, offsetLeft: 20, offsetWidth: 10 }, }, } as any; describe('getCellDimensions', () => { it('cell_1_1, 无合并', () => { expect(getCellDimensions(originState, 'cell_1_1')).toEqual({ left: 0, top: 0, width: 10, height: 10, offsetLeft: 0, offsetTop: 0, offsetWidth: 10, offsetHeight: 10, }); }); it('cell_3_2, 合并', () => { expect(getCellDimensions(originState, 'cell_3_2')).toEqual({ left: 10, top: 20, width: 20, height: 20, offsetLeft: 10, offsetTop: 20, offsetWidth: 20, offsetHeight: 20, }); }); }); describe('getRowRangeDimensions', () => { it('一行', () => { expect(getRowRangeDimensions(originState, ['row_1', 'row_1'])).toEqual({ left: 0, top: 0, width: 30, height: 10, offsetLeft: 0, offsetTop: 0, offsetWidth: 30, offsetHeight: 10, }); }); it('二行', () => { expect(getRowRangeDimensions(originState, ['row_1', 'row_2'])).toEqual({ left: 0, top: 0, width: 30, height: 20, offsetLeft: 0, offsetTop: 0, offsetWidth: 30, offsetHeight: 20, }); }); it('二行, 倒排', () => { expect(getRowRangeDimensions(originState, ['row_3', 'row_2'])).toEqual({ left: 0, top: 10, width: 30, height: 20, offsetLeft: 0, offsetTop: 10, offsetWidth: 30, offsetHeight: 20, }); }); }); describe('getCellRangeDimensions', () => { it('只选cell_1_1', () => { const state: Partial = originState; const expected = { left: 0, top: 0, width: 10, height: 10, offsetLeft: 0, offsetTop: 0, offsetWidth: 10, offsetHeight: 10, }; expect(getCellRangeDimensions(state as any, ['cell_1_1', 'cell_1_1'])).toEqual(expected); }); it('选cell_1_2 - cell_2_3', () => { const state: Partial = originState; const expected = { left: 10, top: 0, width: 20, height: 20, offsetLeft: 10, offsetTop: 0, offsetWidth: 20, offsetHeight: 20, }; expect(getCellRangeDimensions(state as any, ['cell_1_2', 'cell_2_3'])).toEqual(expected); }); it('选cell_2_3 - cell_1_2, 注意是反序的', () => { const state: Partial = originState; const expected = { left: 10, top: 0, width: 20, height: 20, offsetLeft: 10, offsetTop: 0, offsetWidth: 20, offsetHeight: 20, }; expect(getCellRangeDimensions(state as any, ['cell_2_3', 'cell_1_2'])).toEqual(expected); }); it('选cell_3_2 - cell_3_2, 合并单元格', () => { const state: Partial = originState; const expected = { left: 10, top: 20, width: 20, height: 20, offsetLeft: 10, offsetTop: 20, offsetWidth: 20, offsetHeight: 20, }; expect(getCellRangeDimensions(state as any, ['cell_3_2', 'cell_3_2'])).toEqual(expected); }); it('选cell_1_1 - cell_3_2, 其中3_1带rowspan, colspan', () => { const state: Partial = originState; const expected = { left: 0, top: 0, width: 30, height: 40, offsetLeft: 0, offsetTop: 0, offsetWidth: 30, offsetHeight: 40, }; expect(getCellRangeDimensions(state as any, ['cell_1_1', 'cell_3_2'])).toEqual(expected); }); });