Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
test(formula): restore formula testing and add missing dependencies (#2197)
Dushusire00c6607b6b6
diff --git a/packages/sheets-formula/package.json b/packages/sheets-formula/package.jsonindex b2a3551a32..ae52cf5d0e 100644--- a/packages/sheets-formula/package.json+++ b/packages/sheets-formula/package.json@@ -55,6 +55,9 @@"npm": ">=8.0.0"},"scripts": {+ "test": "vitest run",+ "test:watch": "vitest",+ "coverage": "vitest run --coverage","lint:types": "tsc --noEmit","build": "tsc && vite build"},diff --git a/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts b/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.tsindex 0602e287cf..0e0ffe9ecd 100644--- a/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts+++ b/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts@@ -17,14 +17,15 @@import type { IWorkbookData } from '@univerjs/core';import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core';import { LexerTreeBuilder } from '@univerjs/engine-formula';-import { SelectionManagerService } from '@univerjs/sheets';+import { SelectionManagerService, SheetInterceptorService } from '@univerjs/sheets';import type { Dependency } from '@wendellhu/redi';import { Inject, Injector } from '@wendellhu/redi';import { EditorService, IEditorService } from '@univerjs/ui';import { IRenderManagerService, RenderManagerService } from '@univerjs/engine-render';+import { EditorBridgeService, IEditorBridgeService, ISelectionRenderService, SelectionRenderService, SheetSkeletonManagerService } from '@univerjs/sheets-ui';import { FormulaPromptService, IFormulaPromptService } from '../../../services/prompt.service';-const TEST_WORKBOOK_DATA_DEMO: IWorkbookData = {+const TEST_WORKBOOK_DATA_DEMO = (): IWorkbookData => ({id: 'test',appVersion: '3.0.0-alpha',sheets: {@@ -43,7 +44,7 @@ const TEST_WORKBOOK_DATA_DEMO: IWorkbookData = {name: '',sheetOrder: [],styles: {},-};+});export function createCommandTestBed(workbookData?: IWorkbookData, dependencies?: Dependency[]) {const univer = new Univer();@@ -67,18 +68,22 @@ export function createCommandTestBed(workbookData?: IWorkbookData, dependencies?}override onStarting(injector: Injector): void {+ injector.add([ISelectionRenderService, { useClass: SelectionRenderService }]);injector.add([SelectionManagerService]);injector.add([LexerTreeBuilder]);injector.add([IFormulaPromptService, { useClass: FormulaPromptService }]);+ injector.add([IEditorBridgeService, { useClass: EditorBridgeService }]);injector.add([IEditorService, { useClass: EditorService }]);+ injector.add([SheetSkeletonManagerService]);injector.add([IRenderManagerService, { useClass: RenderManagerService }]);+ injector.add([SheetInterceptorService]);dependencies?.forEach((d) => injector.add(d));}}univer.registerPlugin(TestPlugin);- const sheet = univer.createUniverSheet(workbookData || TEST_WORKBOOK_DATA_DEMO);+ const sheet = univer.createUnit(UniverInstanceType.UNIVER_SHEET, workbookData || TEST_WORKBOOK_DATA_DEMO());const univerInstanceService = get(IUniverInstanceService);univerInstanceService.focusUnit('test');diff --git a/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts b/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.tsindex d29fd8a668..a24b3575e6 100644--- a/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts+++ b/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { ICellData, Nullable, Univer } from '@univerjs/core';+import type { Univer } from '@univerjs/core';import { ICommandService, IUniverInstanceService, RANGE_TYPE, RedoCommand, UndoCommand } from '@univerjs/core';import {NORMAL_SELECTION_PLUGIN_NAME,@@ -60,84 +60,86 @@ describe('Test insert function operation', () => {describe('insert function', () => {describe('correct situations', () => {- it('insert function, match the data range above', async () => {+ it('insert function, match the data range above, directly set values', async () => {const selectionManager = get(SelectionManagerService);selectionManager.setCurrentSelection({pluginName: NORMAL_SELECTION_PLUGIN_NAME,unitId: 'test',sheetId: 'sheet1',});- // B3+ // B3:B4selectionManager.add([{- range: { startRow: 2, startColumn: 1, endRow: 2, endColumn: 1, rangeType: RANGE_TYPE.NORMAL },+ range: { startRow: 2, startColumn: 1, endRow: 3, endColumn: 1, rangeType: RANGE_TYPE.NORMAL },primary: null,style: null,},]);- function getValue(): Nullable<ICellData> {+ function getValues() {return get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1')- ?.getRange(2, 1, 2, 1)- .getValue();+ ?.getRange(2, 1, 3, 1)+ .getValues();}const params: IInsertFunctionOperationParams = {value: 'SUM',};expect(await commandService.executeCommand(InsertFunctionOperation.id, params)).toBeTruthy();- expect(getValue()?.f).toStrictEqual('=SUM(B2)');+ let values = getValues();+ expect(values?.[0]?.[0]?.f).toStrictEqual('=SUM(B2)');+ expect(values?.[0]?.[0]?.si).toStrictEqual(values?.[1]?.[0]?.si);// undoexpect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();- expect(getValue()).toStrictEqual({});+ expect(getValues()).toStrictEqual([[{}], [{}]]);// redoexpect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();- expect(getValue()?.f).toStrictEqual('=SUM(B2)');-- // Restore the original data- expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();+ values = getValues();+ expect(values?.[0]?.[0]?.f).toStrictEqual('=SUM(B2)');+ expect(values?.[0]?.[0]?.si).toStrictEqual(values?.[1]?.[0]?.si);});- it('insert function, match the data range left', async () => {+ it('insert function, match the data range left, directly set values', async () => {const selectionManager = get(SelectionManagerService);selectionManager.setCurrentSelection({pluginName: NORMAL_SELECTION_PLUGIN_NAME,unitId: 'test',sheetId: 'sheet1',});- // C2+ // C2:D2selectionManager.add([{- range: { startRow: 1, startColumn: 2, endRow: 1, endColumn: 2, rangeType: RANGE_TYPE.NORMAL },+ range: { startRow: 1, startColumn: 2, endRow: 1, endColumn: 3, rangeType: RANGE_TYPE.NORMAL },primary: null,style: null,},]);- function getValue(): Nullable<ICellData> {+ function getValues() {return get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1')- ?.getRange(1, 2, 1, 2)- .getValue();+ ?.getRange(1, 2, 1, 3)+ .getValues();}const params: IInsertFunctionOperationParams = {value: 'SUM',};expect(await commandService.executeCommand(InsertFunctionOperation.id, params)).toBeTruthy();- expect(getValue()?.f).toStrictEqual('=SUM(B2)');+ let values = getValues();+ expect(values?.[0]?.[0]?.f).toStrictEqual('=SUM(B2)');+ expect(values?.[0]?.[0]?.si).toStrictEqual(values?.[0]?.[1]?.si);// undoexpect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();- expect(getValue()).toStrictEqual({});+ expect(getValues()).toStrictEqual([[{}, {}]]);// redoexpect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();- expect(getValue()?.f).toStrictEqual('=SUM(B2)');-- // Restore the original data- expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();+ values = getValues();+ expect(values?.[0]?.[0]?.f).toStrictEqual('=SUM(B2)');+ expect(values?.[0]?.[0]?.si).toStrictEqual(values?.[0]?.[1]?.si);});});@@ -193,7 +195,7 @@ describe('Test insert function operation', () => {},},};- expect(isNumberCell(cell)).toBeTruthy();+ expect(isNumberCell(cell)).toBeFalsy();});it('should return true when cell is rich text', () => {const cell = {diff --git a/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts b/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.tsindex d48bf3f299..479c47d784 100644--- a/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts+++ b/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts@@ -17,7 +17,10 @@import type { IWorkbookData, Workbook } from '@univerjs/core';import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core';import { CalculateFormulaService, DefinedNamesService, FormulaCurrentConfigService, FormulaDataModel, FormulaRuntimeService, IDefinedNamesService, IFormulaCurrentConfigService, IFormulaRuntimeService, LexerTreeBuilder } from '@univerjs/engine-formula';+import { IRenderManagerService, RenderManagerService } from '@univerjs/engine-render';import { SelectionManagerService, SheetInterceptorService } from '@univerjs/sheets';+import { EditorBridgeService, IEditorBridgeService, SheetSkeletonManagerService } from '@univerjs/sheets-ui';+import { EditorService, IEditorService } from '@univerjs/ui';import type { Dependency } from '@wendellhu/redi';import { Inject, Injector } from '@wendellhu/redi';@@ -83,6 +86,10 @@ export function createCommandTestBed(workbookData?: IWorkbookData, dependencies?injector.add([IDefinedNamesService, { useClass: DefinedNamesService }]);injector.add([IFormulaRuntimeService, { useClass: FormulaRuntimeService }]);injector.add([IFormulaCurrentConfigService, { useClass: FormulaCurrentConfigService }]);+ injector.add([SheetSkeletonManagerService]);+ injector.add([IEditorBridgeService, { useClass: EditorBridgeService }]);+ injector.add([IEditorService, { useClass: EditorService }]);+ injector.add([IRenderManagerService, { useClass: RenderManagerService }]);dependencies?.forEach((d) => injector.add(d));}diff --git a/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.tsindex 43eb338754..4223056f22 100644--- a/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts+++ b/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts@@ -32,7 +32,6 @@ import {SetSelectionsOperation,} from '@univerjs/sheets';import {- AutoFillCommand,AutoFillController,AutoFillService,IAutoFillService,@@ -116,7 +115,7 @@ describe('Test auto fill with formula', () => {},]);- (autoFillController as any)._handleFill(+ (autoFillController as any)._triggerAutoFill({startColumn: 1,endColumn: 1,@@ -167,7 +166,7 @@ describe('Test auto fill with formula', () => {expect(B2?.si).toEqual(B3?.si);// drop to right- (autoFillController as any)._handleFill(+ (autoFillController as any)._triggerAutoFill({startColumn: 1,endColumn: 1,@@ -280,11 +279,4 @@ describe('Test auto fill with formula', () => {expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();});});-- describe('fault situations', () => {- it('will not apply when there is no selected ranges', async () => {- const result = await commandService.executeCommand(AutoFillCommand.id);- expect(result).toBeFalsy();- });- });});diff --git a/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.tsindex c3233415b9..37c3e19e0e 100644--- a/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.ts+++ b/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.ts@@ -108,6 +108,7 @@ describe('Test paste with formula', () => {cellValue: {12: {2: {+ custom: null,s: null,f: null,si: null,@@ -116,6 +117,7 @@ describe('Test paste with formula', () => {t: null,},3: {+ custom: null,s: null,f: null,si: null,@@ -333,6 +335,7 @@ describe('Test paste with formula', () => {cellValue: {5: {5: {+ custom: null,s: null,f: null,si: null,@@ -341,6 +344,7 @@ describe('Test paste with formula', () => {t: null,},6: {+ custom: null,s: null,f: null,si: null,@@ -349,6 +353,7 @@ describe('Test paste with formula', () => {t: null,},7: {+ custom: null,s: null,f: null,si: null,@@ -357,6 +362,7 @@ describe('Test paste with formula', () => {t: null,},8: {+ custom: null,s: null,f: null,si: null,@@ -367,6 +373,7 @@ describe('Test paste with formula', () => {},6: {5: {+ custom: null,s: null,f: null,si: null,@@ -375,6 +382,7 @@ describe('Test paste with formula', () => {t: null,},6: {+ custom: null,s: null,f: null,si: null,@@ -383,6 +391,7 @@ describe('Test paste with formula', () => {t: null,},7: {+ custom: null,s: null,f: null,si: null,@@ -391,6 +400,7 @@ describe('Test paste with formula', () => {t: null,},8: {+ custom: null,s: null,f: null,si: null,@@ -401,6 +411,7 @@ describe('Test paste with formula', () => {},7: {5: {+ custom: null,s: null,f: null,si: null,@@ -409,6 +420,7 @@ describe('Test paste with formula', () => {t: null,},6: {+ custom: null,s: null,f: null,si: null,@@ -417,6 +429,7 @@ describe('Test paste with formula', () => {t: null,},7: {+ custom: null,s: null,f: null,si: null,@@ -425,6 +438,7 @@ describe('Test paste with formula', () => {t: null,},8: {+ custom: null,s: null,f: null,si: null,@@ -435,6 +449,7 @@ describe('Test paste with formula', () => {},8: {5: {+ custom: null,s: null,f: null,si: null,@@ -443,6 +458,7 @@ describe('Test paste with formula', () => {t: null,},6: {+ custom: null,s: null,f: null,si: null,@@ -451,6 +467,7 @@ describe('Test paste with formula', () => {t: null,},7: {+ custom: null,s: null,f: null,si: null,@@ -459,6 +476,7 @@ describe('Test paste with formula', () => {t: null,},8: {+ custom: null,s: null,f: null,si: null,