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(numfmt): add test case (#1179)
Gpound.liu167262ea76e2
diff --git a/packages/sheets-numfmt/src/controllers/__tests__/auto-fill.controller.spec.ts b/packages/sheets-numfmt/src/controllers/__tests__/auto-fill.controller.spec.tsnew file mode 100644index 0000000000..7ef595eae8--- /dev/null+++ b/packages/sheets-numfmt/src/controllers/__tests__/auto-fill.controller.spec.ts@@ -0,0 +1,310 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { IRange, Workbook, Worksheet } from '@univerjs/core';+import { ICommandService, IUniverInstanceService } from '@univerjs/core';+import type { ISetNumfmtMutationParams } from '@univerjs/sheets';+import { SelectionManagerService, SetNumfmtMutation } from '@univerjs/sheets';+import { APPLY_TYPE, AutoFillService, IAutoFillService } from '@univerjs/sheets-ui';+import { beforeEach, describe, expect, it } from 'vitest';++import { SHEET_NUMFMT_PLUGIN } from '../../base/const/PLUGIN_NAME';+import { NumfmtAutoFillController } from '../numfmt.auto-fill.controller';+import { NumfmtCellContent } from '../numfmt.cell-content.controller';+import { createTestBed } from './test.util';++describe('test auto fill', () => {+ let unitId: string = '';+ let subUnitId: string = '';+ let workbook: Workbook;+ let worksheet: Worksheet;+ let testBed: ReturnType<typeof createTestBed>;+ let commandService: ICommandService;++ beforeEach(() => {+ testBed = createTestBed([+ [NumfmtAutoFillController],+ [NumfmtCellContent],+ [SelectionManagerService],+ [IAutoFillService, { useClass: AutoFillService }],+ ]);+ unitId = testBed.unitId;+ subUnitId = testBed.subUnitId;+ commandService = testBed.get(ICommandService);+ const univerInstanceService = testBed.get(IUniverInstanceService);+ testBed.get(NumfmtAutoFillController);+ testBed.get(NumfmtCellContent);+ workbook = univerInstanceService.getCurrentUniverSheetInstance();+ worksheet = workbook.getActiveSheet();+ });+ it('test fill format ,repeat range is divisible', () => {+ const params: ISetNumfmtMutationParams = {+ unitId,+ subUnitId,+ values: {+ 1: {+ ranges: [{ startRow: 0, endRow: 5, startColumn: 0, endColumn: 5 }],+ },+ },+ refMap: {+ 1: {+ pattern: '$#,##0;(#,##0)',+ type: '' as any,+ },+ },+ };+ commandService.syncExecuteCommand(SetNumfmtMutation.id, params);+ const autoFillService = testBed.get(IAutoFillService);+ const hooks = autoFillService.getAllHooks();+ const item = hooks.find((ele) => ele.id === SHEET_NUMFMT_PLUGIN)!;+ expect(!!item).toBeTruthy();+ const source: IRange = {+ startRow: 3,+ endRow: 5,+ startColumn: 0,+ endColumn: 1,+ };+ const target: IRange = {+ startRow: 3,+ endRow: 5,+ startColumn: 2,+ endColumn: 9,+ };+ const result =+ item.onFillData &&+ item.onFillData({ source, target, unitId, subUnitId }, '' as any, APPLY_TYPE.ONLY_FORMAT);+ expect(result).toEqual({+ undos: [+ {+ id: 'sheet.mutation.remove.numfmt',+ params: {+ ranges: [+ {+ startRow: 3,+ endRow: 5,+ startColumn: 6,+ endColumn: 9,+ },+ ],+ unitId: 'test',+ subUnitId: 'sheet1',+ },+ },+ {+ id: 'sheet.mutation.set.numfmt',+ params: {+ values: {+ '1': {+ ranges: [+ {+ startRow: 3,+ endRow: 5,+ startColumn: 2,+ endColumn: 5,+ },+ ],+ },+ },+ refMap: {+ '1': {+ pattern: '$#,##0;(#,##0)',+ type: '',+ },+ },+ unitId: 'test',+ subUnitId: 'sheet1',+ },+ },+ ],+ redos: [+ {+ id: 'sheet.mutation.set.numfmt',+ params: {+ values: {+ '1': {+ ranges: [+ {+ startRow: 3,+ endRow: 5,+ startColumn: 2,+ endColumn: 9,+ },+ ],+ },+ },+ refMap: {+ '1': {+ pattern: '$#,##0;(#,##0)',+ type: '',+ },+ },+ unitId: 'test',+ subUnitId: 'sheet1',+ },+ },+ ],+ });+ });+ // @TODO:YuHong,there are some repeat problems to be solved+ it('test fill format ,repeat range is not divisible', () => {+ const params: ISetNumfmtMutationParams = {+ unitId,+ subUnitId,+ values: {+ 1: {+ ranges: [{ startRow: 0, endRow: 5, startColumn: 0, endColumn: 5 }],+ },+ },+ refMap: {+ 1: {+ pattern: '$#,##0;(#,##0)',+ type: '' as any,+ },+ },+ };+ commandService.syncExecuteCommand(SetNumfmtMutation.id, params);+ const autoFillService = testBed.get(IAutoFillService);+ const hooks = autoFillService.getAllHooks();+ const item = hooks.find((ele) => ele.id === SHEET_NUMFMT_PLUGIN)!;+ expect(!!item).toBeTruthy();+ const source: IRange = {+ startRow: 3,+ endRow: 5,+ startColumn: 0,+ endColumn: 1,+ };+ const target: IRange = {+ startRow: 3,+ endRow: 5,+ startColumn: 2,+ endColumn: 10,+ };+ const result =+ item.onFillData &&+ item.onFillData({ source, target, unitId, subUnitId }, '' as any, APPLY_TYPE.ONLY_FORMAT);+ expect(result).toEqual({+ undos: [+ {+ id: 'sheet.mutation.remove.numfmt',+ params: {+ ranges: [+ {+ startRow: 3,+ endRow: 5,+ startColumn: 6,+ endColumn: 11,+ },+ ],+ unitId: 'test',+ subUnitId: 'sheet1',+ },+ },+ {+ id: 'sheet.mutation.set.numfmt',+ params: {+ values: {+ '1': {+ ranges: [+ {+ startRow: 3,+ endRow: 5,+ startColumn: 2,+ endColumn: 5,+ },+ ],+ },+ },+ refMap: {+ '1': {+ pattern: '$#,##0;(#,##0)',+ type: '',+ },+ },+ unitId: 'test',+ subUnitId: 'sheet1',+ },+ },+ ],+ redos: [+ {+ id: 'sheet.mutation.set.numfmt',+ params: {+ values: {+ '1': {+ ranges: [+ {+ startRow: 3,+ endRow: 5,+ startColumn: 2,+ endColumn: 11, // 有点问题.+ },+ ],+ },+ },+ refMap: {+ '1': {+ pattern: '$#,##0;(#,##0)',+ type: '',+ },+ },+ unitId: 'test',+ subUnitId: 'sheet1',+ },+ },+ ],+ });+ });+ it('test fill without format', () => {+ const params: ISetNumfmtMutationParams = {+ unitId,+ subUnitId,+ values: {+ 1: {+ ranges: [{ startRow: 0, endRow: 5, startColumn: 0, endColumn: 5 }],+ },+ },+ refMap: {+ 1: {+ pattern: '$#,##0;(#,##0)',+ type: '' as any,+ },+ },+ };+ commandService.syncExecuteCommand(SetNumfmtMutation.id, params);+ const autoFillService = testBed.get(IAutoFillService);+ const hooks = autoFillService.getAllHooks();+ const item = hooks.find((ele) => ele.id === SHEET_NUMFMT_PLUGIN)!;+ expect(!!item).toBeTruthy();+ const source: IRange = {+ startRow: 3,+ endRow: 5,+ startColumn: 0,+ endColumn: 1,+ };+ const target: IRange = {+ startRow: 3,+ endRow: 5,+ startColumn: 2,+ endColumn: 10,+ };+ const result =+ item.onFillData && item.onFillData({ source, target, unitId, subUnitId }, '' as any, APPLY_TYPE.NO_FORMAT);+ expect(result).toEqual({+ undos: [],+ redos: [],+ });+ });+});diff --git a/packages/sheets-numfmt/src/controllers/__tests__/copy-paste.controller.spec.ts b/packages/sheets-numfmt/src/controllers/__tests__/copy-paste.controller.spec.tsnew file mode 100644index 0000000000..cafea44d7f--- /dev/null+++ b/packages/sheets-numfmt/src/controllers/__tests__/copy-paste.controller.spec.ts@@ -0,0 +1,216 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { ICommandService } from '@univerjs/core';+import type { ISetNumfmtMutationParams } from '@univerjs/sheets';+import { SetNumfmtMutation } from '@univerjs/sheets';+import type { ISheetClipboardHook } from '@univerjs/sheets-ui';+import { ISheetClipboardService } from '@univerjs/sheets-ui';+import { beforeEach, describe, expect, it } from 'vitest';++import { SHEET_NUMFMT_PLUGIN } from '../../base/const/PLUGIN_NAME';+import { NumfmtCopyPasteController } from '../numfmt.copy-paste.controller';+import { createTestBed } from './test.util';++class MockSheetClipboardService {+ hooks: ISheetClipboardHook[] = [];+ getHooks = () => this.hooks;+ addClipboardHook = (config: ISheetClipboardHook) => {+ this.hooks.push(config);+ };+}+describe('test copy paste', () => {+ let unitId: string = '';+ let subUnitId: string = '';+ let testBed: any;+ let commandService: ICommandService;+ beforeEach(() => {+ testBed = createTestBed([+ [NumfmtCopyPasteController],+ [ISheetClipboardService, { useClass: MockSheetClipboardService }],+ ]);+ unitId = testBed.unitId;+ subUnitId = testBed.subUnitId;+ testBed.get(NumfmtCopyPasteController);+ commandService = testBed.get(ICommandService);+ });+ it('test', () => {+ const params: ISetNumfmtMutationParams = {+ unitId,+ subUnitId,+ values: {+ 1: {+ ranges: [{ startRow: 0, endRow: 3, startColumn: 0, endColumn: 4 }],+ },+ },+ refMap: {+ 1: {+ pattern: '$#,##0;(#,##0)',+ type: '' as any,+ },+ },+ };+ commandService.syncExecuteCommand(SetNumfmtMutation.id, params);+ const sheetClipboardService = testBed.get(ISheetClipboardService) as MockSheetClipboardService;+ const hooks = sheetClipboardService.getHooks();+ const hook = hooks.find((hook) => hook.id === SHEET_NUMFMT_PLUGIN)!;+ expect(!!hook).toBeTruthy();+ hook.onBeforeCopy!(unitId, subUnitId, {+ startRow: 2,+ startColumn: 3,+ endRow: 4,+ endColumn: 4,+ });+ const result = hook.onPasteCells!(+ {+ unitId,+ subUnitId,+ range: {+ startRow: 2,+ startColumn: 3,+ endRow: 4,+ endColumn: 4,+ },+ },+ {+ range: {+ startRow: 9,+ startColumn: 5,+ endRow: 11,+ endColumn: 6,+ rangeType: 0,+ },+ unitId,+ subUnitId,+ },+ {+ '0': {+ '0': {+ p: null,+ v: 5,+ s: null,+ f: null,+ si: null,+ t: 2,+ },+ '1': {+ p: null,+ v: 6,+ s: null,+ f: null,+ si: null,+ t: 2,+ },+ },+ '1': {+ '0': {+ p: null,+ v: 6,+ s: null,+ f: null,+ si: null,+ t: 2,+ },+ '1': {+ p: null,+ v: 7,+ s: null,+ f: null,+ si: null,+ t: 2,+ },+ },+ '2': {+ '0': {+ p: null,+ v: 7,+ s: null,+ f: null,+ si: null,+ t: 2,+ },+ '1': {+ p: null,+ v: 8,+ s: null,+ f: null,+ si: null,+ t: 2,+ },+ },+ } as any,+ {+ copyType: 'COPY',+ copyId: 'asdasd',+ pasteType: 'default-paste',+ } as any+ );+ expect(result).toEqual({+ redos: [+ {+ id: 'sheet.mutation.remove.numfmt',+ params: {+ unitId,+ subUnitId,+ ranges: [],+ },+ },+ {+ id: 'sheet.mutation.set.numfmt',+ params: {+ unitId,+ subUnitId,+ refMap: {+ '0': {+ pattern: '$#,##0;(#,##0)',+ type: '',+ },+ },+ values: {+ '0': {+ ranges: [+ {+ startRow: 9,+ endRow: 10,+ startColumn: 5,+ endColumn: 6,+ },+ ],+ },+ },+ },+ },+ ],+ undos: [+ {+ id: 'sheet.mutation.remove.numfmt',+ params: {+ ranges: [+ {+ startRow: 9,+ endRow: 10,+ startColumn: 5,+ endColumn: 6,+ },+ ],+ unitId,+ subUnitId,+ },+ },+ ],+ });+ });+});diff --git a/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.tsindex 4282f09f54..8562a45bc3 100644--- a/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.ts+++ b/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.ts@@ -37,6 +37,9 @@ import {import { COPY_TYPE, getRepeatRange, ISheetClipboardService, PREDEFINED_HOOK_NAME } from '@univerjs/sheets-ui';import { Inject, Injector } from '@wendellhu/redi';+import { SHEET_NUMFMT_PLUGIN } from '../base/const/PLUGIN_NAME';+import { mergeNumfmtMutations } from '../utils/mutation';+@OnLifecycle(LifecycleStages.Rendered, NumfmtCopyPasteController)export class NumfmtCopyPasteController extends Disposable {private _copyInfo: Nullable<{@@ -60,7 +63,7 @@ export class NumfmtCopyPasteController extends Disposable {private _initClipboardHook() {this.disposeWithMe(this._sheetClipboardService.addClipboardHook({- id: 'numfmt',+ id: SHEET_NUMFMT_PLUGIN,onBeforeCopy: (unitId, subUnitId, range) => this._collectNumfmt(unitId, subUnitId, range),onPasteCells: (pasteFrom, pasteTo, data, payload) => {const { copyType = COPY_TYPE.COPY, pasteType } = payload;@@ -186,7 +189,7 @@ export class NumfmtCopyPasteController extends Disposable {{ id: RemoveNumfmtMutation.id, params: removeRedos },{ id: SetNumfmtMutation.id, params: setRedos },],- undos,+ undos: mergeNumfmtMutations(undos),};}}diff --git a/packages/sheets/src/commands/mutations/numfmt-mutation.ts b/packages/sheets/src/commands/mutations/numfmt-mutation.tsindex 8d23b31ed4..9075dabcb6 100644--- a/packages/sheets/src/commands/mutations/numfmt-mutation.ts+++ b/packages/sheets/src/commands/mutations/numfmt-mutation.ts@@ -48,7 +48,7 @@ export const factorySetNumfmtUndoMutation = (accessor: IAccessor, option: ISetNu});});const result: Array<IMutationInfo<ISetNumfmtMutationParams | IRemoveNumfmtMutationParams>> = [];- if (cells) {+ if (cells.length) {const params = transformCellsToRange(unitId, subUnitId, cells);Object.keys(params.values).forEach((key) => {const v = params.values[key];@@ -59,7 +59,7 @@ export const factorySetNumfmtUndoMutation = (accessor: IAccessor, option: ISetNuparams: transformCellsToRange(unitId, subUnitId, cells),});}- if (removeCells) {+ if (removeCells.length) {result.push({id: RemoveNumfmtMutation.id,params: {@@ -154,6 +154,9 @@ export const factoryRemoveNumfmtUndoMutation = (accessor: IAccessor, option: IRe}});});+ if (!cells.length) {+ return [];+ }const params = transformCellsToRange(unitId, subUnitId, cells);Object.keys(params.values).forEach((key) => {const v = params.values[key];