|
1 | 1 | import * as Formula from "./formula"; |
2 | | -import { getFormulaComputedValue } from "./engine"; |
| 2 | +import { getFormulaComputedValue, updateCellValue, Model } from "./engine"; |
| 3 | +import { CellBase } from "./types"; |
3 | 4 | import FormulaParser from "fast-formula-parser"; |
4 | | -import { ORIGIN } from "./point"; |
| 5 | +import { ORIGIN, Point } from "./point"; |
5 | 6 |
|
6 | 7 | const MOCK_PARSE = jest.fn(); |
7 | 8 | const MOCK_FORMULA_PARSER = { |
@@ -41,3 +42,36 @@ describe("getFormulaComputedValue()", () => { |
41 | 42 | ); |
42 | 43 | }); |
43 | 44 | }); |
| 45 | + |
| 46 | +describe("updateCellValue", () => { |
| 47 | + test("update simple cell", () => { |
| 48 | + const model = new Model([]); |
| 49 | + const cell: CellBase = { value: "1" }; |
| 50 | + const point: Point = { row: 0, column: 0 }; |
| 51 | + const nextModel = updateCellValue(model, point, cell); |
| 52 | + expect(nextModel.data).toEqual([[cell]]); |
| 53 | + expect(nextModel.evaluatedData).toEqual([[cell]]); |
| 54 | + }); |
| 55 | + test("update simple formula cell", () => { |
| 56 | + const model = new Model([[{ value: 1 }], [{ value: 2 }]]); |
| 57 | + const cell: CellBase = { value: "=A1" }; |
| 58 | + const point: Point = { row: 0, column: 1 }; |
| 59 | + const nextModel = updateCellValue(model, point, cell); |
| 60 | + expect(nextModel.data).toEqual([[{ value: 1 }, cell], [{ value: 2 }]]); |
| 61 | + expect(nextModel.evaluatedData).toEqual([ |
| 62 | + [{ value: 1 }, { value: 1 }], |
| 63 | + [{ value: 2 }], |
| 64 | + ]); |
| 65 | + }); |
| 66 | + test("update range formula cell", () => { |
| 67 | + const model = new Model([[{ value: 1 }], [{ value: 2 }]]); |
| 68 | + const cell: CellBase = { value: "=SUM(A:A)" }; |
| 69 | + const point: Point = { row: 0, column: 1 }; |
| 70 | + const nextModel = updateCellValue(model, point, cell); |
| 71 | + expect(nextModel.data).toEqual([[{ value: 1 }, cell], [{ value: 2 }]]); |
| 72 | + expect(nextModel.evaluatedData).toEqual([ |
| 73 | + [{ value: 1 }, { value: 3 }], |
| 74 | + [{ value: 2 }], |
| 75 | + ]); |
| 76 | + }); |
| 77 | +}); |
0 commit comments