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.

test: fix sheets filter panel tests

Wenzhao Hue78f00a6ed0e
diff --git a/packages/sheets-filter-ui/src/commands/operations/sheets-filter.operation.ts b/packages/sheets-filter-ui/src/commands/operations/sheets-filter.operation.ts
index c26ecc7a63..3498b4331c 100644
--- a/packages/sheets-filter-ui/src/commands/operations/sheets-filter.operation.ts
+++ b/packages/sheets-filter-ui/src/commands/operations/sheets-filter.operation.ts
@@ -50,9 +50,7 @@ export const OpenFilterPanelOperation: IOperation<IOpenFilterPanelOperationParam
const filterModel = sheetsFilterService.getFilterModel(unitId, subUnitId);
if (!filterModel) return false;
- const result = sheetsFilterPanelService.setupCol(filterModel, col);
- if (!result) return false;
-
+ sheetsFilterPanelService.setupCol(filterModel, col);
if (!contextService.getContextValue(FILTER_PANEL_OPENED_KEY)) {
contextService.setContextValue(FILTER_PANEL_OPENED_KEY, true);
}
diff --git a/packages/sheets-filter-ui/src/services/__tests__/sheets-filter-panel.service.spec.ts b/packages/sheets-filter-ui/src/services/__tests__/sheets-filter-panel.service.spec.ts
index ccd588b07b..f53791d631 100644
--- a/packages/sheets-filter-ui/src/services/__tests__/sheets-filter-panel.service.spec.ts
+++ b/packages/sheets-filter-ui/src/services/__tests__/sheets-filter-panel.service.spec.ts
@@ -202,7 +202,15 @@ describe('test "SheetsFilterPanelService"', () => {
});
describe('test filter by values', () => {
- it('should initialize by filters when the filter column holds "filters"', () => {
+ beforeEach(() => {
+ vitest.useFakeTimers();
+ });
+
+ afterEach(() => {
+ vitest.useRealTimers();
+ });
+
+ it('should initialize by filters when the filter column holds "filters"', async () => {
prepare(WithValuesFilterModelFactory());
expect(commandService.syncExecuteCommand(OpenFilterPanelOperation.id, {
@@ -210,6 +218,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 0,
} as IOpenFilterPanelOperationParams)).toBeTruthy();
+ await await tick();
expect(sheetsFilterPanelService.filterBy).toBe(FilterBy.VALUES);
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
@@ -217,7 +226,7 @@ describe('test "SheetsFilterPanelService"', () => {
expect(filterByModel.filterItems).toEqual(ITEMS);
});
- it('should initialize with blank content', () => {
+ it('should initialize with blank content', async () => {
prepare(WithValuesAndEmptyFilterModelFactory());
expect(commandService.syncExecuteCommand(OpenFilterPanelOperation.id, {
@@ -225,6 +234,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 0,
} as IOpenFilterPanelOperationParams)).toBeTruthy();
+ await tick();
expect(sheetsFilterPanelService.filterBy).toBe(FilterBy.VALUES);
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
@@ -238,7 +248,7 @@ describe('test "SheetsFilterPanelService"', () => {
}]);
});
- it('should count empty cells', () => {
+ it('should count empty cells', async () => {
prepare(WithMultiEmptyCellsModelFactory());
expect(commandService.syncExecuteCommand(OpenFilterPanelOperation.id, {
@@ -246,6 +256,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 0,
} as IOpenFilterPanelOperationParams)).toBeTruthy();
+ await tick();
expect(sheetsFilterPanelService.filterBy).toBe(FilterBy.VALUES);
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
@@ -260,7 +271,7 @@ describe('test "SheetsFilterPanelService"', () => {
});
});
- it('should filter out the items from the panel if its row is already filtered out by other column', () => {
+ it('should filter out the items from the panel if its row is already filtered out by other column', async () => {
prepare(WithTwoFilterColumnsFactory());
expect(commandService.syncExecuteCommand(OpenFilterPanelOperation.id, {
@@ -268,6 +279,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 1,
} as IOpenFilterPanelOperationParams)).toBeTruthy();
+ await tick();
expect(sheetsFilterPanelService.filterBy).toBe(FilterBy.VALUES);
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
@@ -305,7 +317,7 @@ describe('test "SheetsFilterPanelService"', () => {
]);
});
- it('merged cell should use value of the top left corner', () => {
+ it('merged cell should use value of the top left corner', async () => {
prepare(WithMergedCellFilterFactory());
expect(commandService.syncExecuteCommand(OpenFilterPanelOperation.id, {
@@ -313,6 +325,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 1,
} as IOpenFilterPanelOperationParams)).toBeTruthy();
+ await tick();
expect(sheetsFilterPanelService.filterBy).toBe(FilterBy.VALUES);
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
@@ -343,7 +356,7 @@ describe('test "SheetsFilterPanelService"', () => {
]);
});
- it('should update the filter items when toggle checked status', () => {
+ it('should update the filter items when toggle checked status', async () => {
prepare(WithValuesFilterModelFactory());
expect(commandService.syncExecuteCommand(OpenFilterPanelOperation.id, {
@@ -351,6 +364,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 0,
} as IOpenFilterPanelOperationParams)).toBe(true);
+ await tick();
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
filterByModel.onFilterCheckToggled(ITEMS[0], true);
@@ -376,6 +390,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 0,
} as IOpenFilterPanelOperationParams)).toBe(true);
+ await tick();
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
filterByModel.onFilterCheckToggled(ITEMS[0], true);
@@ -388,7 +403,7 @@ describe('test "SheetsFilterPanelService"', () => {
expect(filterModel!.filteredOutRows).toEqual(new Set([1, 2, 3, 4, 5, 7, 8, 9, 10]));
});
- describe('with searching', () => {
+ describe('with searching', async () => {
// use fake timers
// for performance reasons, search string are throttled to change the search results
beforeEach(() => {
@@ -399,7 +414,7 @@ describe('test "SheetsFilterPanelService"', () => {
vitest.useRealTimers();
});
- it('should update the filter items when searching', () => {
+ it('should update the filter items when searching', async () => {
prepare(WithValuesFilterModelFactory());
expect(commandService.syncExecuteCommand(OpenFilterPanelOperation.id, {
@@ -407,6 +422,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 0,
} as IOpenFilterPanelOperationParams)).toBe(true);
+ await tick();
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
@@ -415,7 +431,7 @@ describe('test "SheetsFilterPanelService"', () => {
expect(filterByModel.filterItems).toEqual(E_ITEMS);
});
- it('should toggle all applied to searched results', () => {
+ it('should toggle all applied to searched results', async () => {
prepare(WithValuesFilterModelFactory());
expect(commandService.syncExecuteCommand(OpenFilterPanelOperation.id, {
@@ -423,6 +439,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 0,
} as IOpenFilterPanelOperationParams)).toBe(true);
+ await tick();
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
@@ -433,7 +450,7 @@ describe('test "SheetsFilterPanelService"', () => {
expect(filterByModel.rawFilterItems.filter((i) => i.checked).length).toBe(8); // the original "1" should be checked as well
});
- it('should filter only applied to searched results', () => {
+ it('should filter only applied to searched results', async () => {
prepare(WithValuesFilterModelFactory());
expect(commandService.syncExecuteCommand(OpenFilterPanelOperation.id, {
@@ -441,6 +458,7 @@ describe('test "SheetsFilterPanelService"', () => {
subUnitId: 'sheet1',
col: 0,
} as IOpenFilterPanelOperationParams)).toBe(true);
+ await tick();
const filterByModel = sheetsFilterPanelService.filterByModel as ByValuesModel;
@@ -463,3 +481,9 @@ describe('test "SheetsFilterPanelService"', () => {
});
});
});
+
+function tick(milliseconds: number = 0): Promise<void> {
+ const result = new Promise<void>((resolve) => setTimeout(resolve, milliseconds));
+ vitest.advanceTimersByTime(milliseconds + 1);
+ return result;
+}
diff --git a/packages/sheets-filter-ui/src/services/sheets-filter-panel.service.ts b/packages/sheets-filter-ui/src/services/sheets-filter-panel.service.ts
index de364ea0ef..263e138f55 100644
--- a/packages/sheets-filter-ui/src/services/sheets-filter-panel.service.ts
+++ b/packages/sheets-filter-ui/src/services/sheets-filter-panel.service.ts
@@ -16,7 +16,6 @@
import type { IDisposable, IRange, Nullable } from '@univerjs/core';
import { createIdentifier, Disposable, ICommandService, Inject, Injector, IUniverInstanceService, LocaleService, Quantity } from '@univerjs/core';
-import { SheetsFilterService } from '@univerjs/sheets-filter';
import type { FilterColumn, FilterModel, IFilterColumn } from '@univerjs/sheets-filter';
import type { Observable } from 'rxjs';
import { BehaviorSubject, combineLatest, map, merge, of, ReplaySubject, shareReplay, startWith, Subject, throttleTime } from 'rxjs';
@@ -100,8 +99,6 @@ export class SheetsFilterPanelService extends Disposable {
constructor(
@Inject(Injector) private readonly _injector: Injector,
- @Inject(SheetsFilterService) private _sheetsFilterService: SheetsFilterService,
- @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@Inject(RefRangeService) private readonly _refRangeService: RefRangeService
) {
super();
@@ -113,38 +110,37 @@ export class SheetsFilterPanelService extends Disposable {
this._hasCriteria$.complete();
}
- setupCol(filterModel: FilterModel, col: number): { result: Promise<boolean> } {
+ setupCol(filterModel: FilterModel, col: number): void {
this.terminate();
this._filterModel = filterModel;
this._col$.next(col);
- let result;
-
// We use filter type that (if) has been set on the column as the default filter type.
const filterColumn = filterModel.getFilterColumn(col);
if (filterColumn) {
const info = filterColumn.getColumnData();
if (info.customFilters) {
this._hasCriteria$.next(true);
- result = { result: this._setupByConditions(filterModel, col) };
+ this._setupByConditions(filterModel, col);
+ return;
}
if (info.filters) {
this._hasCriteria$.next(true);
- result = { result: this._setupByValues(filterModel, col) };
+ this._setupByValues(filterModel, col);
+ return;
}
// Use value values by default.
this._hasCriteria$.next(false);
- result = { result: this._setupByValues(filterModel, col) };
+ this._setupByValues(filterModel, col);
+ return;
}
// By default we filter by values.
this._hasCriteria$.next(false);
- result = { result: this._setupByValues(filterModel, col) };
-
- return result;
+ this._setupByValues(filterModel, col);
};
changeFilterBy(filterBy: FilterBy): boolean {
@@ -207,13 +203,13 @@ export class SheetsFilterPanelService extends Disposable {
const range = filterModel.getRange();
if (range.startRow === range.endRow) return false;
- const model = await ByValuesModel.fromFilterColumn(
+ const filterByModel = await ByValuesModel.fromFilterColumn(
this._injector,
filterModel,
col
);
- this.filterByModel = model;
+ this.filterByModel = filterByModel;
this._filterBy$.next(FilterBy.VALUES);
this._listenToFilterHeaderChange(filterModel, col);