Skip to content

Commit

Permalink
fix(formula): date function test, link to formatted cell supports format
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Feb 3, 2024
1 parent 589cccb commit d1864e6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
24 changes: 24 additions & 0 deletions packages/engine-formula/src/basics/__tests__/date.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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
*
* https://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 { describe, expect, it } from 'vitest';
import { excelDateSerial } from '../date';

describe('Test date', () => {
it('Function excelDateSerial', () => {
expect(excelDateSerial(new Date(2024, 1, 2))).toBe(45324);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@ import { describe, expect, it, vi } from 'vitest';

import { FUNCTION_NAMES_DATE } from '../../function-names';
import { Today } from '..';
import { NumberValueObject } from '../../../../engine/value-object/primitive-object';
import { ErrorType } from '../../../..';

// mock new Date() use V
const _Date = Date;
global.Date = vi.fn(() => new _Date('2024-01-01T00:00:00.000Z')) as any;
global.Date = vi.fn((...params) => params.length > 0 ? new _Date(params[0], params[1], params[2]) : new _Date(2020, 0, 1)) as any;

describe('Test today function', () => {
const textFunction = new Today(FUNCTION_NAMES_DATE.TODAY);

describe('Today', () => {
it('Value is normal', () => {
it('Normal', () => {
const result = textFunction.calculate();
expect(result.getValue()).toBe(1);
expect(result.getValue()).toBe(43831);
});

it('Set a parameter', () => {
const result = textFunction.calculate(new NumberValueObject(1));
expect(result.getValue()).toBe(ErrorType.NA);
});
});
});
9 changes: 7 additions & 2 deletions packages/engine-formula/src/functions/date/today/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
* limitations under the License.
*/

import { NumberValueObject } from '../../..';
import type { BaseValueObject } from '../../..';
import { ErrorType, ErrorValueObject, NumberValueObject } from '../../..';
import { DEFFAULT_DATE_FORMAT, excelDateSerial } from '../../../basics/date';
import { BaseFunction } from '../../base-function';

export class Today extends BaseFunction {
override calculate() {
override calculate(value?: BaseValueObject) {
if (value) {
return new ErrorValueObject(ErrorType.NA);
}

const currentSerial = excelDateSerial(new Date());
const valueObject = new NumberValueObject(currentSerial);
valueObject.setPattern(DEFFAULT_DATE_FORMAT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class TriggerCalculationController extends Disposable {
const allDirtyRanges: IUnitRange[] = [];
const allDirtyNameMap: IDirtyUnitSheetNameMap = {};
const allDirtyUnitFeatureMap: IDirtyUnitFeatureMap = {};
const numfmtItemMap: INumfmtItemMap = {};
const numfmtItemMap: INumfmtItemMap = this._formulaDataModel.getNumfmtItemMap();

for (const command of commands) {
const conversion = this._activeDirtyManagerService.get(command.id);
Expand Down

0 comments on commit d1864e6

Please sign in to comment.