Skip to content

Commit

Permalink
test(shared): 完善公用包单测 (#2657)
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 authored Apr 15, 2024
1 parent 821e2ac commit 58faf9f
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 4 deletions.
7 changes: 6 additions & 1 deletion jest.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)?$',
Expand Down
12 changes: 12 additions & 0 deletions packages/s2-shared/__tests__/unit/utils/classnames-spec.ts
Original file line number Diff line number Diff line change
@@ -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',
);
});
});
178 changes: 178 additions & 0 deletions packages/s2-shared/__tests__/unit/utils/drill-down-spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
60 changes: 60 additions & 0 deletions packages/s2-shared/__tests__/unit/utils/resize-spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
3 changes: 3 additions & 0 deletions packages/s2-shared/__tests__/util/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const sleep = async (timeout = 0) => {
await new Promise((resolve) => setTimeout(resolve, timeout));
};
5 changes: 3 additions & 2 deletions packages/s2-shared/src/utils/drill-down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type ActionIconCallbackParams = {
disabledFields?: string[];
event?: GEvent;
};

/** 下钻 icon 点击回调 */
export type ActionIconCallback = (params: ActionIconCallbackParams) => void;

Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 58faf9f

Please sign in to comment.