From 58faf9f5ab15ee63f3a557b0065123a231a1dcd6 Mon Sep 17 00:00:00 2001 From: Jinke Li Date: Mon, 15 Apr 2024 20:41:07 +0800 Subject: [PATCH] =?UTF-8?q?test(shared):=20=E5=AE=8C=E5=96=84=E5=85=AC?= =?UTF-8?q?=E7=94=A8=E5=8C=85=E5=8D=95=E6=B5=8B=20(#2657)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest.config.base.js | 7 +- .../__tests__/unit/utils/classnames-spec.ts | 12 ++ .../__tests__/unit/utils/drill-down-spec.ts | 178 ++++++++++++++++++ .../{ => unit}/utils/options-spec.ts | 2 +- .../__tests__/unit/utils/resize-spec.ts | 60 ++++++ packages/s2-shared/__tests__/util/helpers.ts | 3 + packages/s2-shared/src/utils/drill-down.ts | 5 +- 7 files changed, 263 insertions(+), 4 deletions(-) create mode 100644 packages/s2-shared/__tests__/unit/utils/classnames-spec.ts create mode 100644 packages/s2-shared/__tests__/unit/utils/drill-down-spec.ts rename packages/s2-shared/__tests__/{ => unit}/utils/options-spec.ts (97%) create mode 100644 packages/s2-shared/__tests__/unit/utils/resize-spec.ts create mode 100644 packages/s2-shared/__tests__/util/helpers.ts diff --git a/jest.config.base.js b/jest.config.base.js index 3f78e863d9..737f27ef59 100644 --- a/jest.config.base.js +++ b/jest.config.base.js @@ -10,8 +10,13 @@ module.exports = { '!**/node_modules/**', '!**/interface/**', '!**/interface.ts', + '!**/i18n/**', + '!**/icons/**', + '!**/constant/**', + '!**/constant.ts', + '!**/index.{ts,tsx,js,vue}', ], - coveragePathIgnorePatterns: ['hd-adapter/index.ts'], + coveragePathIgnorePatterns: ['hd-adapter/index.ts', 'packages/s2-vue'], coverageReporters: ['text', 'clover', 'html'], transformIgnorePatterns: [], testRegex: '/__tests__/*.*(-|\\.)spec\\.(tsx|ts|js|vue)?$', diff --git a/packages/s2-shared/__tests__/unit/utils/classnames-spec.ts b/packages/s2-shared/__tests__/unit/utils/classnames-spec.ts new file mode 100644 index 0000000000..36264a5641 --- /dev/null +++ b/packages/s2-shared/__tests__/unit/utils/classnames-spec.ts @@ -0,0 +1,12 @@ +import { getStrategySheetTooltipClsName } from '../../../src/utils/classnames'; + +describe('classnames test', () => { + test('#getStrategySheetTooltipClsName()', () => { + expect(getStrategySheetTooltipClsName()).toEqual( + 's2-strategy-sheet-tooltip', + ); + expect(getStrategySheetTooltipClsName('test')).toEqual( + 's2-strategy-sheet-tooltip-test', + ); + }); +}); diff --git a/packages/s2-shared/__tests__/unit/utils/drill-down-spec.ts b/packages/s2-shared/__tests__/unit/utils/drill-down-spec.ts new file mode 100644 index 0000000000..9e71d3becb --- /dev/null +++ b/packages/s2-shared/__tests__/unit/utils/drill-down-spec.ts @@ -0,0 +1,178 @@ +import { + Store, + type Node, + type SpreadSheet, + GEvent, + S2Event, + PivotDataSet, +} from '@antv/s2'; +import type { PartDrillDown } from '../../../src/interface'; +import { + buildDrillDownOptions, + defaultPartDrillDownDisplayCondition, + getDrillDownCache, + handleActionIconClick, + handleDrillDown, +} from '../../../src/utils/drill-down'; +import { sleep } from '../../util/helpers'; + +describe('drill-down test', () => { + let s2: SpreadSheet; + + beforeEach(() => { + s2 = { + store: new Store(), + dataCfg: { + fields: { + rows: [], + }, + }, + interaction: {}, + } as unknown as SpreadSheet; + }); + + test('#getDrillDownCache()', () => { + const node = { + id: 'test', + } as Node; + + expect(getDrillDownCache(s2, node)).toMatchInlineSnapshot(` + Object { + "drillDownCurrentCache": undefined, + "drillDownDataCache": Array [], + } + `); + + s2.store.set('drillDownDataCache', [{ rowId: node.id }]); + + expect(getDrillDownCache(s2, node)).toMatchInlineSnapshot(` + Object { + "drillDownCurrentCache": Object { + "rowId": "test", + }, + "drillDownDataCache": Array [ + Object { + "rowId": "test", + }, + ], + } + `); + }); + + test('#handleActionIconClick()', () => { + const callback = jest.fn(); + const emit = jest.fn(); + s2.emit = emit; + const node = { + id: 'test', + spreadsheet: s2, + } as Node; + const event = {} as GEvent; + + handleActionIconClick({ + meta: node, + event, + callback, + }); + + expect(s2.store.get('drillDownNode')).toEqual(node); + expect(callback).toHaveBeenCalledWith({ + cacheDrillFields: [], + disabledFields: [], + event, + sheetInstance: s2, + }); + expect(emit).toHaveBeenCalledWith(S2Event.GLOBAL_ACTION_ICON_CLICK, event); + }); + + test('#defaultPartDrillDownDisplayCondition()', () => { + const node = { + id: 'test', + spreadsheet: s2, + } as Node; + + expect(defaultPartDrillDownDisplayCondition(node)).toEqual(false); + }); + + test('#buildDrillDownOptions()', () => { + const partDrillDown: PartDrillDown = { + fetchData: jest.fn(), + drillItemsNum: 1, + drillConfig: { + dataSet: [], + }, + }; + + const callback = jest.fn(); + + expect( + buildDrillDownOptions( + { + headerActionIcons: [], + }, + null, + callback, + ), + ).toMatchInlineSnapshot(` + Object { + "headerActionIcons": Array [], + } + `); + + expect( + buildDrillDownOptions( + { + headerActionIcons: [], + }, + partDrillDown, + callback, + ), + ).toMatchInlineSnapshot(` + Object { + "headerActionIcons": Array [ + Object { + "action": [Function], + "belongsCell": "rowCell", + "defaultHide": true, + "displayCondition": [Function], + "iconNames": Array [ + "DrillDownIcon", + ], + }, + ], + } + `); + }); + + test('#handleDrillDown()', async () => { + const render = jest.fn(); + const reset = jest.fn(); + + s2.dataSet = { + transformDrillDownData: jest.fn(), + } as unknown as PivotDataSet; + s2.interaction.reset = reset; + s2.render = render; + + const fetchData = () => + Promise.resolve({ + drillField: 'area', + drillData: [], + }); + + handleDrillDown({ + fetchData, + spreadsheet: s2, + drillFields: [], + rows: [], + }); + + await sleep(200); + + expect( + (s2.dataSet as PivotDataSet).transformDrillDownData, + ).toHaveBeenCalledTimes(1); + expect(reset).toHaveBeenCalledTimes(1); + expect(render).toHaveBeenCalledTimes(1); + }); +}); diff --git a/packages/s2-shared/__tests__/utils/options-spec.ts b/packages/s2-shared/__tests__/unit/utils/options-spec.ts similarity index 97% rename from packages/s2-shared/__tests__/utils/options-spec.ts rename to packages/s2-shared/__tests__/unit/utils/options-spec.ts index 3c3f14f9bc..e4799199e3 100644 --- a/packages/s2-shared/__tests__/utils/options-spec.ts +++ b/packages/s2-shared/__tests__/unit/utils/options-spec.ts @@ -1,5 +1,5 @@ import { DEFAULT_STYLE, HOVER_FOCUS_DURATION } from '@antv/s2'; -import { getBaseSheetComponentOptions } from '../../src'; +import { getBaseSheetComponentOptions } from '../../../src/utils/options'; describe('Options Tests', () => { test('should get safety options', () => { diff --git a/packages/s2-shared/__tests__/unit/utils/resize-spec.ts b/packages/s2-shared/__tests__/unit/utils/resize-spec.ts new file mode 100644 index 0000000000..7097678881 --- /dev/null +++ b/packages/s2-shared/__tests__/unit/utils/resize-spec.ts @@ -0,0 +1,60 @@ +import { PivotSheet } from '@antv/s2'; +import { + analyzeAdaptive, + createResizeObserver, +} from '../../../src/utils/resize'; + +describe('resize test', () => { + test('#analyzeAdaptive()', () => { + const container = document.createElement('div'); + + expect(analyzeAdaptive(container)).toEqual({ + container, + adaptiveWidth: true, + adaptiveHeight: true, + }); + + expect(analyzeAdaptive(container, true)).toEqual({ + container, + adaptiveWidth: true, + adaptiveHeight: false, + }); + + expect(analyzeAdaptive(container, false)).toEqual({ + container, + adaptiveWidth: true, + adaptiveHeight: false, + }); + }); + + test('#createResizeObserver()', () => { + const container = document.createElement('div'); + const wrapper = document.createElement('div'); + const s2 = new PivotSheet( + container, + { data: [], fields: { rows: [], columns: [], values: [] } }, + { + width: 200, + height: 200, + }, + ); + + expect( + createResizeObserver({ + s2, + container: null, + wrapper: null, + adaptive: true, + }), + ).toBeUndefined(); + + expect( + createResizeObserver({ + s2, + container, + wrapper, + adaptive: true, + }), + ).toBeFunction(); + }); +}); diff --git a/packages/s2-shared/__tests__/util/helpers.ts b/packages/s2-shared/__tests__/util/helpers.ts new file mode 100644 index 0000000000..2996b34ccc --- /dev/null +++ b/packages/s2-shared/__tests__/util/helpers.ts @@ -0,0 +1,3 @@ +export const sleep = async (timeout = 0) => { + await new Promise((resolve) => setTimeout(resolve, timeout)); +}; diff --git a/packages/s2-shared/src/utils/drill-down.ts b/packages/s2-shared/src/utils/drill-down.ts index e3eba6b856..0f09f2d080 100644 --- a/packages/s2-shared/src/utils/drill-down.ts +++ b/packages/s2-shared/src/utils/drill-down.ts @@ -29,6 +29,7 @@ export type ActionIconCallbackParams = { disabledFields?: string[]; event?: GEvent; }; + /** 下钻 icon 点击回调 */ export type ActionIconCallback = (params: ActionIconCallbackParams) => void; @@ -51,7 +52,7 @@ export const getDrillDownCache = (spreadsheet: SpreadSheet, meta: Node) => { 'drillDownDataCache', [], ) as PartDrillDownDataCache[]; - const cache = drillDownDataCache.find((dc) => dc.rowId === meta.id); + const cache = drillDownDataCache?.find((dc) => dc.rowId === meta.id); return { drillDownDataCache, drillDownCurrentCache: cache, @@ -97,7 +98,7 @@ export const handleActionIconClick = (params: ActionIconParams) => { * @param meta 节点 * @returns */ -const defaultPartDrillDownDisplayCondition = (meta: Node) => { +export const defaultPartDrillDownDisplayCondition = (meta: Node) => { const s2 = meta.spreadsheet; const { fields } = s2.dataCfg; const iconLevel = fields.rows?.length - 1;