import { fillRowNoCellValue, tryEnableFormula, setCellData, valuePropagation } from './cell'; import { IRow, ICell, IStoreState, ICellNN } from '../../index.data'; import { IRowWithCell } from '../../SpreadSheetProvider/index.data'; import { insertIndex, updateBy } from 'valor-app-utils'; import * as R from 'rambda'; describe('fillRowNoCellValue', () => { it('case0', () => { const state = { columns: [ { type: 'row-no', }, { type: 'text', }, ], rows: [ { id: 1, level: 1, type: 'header', cellIds: [1, 2], }, { id: 2, level: 1, type: 'body', cellIds: [3, 4], }, { id: 3, level: 2, type: 'body', cellIds: [5, 6], }, { id: 4, level: 1, type: 'body', cellIds: [7, 8], }, ], cells: { 1: { rowId: 1, j: 0 }, 2: { rowId: 1, j: 1 }, 3: { rowId: 2, j: 0 }, 4: { rowId: 2, j: 1 }, 5: { rowId: 3, j: 0 }, 6: { rowId: 3, j: 1 }, 7: { rowId: 4, j: 0 }, 8: { rowId: 4, j: 1 }, }, treeContext: { 2: { childrenIds: [3], path: [0] }, 3: { childrenIds: [], path: [0, 0] }, 4: { childrenIds: [], path: [1] }, }, }; expect(fillRowNoCellValue(state as any).cells).toEqual({ 1: { rowId: 1, j: 0 }, 2: { rowId: 1, j: 1 }, 3: { rowId: 2, j: 0, value: { isLeaf: false, expanded: true, path: [0] } }, 4: { rowId: 2, j: 1 }, 5: { rowId: 3, j: 0, value: { isLeaf: true, expanded: true, path: [0, 0] } }, 6: { rowId: 3, j: 1 }, 7: { rowId: 4, j: 0, value: { isLeaf: true, expanded: true, path: [1] } }, 8: { rowId: 4, j: 1 }, }); }); }); describe('tryEnableFormula', () => { const rows: IRow[] = [ { id: 1, cellIds: [11, null, 12], type: 'body' }, { id: 2, cellIds: [21, 22, 23], type: 'body' }, { id: 3, cellIds: [31, 32, 33], type: 'body' }, ]; const treeContext = { 1: { childrenIds: [2] }, 2: { childrenIds: [3] }, 3: {} }; const cells: { [id: string]: ICell } = { 11: { id: 11, rowId: 1, j: 0, formula: 'x_sumChildren()', formulaDisabled: true, }, 12: { id: 12, rowId: 1, j: 1, formula: 'x_sumChildren()', formulaDisabled: false, }, 21: { id: 21, rowId: 2, j: 0, formula: 'x_sumChildren()', formulaDisabled: true, }, 22: { id: 22, rowId: 2, j: 1, formula: 'x_sumChildren()', formulaDisabled: false, }, 23: { id: 23, rowId: 2, j: 1, formula: '1+1', formulaDisabled: false, }, 31: { id: 31, rowId: 3, j: 0, formula: 'x_sumChildren()', formulaDisabled: true, value: 3, }, 32: { id: 32, rowId: 3, j: 1, formula: 'x_sumChildren()', formulaDisabled: false, }, 33: { id: 33, rowId: 3, j: 2, formula: '1+1', formulaDisabled: false, }, 41: { id: 41, rowId: 4, j: 0, formula: 'x_sumChildren()', formulaDisabled: true, }, 42: { id: 42, rowId: 4, j: 1, formula: 'x_sumChildren()', formulaDisabled: false, }, }; const state = { rows, treeContext, cells }; const normalizeRow = (rows: IRow[], row: IRowWithCell): IRowWithCell => { return { ...row, cells: R.update(1, { ...row.cells[1], formula: '[2]' }, row.cells as any) as ICell[], }; }; it('case0, 叶节点公式', () => { expect(tryEnableFormula(state as any, [3], { normalizeRow })).toEqual({ cells: { 32: { ...cells[32], formula: '[2]' }, }, rows: [], }); }); }); describe('setCellData', () => { const rows: IRow[] = [{ id: 1, cellIds: [11, 12, 13, 14] }]; const treeContext = { 1: {} }; const cells: { [id: string]: ICell } = { 11: { id: 11, rowId: 1, j: 0, value: 2, formula: 'k+{{id12}}+{{id13}}', formulaDisabled: false, }, 12: { id: 12, rowId: 1, j: 1, value: 1, formulaDisabled: false }, 13: { id: 13, rowId: 1, j: 2, value: 1, formulaDisabled: false }, 14: { id: 14, rowId: 1, j: 3, value: 0 }, }; const state = { rows, treeContext, cells, varDeps: { k: [11] }, cellDeps: { 12: [11], 13: [11] }, variables: { k: 0, j: 10 }, } as any; it('不改公式', () => { expect( setCellData( state, 11, { formula: 'k+{{id12}}+{{id13}}' }, undefined as any, undefined as any, ), ).toEqual({}); }); it('禁用公式', () => { expect( setCellData(state, 11, { formulaDisabled: true }, undefined as any, undefined as any), ).toEqual({ cells: { ...cells, 11: { id: 11, rowId: 1, j: 0, value: 2, formula: 'k+{{id12}}+{{id13}}', formulaDisabled: true, }, }, varDeps: { k: [] }, cellDeps: { 12: [], 13: [] }, }); }); it('改公式变量, 将更新value和varDeps', () => { expect( setCellData( state, 11, { formula: 'j+{{id12}}+{{id13}}' }, undefined as any, undefined as any, ), ).toEqual({ cells: { ...cells, 11: { id: 11, rowId: 1, j: 0, value: 12, error: null, formula: 'j+{{id12}}+{{id13}}', formulaDisabled: false, }, }, varDeps: { k: [], j: [11] }, cellDeps: { 12: [11], 13: [11] }, }); }); it('改公式依赖, 将更新value和cellDeps', () => { expect( setCellData( state, 11, { formula: 'k+{{id12}}+{{id14}}' }, undefined as any, undefined as any, ), ).toEqual({ cells: { ...cells, 11: { id: 11, rowId: 1, j: 0, value: 1, error: null, formula: 'k+{{id12}}+{{id14}}', formulaDisabled: false, }, }, varDeps: { k: [11] }, cellDeps: { 12: [11], 13: [], 14: [11] }, }); }); it('单元格的value变动, 将引起 其它单元格变动 ', () => { expect(setCellData(state, 12, { value: 10 }, undefined as any, undefined as any)).toEqual({ cells: { ...cells, 11: { id: 11, rowId: 1, j: 0, value: 11, error: null, formula: 'k+{{id12}}+{{id13}}', formulaDisabled: false, }, 12: { id: 12, rowId: 1, j: 1, value: 10, formulaDisabled: false }, }, varDeps: { k: [11] }, cellDeps: { 12: [11], 13: [11] }, }); }); // 出错情形 }); describe('valuePropagation', () => { const rows: IRow[] = [{ id: 1, cellIds: [11, 12, 13, 14, 15] }]; const cells: { [id: string]: ICell } = { 11: { id: 11, rowId: 1, j: 0, value: 2, formula: '{{id12}}+{{id13}}', formulaDisabled: false, }, 12: { id: 12, rowId: 1, j: 1, value: 1, formula: '{{id13}}', formulaDisabled: false }, // 注意value 的值已改成了100!!! 13: { id: 13, rowId: 1, j: 2, value: 100, formulaDisabled: false }, 14: { id: 14, rowId: 1, j: 3, value: 0, formula: '{{id11}}+{{id12}}', formulaDisabled: false }, 15: { id: 15, rowId: 1, j: 4, value: 0 }, }; const state = { rows, cells, cellDeps: { 11: [14], 12: [11, 14], 13: [11, 12], 14: [] }, } as any; it('default', () => { const result = valuePropagation(state, 13); expect(result).toEqual({ cells: { 11: { id: 11, rowId: 1, j: 0, value: 200, error: null, formula: '{{id12}}+{{id13}}', formulaDisabled: false, }, 12: { id: 12, rowId: 1, j: 1, value: 100, error: null, formula: '{{id13}}', formulaDisabled: false, }, 13: { id: 13, rowId: 1, j: 2, value: 100, formulaDisabled: false }, 14: { id: 14, rowId: 1, j: 3, value: 300, error: null, formula: '{{id11}}+{{id12}}', formulaDisabled: false, }, 15: { id: 15, rowId: 1, j: 4, value: 0 }, }, }); }); });