-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathcell-linkage.test.ts
More file actions
89 lines (78 loc) · 3.47 KB
/
Copy pathcell-linkage.test.ts
File metadata and controls
89 lines (78 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { FormulaManager } from '../../src/managers/formula-manager';
import type VTableSheet from '../../src/components/vtable-sheet';
// Mock VTableSheet for testing
// 使用闭包共享 sheets Map,确保 addSheet 和 getSheetManager 都能访问
const mockSheets = new Map<string, { sheetTitle: string; sheetKey: string; showHeader: boolean; columns: any[] }>();
const mockVTableSheet = {
workSheetInstances: new Map(), // 添加缺失的 workSheetInstances 属性
getSheetManager: () => ({
getSheet: (sheetKey: string) => {
if (!mockSheets.has(sheetKey)) {
mockSheets.set(sheetKey, {
sheetTitle: sheetKey,
sheetKey: sheetKey,
showHeader: true,
columns: [] as any[]
});
}
return mockSheets.get(sheetKey);
},
getAllSheets: () => {
// 返回所有 sheets 的数组
return Array.from(mockSheets.values()).map(sheet => ({
sheetKey: sheet.sheetKey,
sheetTitle: sheet.sheetTitle
}));
},
getSheetCount: () => mockSheets.size
}),
getActiveSheet: (): any => null,
createWorkSheetInstance: (sheetDefine: any): any => {
// 返回一个简单的 mock 实例
return {
getElement: () => ({ style: { display: '' } }),
getData: (): any[] => [],
getColumns: (): any[] => [],
release: (): void => {}
};
}
} as unknown as VTableSheet;
describe('Cell Linkage Test', () => {
let formulaManager: FormulaManager;
beforeEach(() => {
formulaManager = new FormulaManager(mockVTableSheet);
});
afterEach(() => {
formulaManager.release();
});
test('should handle basic cell linkage', () => {
formulaManager.addSheet('Sheet1', [
['A', 'B', 'C', 'D', 'E'],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', '']
]);
// Set numeric values first (row 1 = A2, B2 in Excel notation)
formulaManager.setCellContent({ sheet: 'Sheet1', row: 1, col: 0 }, 10);
formulaManager.setCellContent({ sheet: 'Sheet1', row: 1, col: 1 }, 20);
formulaManager.setCellContent({ sheet: 'Sheet1', row: 2, col: 0 }, 30);
formulaManager.setCellContent({ sheet: 'Sheet1', row: 2, col: 1 }, 40);
// Set formulas (row 1 = A2, B2, C2, D2, E2 in Excel notation)
formulaManager.setCellContent({ sheet: 'Sheet1', row: 1, col: 2 }, '=A2+B2');
formulaManager.setCellContent({ sheet: 'Sheet1', row: 1, col: 3 }, '=C2*2');
formulaManager.setCellContent({ sheet: 'Sheet1', row: 1, col: 4 }, '=SUM(A2:B2)');
// Verify initial calculations
expect(formulaManager.getCellValue({ sheet: 'Sheet1', row: 1, col: 2 }).value).toBe(30);
expect(formulaManager.getCellValue({ sheet: 'Sheet1', row: 1, col: 3 }).value).toBe(60);
expect(formulaManager.getCellValue({ sheet: 'Sheet1', row: 1, col: 4 }).value).toBe(30);
// Test dependency tracking
const a1Dependents = formulaManager.getCellDependents({ sheet: 'Sheet1', row: 1, col: 0 });
expect(a1Dependents.some((dep: any) => dep.row === 1 && dep.col === 2)).toBe(true);
expect(a1Dependents.some((dep: any) => dep.row === 1 && dep.col === 4)).toBe(true);
// Change A1 and verify updates
formulaManager.setCellContent({ sheet: 'Sheet1', row: 1, col: 0 }, '100');
expect(formulaManager.getCellValue({ sheet: 'Sheet1', row: 1, col: 2 }).value).toBe(120);
expect(formulaManager.getCellValue({ sheet: 'Sheet1', row: 1, col: 3 }).value).toBe(240);
expect(formulaManager.getCellValue({ sheet: 'Sheet1', row: 1, col: 4 }).value).toBe(120);
});
});