From 10fa34ad4293f5b1e58154485cc185cbcc297596 Mon Sep 17 00:00:00 2001 From: lijinke666 Date: Fri, 10 Nov 2023 11:05:41 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E4=BF=AE=E5=A4=8D=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../custom-table-col-spec.ts.snap | 2 +- .../spreadsheet/custom-table-col-spec.ts | 2 +- .../spreadsheet/layout-hooks-spec.ts | 8 ++------ .../__tests__/unit/facet/table-facet-spec.ts | 5 ++++- .../base-interaction/hover-spec.ts | 19 ++++++++++--------- .../unit/interaction/event-controller-spec.ts | 3 +++ .../interaction/row-column-resize-spec.ts | 12 ++++++------ .../unit/sheet-type/pivot-sheet-spec.ts | 2 +- packages/s2-core/src/facet/pivot-facet.ts | 2 +- .../src/interaction/event-controller.ts | 6 +++--- .../s2-core/src/sheet-type/spread-sheet.ts | 4 ++-- 11 files changed, 34 insertions(+), 31 deletions(-) diff --git a/packages/s2-core/__tests__/spreadsheet/__snapshots__/custom-table-col-spec.ts.snap b/packages/s2-core/__tests__/spreadsheet/__snapshots__/custom-table-col-spec.ts.snap index f14925d59e..e607dd4387 100644 --- a/packages/s2-core/__tests__/spreadsheet/__snapshots__/custom-table-col-spec.ts.snap +++ b/packages/s2-core/__tests__/spreadsheet/__snapshots__/custom-table-col-spec.ts.snap @@ -44,7 +44,7 @@ Array [ "value": "类型", }, Object { - "height": 30, + "height": 100, "value": "金额", }, Object { diff --git a/packages/s2-core/__tests__/spreadsheet/custom-table-col-spec.ts b/packages/s2-core/__tests__/spreadsheet/custom-table-col-spec.ts index b637d4ac0a..3cfbb7dbb1 100644 --- a/packages/s2-core/__tests__/spreadsheet/custom-table-col-spec.ts +++ b/packages/s2-core/__tests__/spreadsheet/custom-table-col-spec.ts @@ -62,7 +62,7 @@ describe('TableSheet Custom Tests', () => { }); afterEach(() => { - s2.destroy(); + // s2.destroy(); }); test('should render custom layout column nodes', () => { diff --git a/packages/s2-core/__tests__/spreadsheet/layout-hooks-spec.ts b/packages/s2-core/__tests__/spreadsheet/layout-hooks-spec.ts index 7539423bc2..f65d059715 100644 --- a/packages/s2-core/__tests__/spreadsheet/layout-hooks-spec.ts +++ b/packages/s2-core/__tests__/spreadsheet/layout-hooks-spec.ts @@ -136,10 +136,6 @@ const s2Options: S2Options = { collapseAll: false, width: 120, }, - colCell: { - widthByField: {}, - heightByField: {}, - }, dataCell: { height: 32, }, @@ -164,7 +160,7 @@ describe('layout hooks spec', () => { test('layout arrange hook', () => { const rowLeafNodes = s2.facet.getRowLeafNodes(); - let arrangeValues; + let arrangeValues: string[]; if (s2.isHierarchyTreeType()) { arrangeValues = rowLeafNodes.slice(2, 5).map((v) => v.value); @@ -177,7 +173,7 @@ describe('layout hooks spec', () => { test('layout hierarchy hook', () => { const rowLeafNodes = s2.facet.getRowLeafNodes(); - let addValues; + let addValues: string[]; if (s2.isHierarchyTreeType()) { addValues = rowLeafNodes.slice(5, 8).map((v) => v.value); diff --git a/packages/s2-core/__tests__/unit/facet/table-facet-spec.ts b/packages/s2-core/__tests__/unit/facet/table-facet-spec.ts index 1b1615755e..7a8f38966f 100644 --- a/packages/s2-core/__tests__/unit/facet/table-facet-spec.ts +++ b/packages/s2-core/__tests__/unit/facet/table-facet-spec.ts @@ -46,12 +46,13 @@ jest.mock('@/sheet-type', () => { isTableMode: jest.fn().mockReturnValue(true), isPivotMode: jest.fn(), getTotalsConfig: jest.fn(), - getLayoutWidthType: jest.fn().mockRejectedValue('adaptive'), + getLayoutWidthType: jest.fn().mockReturnValue('adaptive'), emit: jest.fn(), facet: { getColLeafNodes: jest.fn().mockReturnValue([]), getColNodes: jest.fn().mockReturnValue([]), getHiddenColumnsInfo: jest.fn(), + getColNodeHeight: jest.fn(), }, isHierarchyTreeType: jest.fn(), getCanvasElement: () => @@ -59,6 +60,8 @@ jest.mock('@/sheet-type', () => { hideTooltip: jest.fn(), interaction: { clearHoverTimer: jest.fn(), + getState: jest.fn(), + getCells: jest.fn(() => []), }, enableFrozenHeaders() { return false; diff --git a/packages/s2-core/__tests__/unit/interaction/base-interaction/hover-spec.ts b/packages/s2-core/__tests__/unit/interaction/base-interaction/hover-spec.ts index 23ce80a704..42bfa5ec4d 100644 --- a/packages/s2-core/__tests__/unit/interaction/base-interaction/hover-spec.ts +++ b/packages/s2-core/__tests__/unit/interaction/base-interaction/hover-spec.ts @@ -1,5 +1,9 @@ import { omit } from 'lodash'; -import { createFakeSpreadSheet, sleep } from 'tests/util/helpers'; +import { + createFakeSpreadSheet, + createFederatedMouseEvent, + sleep, +} from 'tests/util/helpers'; import type { GEvent } from '@/index'; import type { CellEventTarget, S2Options, ViewMeta } from '@/common/interface'; import { HoverEvent } from '@/interaction/base-interaction/hover'; @@ -47,7 +51,7 @@ describe('Interaction Hover Tests', () => { return mockCell; }, - isTextOverflowing: jest.fn(), + isTextOverflowing: jest.fn(() => true), getActualText: () => ELLIPSIS_SYMBOL, getFieldValue: () => '', cellType: 'dataCell', @@ -167,12 +171,9 @@ describe('Interaction Hover Tests', () => { // click date cell before will trigger hover focus await sleep(HOVER_FOCUS_DURATION - 200); - const mockEvent = { - preventDefault: () => {}, - originalEvent: {}, - }; + const event = createFederatedMouseEvent(s2, OriginEventType.MOUSE_DOWN); - s2.container.emit(OriginEventType.MOUSE_DOWN, mockEvent); + s2.container.dispatchEvent(event); await sleep(200); @@ -305,9 +306,9 @@ describe('Interaction Hover Tests', () => { return mockCell; }, - getActualText: () => 'test', + getActualText: () => `test`, getFieldValue: () => 'test', - isTextOverflowing: jest.fn(), + isTextOverflowing: jest.fn(() => false), cellType: 'dataCell', }) as any; diff --git a/packages/s2-core/__tests__/unit/interaction/event-controller-spec.ts b/packages/s2-core/__tests__/unit/interaction/event-controller-spec.ts index f18aad9fe1..ba67172c53 100644 --- a/packages/s2-core/__tests__/unit/interaction/event-controller-spec.ts +++ b/packages/s2-core/__tests__/unit/interaction/event-controller-spec.ts @@ -137,6 +137,7 @@ describe('Interaction Event Controller Tests', () => { ); spreadsheet.interaction.reset = jest.fn(); spreadsheet.interaction.removeIntercepts = jest.fn(); + spreadsheet.interaction.hasIntercepts = jest.fn(); spreadsheet.interaction.intercepts.clear(); spreadsheet.getCell = () => ({}) as any; spreadsheet.options = s2Options; @@ -483,6 +484,7 @@ describe('Interaction Event Controller Tests', () => { spreadsheet.interaction.addIntercepts([ InterceptType.DATA_CELL_BRUSH_SELECTION, ]); + spreadsheet.interaction.hasIntercepts = jest.fn(() => true); const reset = jest.fn(); spreadsheet.on(S2Event.GLOBAL_RESET, reset); @@ -491,6 +493,7 @@ describe('Interaction Event Controller Tests', () => { expect(spreadsheet.interaction.removeIntercepts).toHaveBeenCalled(); expect(reset).not.toHaveBeenCalled(); + spreadsheet.interaction.hasIntercepts = jest.fn(); }); test('should not reset if current mouse on the canvas container', () => { diff --git a/packages/s2-core/__tests__/unit/interaction/row-column-resize-spec.ts b/packages/s2-core/__tests__/unit/interaction/row-column-resize-spec.ts index 1e299d9600..26b133a534 100644 --- a/packages/s2-core/__tests__/unit/interaction/row-column-resize-spec.ts +++ b/packages/s2-core/__tests__/unit/interaction/row-column-resize-spec.ts @@ -260,7 +260,7 @@ describe('Interaction Row Column Resize Tests', () => { height: 0, isResizeArea: true, effect: ResizeAreaEffect.Cell, - resizedWidth: 40, + resizedWidth: 42, resizedHeight: 0, size: 3, meta: { @@ -304,9 +304,9 @@ describe('Interaction Row Column Resize Tests', () => { info: resizeInfo, style: { colCell: { - width: 40, + width: resizeInfo.resizedWidth!, widthByField: { - [resizeInfo.meta.field!]: 40, + [resizeInfo.meta.field!]: resizeInfo.resizedWidth!, }, }, }, @@ -317,11 +317,11 @@ describe('Interaction Row Column Resize Tests', () => { // update style options expect(s2.options.style!.colCell).toEqual({ - width: 40, + width: resizeInfo.resizedWidth!, height: 30, heightByField: null, widthByField: { - [resizeInfo.meta.field!]: 40, + [resizeInfo.meta.field!]: resizeInfo.resizedWidth!, }, }); @@ -768,7 +768,7 @@ describe('Interaction Row Column Resize Tests', () => { expect(disable).toHaveBeenCalledWith({ ...resizeInfo, resizedWidth: 0, - resizedHeight: 16, + resizedHeight: 40, }); emitResizeEvent( diff --git a/packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts b/packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts index 46ea3cc5d3..0a9d330209 100644 --- a/packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts +++ b/packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts @@ -935,7 +935,7 @@ describe('PivotSheet Tests', () => { .mockImplementation((_, __, options) => { return { forceRender: options?.forceRender, - }; + } as unknown as void; }); const nodeMeta = new Node({ id: '1', field: '1', value: 'testValue' }); diff --git a/packages/s2-core/src/facet/pivot-facet.ts b/packages/s2-core/src/facet/pivot-facet.ts index b8af9b362b..c02cd70170 100644 --- a/packages/s2-core/src/facet/pivot-facet.ts +++ b/packages/s2-core/src/facet/pivot-facet.ts @@ -415,7 +415,7 @@ export class PivotFacet extends BaseFacet { } private getRowNodeHeight(rowNode: Node): number { - const rowCell = new RowCell(rowNode, rowNode.spreadsheet, {}); + const rowCell = new RowCell(rowNode, this.spreadsheet, {}); const defaultHeight = this.getRowCellHeight(rowNode); return this.getCellAdaptiveHeight(rowCell, defaultHeight); diff --git a/packages/s2-core/src/interaction/event-controller.ts b/packages/s2-core/src/interaction/event-controller.ts index 21e3aab5e0..6482d607b4 100644 --- a/packages/s2-core/src/interaction/event-controller.ts +++ b/packages/s2-core/src/interaction/event-controller.ts @@ -153,13 +153,13 @@ export class EventController { const { interaction } = this.spreadsheet; if ( - interaction.hasIntercepts([ + interaction?.hasIntercepts?.([ InterceptType.DATA_CELL_BRUSH_SELECTION, InterceptType.COL_CELL_BRUSH_SELECTION, InterceptType.ROW_CELL_BRUSH_SELECTION, ]) ) { - interaction.removeIntercepts([ + interaction?.removeIntercepts?.([ InterceptType.DATA_CELL_BRUSH_SELECTION, InterceptType.ROW_CELL_BRUSH_SELECTION, InterceptType.COL_CELL_BRUSH_SELECTION, @@ -176,7 +176,7 @@ export class EventController { } this.spreadsheet.emit(S2Event.GLOBAL_RESET, event); - interaction.reset(); + interaction?.reset(); } private isMouseEvent(event: Event): event is MouseEvent { diff --git a/packages/s2-core/src/sheet-type/spread-sheet.ts b/packages/s2-core/src/sheet-type/spread-sheet.ts index b4a2813edc..ee96189281 100644 --- a/packages/s2-core/src/sheet-type/spread-sheet.ts +++ b/packages/s2-core/src/sheet-type/spread-sheet.ts @@ -287,7 +287,7 @@ export abstract class SpreadSheet extends EE { event: CanvasEvent | MouseEvent, cellInfos: TooltipData[], options?: TooltipOptions, - ) { + ): Promise | void { const { enable: showTooltip, content } = getTooltipOptions(this, event)!; if (!showTooltip) { @@ -307,7 +307,7 @@ export abstract class SpreadSheet extends EE { }, }); - this.showTooltip({ + return this.showTooltip({ data: tooltipData, position: { x: event.clientX,