import { IRow, ICell, IStoreState } from '../../index.data'; import { idMap } from 'valor-app-utils'; import { IRowWithCell } from '../../SpreadSheetProvider/index.data'; import { inferHostRowsByInsert, insertRow } from './flattenRow'; describe('insertRow', () => { const rows: IRow[] = [ { id: 1, level: 1, i: 0, type: 'body', area: 'flatten', cellIds: [1], }, ]; const cells: ICell[] = [ { id: 1, rowId: 1, i: 0, j: 0, value: 1, formulaDisabled: true, }, ]; const state = ({ columns: [{ type: 'text' }], rows, cells: idMap(cells), treeContext: { 1: { childrenIds: [] } }, } as any) as IStoreState; const rowsToInsert: IRowWithCell[] = [ { id: 100, i: 0, type: 'body', area: 'flatten', cells: [ { id: 100, rowId: 100, i: 0, j: 0, value: 100, formulaDisabled: false, formula: '{{id101}}+1', type: 'numeric', }, ], }, { id: 101, level: 2, i: 1, type: 'body', area: 'flatten', cells: [{ id: 101, rowId: 101, i: 1, j: 0, value: 100, type: 'numeric' }], }, ]; it('case0:插入2行, 其中一行有公式', () => { const result = insertRow(state, 0, rowsToInsert, undefined as any); const expected = { rows: [ { id: 1, level: 1, i: 0, type: 'body', area: 'flatten', cellIds: [1], }, { id: 100, i: 1, type: 'body', area: 'flatten', cellIds: [100], }, { id: 101, level: 2, i: 2, type: 'body', area: 'flatten', cellIds: [101] }, ], cells: { 1: { id: 1, rowId: 1, i: 0, j: 0, value: 1, formulaDisabled: true, }, 100: { id: 100, rowId: 100, i: 1, j: 0, value: 101, formulaDisabled: false, formula: '{{id101}}+1', error: null, type: 'numeric', }, 101: { id: 101, rowId: 101, i: 2, j: 0, value: 100, type: 'numeric', }, }, cellDeps: { 101: [100], }, varDeps: {}, }; expect(result).toEqual(expected); }); }); describe('inferHostRows', () => { const rows = [ { id: 1, i: 0, type: 'body', area: 'flatten', cellIds: [1] }, // 1=sum(2:3) { id: 2, i: 1, type: 'body', area: 'flatten', cellIds: [2] }, // { id: 3, i: 2, type: 'body', area: 'flatten', cellIds: [3] }, // { id: 4, i: 3, type: 'body', area: 'flatten', cellIds: [4] }, // { id: 5, i: 4, type: 'body', area: 'flatten', cellIds: [5] }, // { id: 6, i: 5, type: 'body', area: 'flatten', cellIds: [6] }, // 6=sum(1,4,5) ]; const cells = { 1: { rowId: 1, i: 0, j: 0 }, 2: { rowId: 2, i: 1, j: 0 }, 3: { rowId: 3, i: 2, j: 0 }, 4: { rowId: 4, i: 3, j: 0 }, 5: { rowId: 5, i: 4, j: 0 }, 6: { rowId: 6, i: 5, j: 0 }, }; const state = { rows, cells, cellDeps: { 1: [6], 2: [1, 6], 3: [1, 6], 4: [6], 5: [6], }, } as any; it('inferHostRowByInsert', () => { expect(inferHostRowsByInsert(state, 0, [])).toEqual([6]); //实际应是[], 判断6&1其实是多余的 expect(inferHostRowsByInsert(state, 1, [])).toEqual([1, 6]); expect(inferHostRowsByInsert(state, 1, [10])).toEqual([1, 6, 10]); }); });