Skip to content

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.

3.8 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * 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 { Injector } from '@univerjs/core';
import { beforeEach, describe, expect, it } from 'vitest';
import { IOtherFormulaManagerService, OtherFormulaManagerService } from '../other-formula-manager.service';

describe('OtherFormulaManagerService', () => {
    let service: IOtherFormulaManagerService;

    beforeEach(() => {
        const injector = new Injector();
        injector.add([IOtherFormulaManagerService, { useClass: OtherFormulaManagerService }]);
        service = injector.get(IOtherFormulaManagerService);
    });

    it.each(['__proto__', 'constructor', 'prototype', 'toString'])('keeps special identifiers (%s) as ordinary formula data', (key) => {
        const injector = new Injector([[OtherFormulaManagerService]]);
        const service = injector.get(OtherFormulaManagerService);
        const marker = '__formula_pollution__';
        const item = { f: '=1', ranges: [] };
        try {
            for (const search of [
                { unitId: key, subUnitId: marker, formulaId: marker },
                { unitId: 'unit', subUnitId: key, formulaId: marker },
                { unitId: 'unit', subUnitId: 'sheet', formulaId: key },
            ]) {
                service.register({ ...search, item });
                expect(service.get(search)).toBe(item);
                expect(Object.getOwnPropertyDescriptor(Object.prototype, marker)).toBeUndefined();
                expect(Object.getOwnPropertyDescriptor(Object, marker)).toBeUndefined();
                expect(Object.getOwnPropertyDescriptor(Object.prototype.toString, marker)).toBeUndefined();
                service.remove(search);
                expect(service.get(search)).toBeUndefined();
            }
            expect(Object.getOwnPropertyDescriptor(Object.prototype, marker)).toBeUndefined();
        } finally {
            Reflect.deleteProperty(Object.prototype, marker);
            Reflect.deleteProperty(Object, marker);
            Reflect.deleteProperty(Object.prototype.toString, marker);
            injector.dispose();
        }
    });

    it('keeps plugin-owned formula data isolated by unit, sheet and formula id', () => {
        const item = { f: '=TABLE()', ranges: [] };

        service.register({ unitId: 'book-1', subUnitId: 'sheet-1', formulaId: 'table-1', item: item as never });

        expect(service.has({ unitId: 'book-1', subUnitId: 'sheet-1', formulaId: 'table-1' })).toBe(true);
        expect(service.get({ unitId: 'book-1', subUnitId: 'sheet-1', formulaId: 'table-1' })).toBe(item);
        expect(service.get({ unitId: 'book-1', subUnitId: 'sheet-2', formulaId: 'table-1' })).toBeUndefined();
    });

    it('batch removes only dirty formula ids reported by a feature plugin', () => {
        service.batchRegister({
            'book-1': {
                'sheet-1': {
                    'table-1': { f: '=TABLE(1)' } as never,
                    'table-2': { f: '=TABLE(2)' } as never,
                },
            },
        });

        service.batchRemove({ 'book-1': { 'sheet-1': { 'table-1': true } } } as never);

        expect(service.has({ unitId: 'book-1', subUnitId: 'sheet-1', formulaId: 'table-1' })).toBe(false);
        expect(service.has({ unitId: 'book-1', subUnitId: 'sheet-1', formulaId: 'table-2' })).toBe(true);
    });
});