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.

44.4 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 type { ICellData, Workbook } from '@univerjs/core';
import type {
    IAverageHighlightCell,
    IConditionFormattingRule,
    INumberHighlightCell,
    IRankHighlightCell,
    ITextHighlightCell,
} from '../../type';
import { CellValueType } from '@univerjs/core';
import { beforeEach, describe, expect, it } from 'vitest';
import { CFNumberOperator, CFRuleType, CFSubRuleType, CFTextOperator } from '../../../base/const';
import { createTestBed } from './test.util';

describe('Test conditional formatting highlight', () => {
    let testBed: ReturnType<typeof createTestBed>;

    beforeEach(() => {
        testBed && testBed.univer.dispose();
        testBed = createTestBed();
        testBed.getConditionalFormattingViewModel();
    });

    describe('Test Number', async () => {
        // Expected results recorded with Excel COM DisplayFormat for cellIs rules.
        it.each<{ name: string; cell: ICellData; matches: boolean[] }>([
            { name: 'blank', cell: {}, matches: [true, false, true, false, false, true, true, true] },
            { name: 'formula empty string', cell: { f: '=IF(TRUE,"",1)', v: '', t: CellValueType.STRING }, matches: [false, true, false, true, true, false, true, false] },
            { name: 'text', cell: { v: 'abc', t: CellValueType.STRING }, matches: [false, true, false, true, true, false, true, false] },
            { name: 'numeric text', cell: { v: '5', t: CellValueType.FORCE_STRING }, matches: [false, true, false, true, true, false, true, false] },
            { name: 'zero', cell: { v: 0, t: CellValueType.NUMBER }, matches: [true, false, true, false, false, true, true, true] },
            { name: 'five', cell: { v: 5, t: CellValueType.NUMBER }, matches: [true, false, false, true, true, true, true, true] },
            { name: 'true', cell: { v: 1, t: CellValueType.BOOLEAN }, matches: [false, true, false, true, true, false, true, false] },
            { name: 'false', cell: { v: 0, t: CellValueType.BOOLEAN }, matches: [false, true, false, true, true, false, true, false] },
            { name: 'error', cell: { f: '=1/0', v: '#DIV/0!', t: CellValueType.STRING }, matches: [false, false, false, false, false, false, false, false] },
        ])('matches Excel number comparisons for $name', ({ cell, matches }) => {
            const comparisons: [CFNumberOperator, number | [number, number]][] = [
                [CFNumberOperator.between, [0, 10]],
                [CFNumberOperator.notBetween, [0, 10]],
                [CFNumberOperator.equal, 0],
                [CFNumberOperator.notEqual, 0],
                [CFNumberOperator.greaterThan, 0],
                [CFNumberOperator.lessThan, 10],
                [CFNumberOperator.greaterThanOrEqual, 0],
                [CFNumberOperator.lessThanOrEqual, 10],
            ];
            for (const [column, [operator, value]] of comparisons.entries()) {
                (testBed.workbook as Workbook).getSheetBySheetId(testBed.subUnitId)!.getCellMatrix().setValue(0, column, cell);
                testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, {
                    ranges: [{ startRow: 0, endRow: 0, startColumn: column, endColumn: column }],
                    cfId: `comparison-${column}`,
                    stopIfTrue: false,
                    rule: { type: CFRuleType.highlightCell, subType: CFSubRuleType.number, operator, value, style: { bl: 1 } } as INumberHighlightCell,
                });
                expect(testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 0, column))
                    .toEqual({ style: matches[column] ? { bl: 1 } : {} });
            }
        });

        it('Should apply higher-priority rule style when multiple rules are matched', () => {
            const lowerPriorityRule: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#FFC000' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.between,
                    value: [0, 10],
                },
            };

            const higherPriorityRule: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#FFFF00' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.between,
                    value: [0, 10],
                },
            };

            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, lowerPriorityRule);
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, higherPriorityRule);

            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#FFFF00' } } });
        });

        it('Should not stop at stopIfTrue rule when the rule is not matched', () => {
            const shouldApplyLaterRule: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.between,
                    value: [0, 10],
                },
            };

            const stopIfTrueButNotMatched: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: true,
                rule: {
                    style: { bg: { rgb: '#FF0000' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.greaterThan,
                    value: 100,
                },
            };

            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, shouldApplyLaterRule);
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, stopIfTrueButNotMatched);

            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });

        it('Is CFNumberOperator.between', async () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.between,
                    value: [0, 10],
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });

        it('Is not CFNumberOperator.between', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.between,
                    value: [5, 10],
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
        it('Is CFNumberOperator.equal', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.equal,
                    value: 2,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('Is not CFNumberOperator.equal', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.equal,
                    value: 2,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 2);
            expect(result).toEqual({ style: {} });
        });
        it('Is CFNumberOperator.greaterThan', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.greaterThan,
                    value: 0,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('Is not CFNumberOperator.greaterThan', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.greaterThan,
                    value: 2,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
        it('Is CFNumberOperator.greaterThanOrEqual', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.greaterThanOrEqual,
                    value: 0,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('Is not CFNumberOperator.greaterThanOrEqual', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.greaterThanOrEqual,
                    value: 3,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
        it('Is CFNumberOperator.lessThan', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.lessThan,
                    value: 3,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it(`Is not ${CFNumberOperator.lessThan}`, () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.lessThan,
                    value: 0,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
        it(`Is ${CFNumberOperator.lessThanOrEqual}`, () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.lessThanOrEqual,
                    value: 3,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('Is not CFNumberOperator.lessThanOrEqual', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.lessThanOrEqual,
                    value: 0,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
        it('Is CFNumberOperator.notBetween', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.notBetween,
                    value: [5, 7],
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('Is not CFNumberOperator.notBetween', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.notBetween,
                    value: [0, 3],
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
        it('Is CFNumberOperator.notEqual', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.notEqual,
                    value: 11,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('Is not CFNumberOperator.notEqual', () => {
            const params: IConditionFormattingRule<INumberHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.number,
                    operator: CFNumberOperator.notEqual,
                    value: 2,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
    });
    describe('Test Text', () => {
        it('is CFTextOperator.beginsWith', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.beginsWith,
                    value: '2',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('is not CFTextOperator.beginsWith', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.beginsWith,
                    value: '11',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });

        it('is CFTextOperator.endsWith', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.endsWith,
                    value: '2',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('is not CFTextOperator.endsWith', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.endsWith,
                    value: '11',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });

        it('is CFTextOperator.containsText', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.containsText,
                    value: '2',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('is not CFTextOperator.containsText', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.containsText,
                    value: '11',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
        it('is CFTextOperator.equal', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.containsText,
                    value: '2',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('is not CFTextOperator.equal', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.containsText,
                    value: '11',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
        it('is CFTextOperator.notEqual', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.notEqual,
                    value: '22',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('is not CFTextOperator.notEqual', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.notEqual,
                    value: '2',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
        it('is CFTextOperator.notContainsText', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.notEqual,
                    value: '11',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it('is not CFTextOperator.notContainsText', () => {
            const params: IConditionFormattingRule<ITextHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.text,
                    operator: CFTextOperator.notEqual,
                    value: '2',
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: {} });
        });
    });
    describe('Test average', () => {
        it(`is ${CFNumberOperator.greaterThanOrEqual} average`, () => {
            const params: IConditionFormattingRule<IAverageHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.average,
                    operator: CFNumberOperator.greaterThanOrEqual,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
        it(`is ${CFNumberOperator.lessThanOrEqual} average`, () => {
            const params: IConditionFormattingRule<IAverageHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 2, endColumn: 2 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.average,
                    operator: CFNumberOperator.lessThanOrEqual,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 0);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });
    });
    describe('Test rank', () => {
        it('is bottom 2', () => {
            const params: IConditionFormattingRule<IRankHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 4, endColumn: 4 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.rank,
                    isBottom: true,
                    isPercent: false,
                    value: 2,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 0);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 0);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });

        it('is bottom 6', () => {
            const params: IConditionFormattingRule<IRankHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 4, endColumn: 4 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.rank,
                    isBottom: true,
                    isPercent: false,
                    value: 6,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            const result = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 1, 1);
            expect(result).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
        });

        it('is bottom 22%', () => {
            const params: IConditionFormattingRule<IRankHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 0, endColumn: 8 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.rank,
                    isBottom: true,
                    isPercent: true,
                    value: 22,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const results = [
                testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 0, 0),
                testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 0, 1),
            ];
            expect(results[0]).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
            expect(results[1]).toEqual({ style: {} });
        });

        it('is bottom 23%', () => {
            const params: IConditionFormattingRule<IRankHighlightCell> = {
                ranges: [{ startRow: 0, startColumn: 0, endRow: 0, endColumn: 8 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.rank,
                    isBottom: true,
                    isPercent: true,
                    value: 23,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const results = [
                testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 0, 0),
                testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 0, 1),
                testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 0, 2),
            ];
            expect(results[0]).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
            expect(results[1]).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
            expect(results[2]).toEqual({ style: {} });
        });

        // In this case, 2 items should be highlighted. Where the second item is exactly 20%.
        it('is bottom 20%', () => {
            const params: IConditionFormattingRule<IRankHighlightCell> = {
                ranges: [{ startRow: 7, startColumn: 0, endRow: 7, endColumn: 9 }],
                cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
                stopIfTrue: false,
                rule: {
                    style: { bg: { rgb: '#2AEAEA' } },
                    type: CFRuleType.highlightCell,
                    subType: CFSubRuleType.rank,
                    isBottom: true,
                    isPercent: true,
                    value: 20,
                },
            };
            testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
            const results = [
                testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 7, 0),
                testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 7, 1),
                testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 7, 2),
            ];
            expect(results[0]).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
            expect(results[1]).toEqual({ style: { bg: { rgb: '#2AEAEA' } } });
            expect(results[2]).toEqual({ style: {} });
        });
    });
});