Skip to content

Commit

Permalink
chore: merge with 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Jun 3, 2024
2 parents 836dab6 + c54ca82 commit c5c20e7
Show file tree
Hide file tree
Showing 55 changed files with 1,017 additions and 167 deletions.
10 changes: 8 additions & 2 deletions packages/s2-core/__tests__/unit/data-set/pivot-data-set-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import {
totalData as drillDownTotalData,
} from 'tests/data/mock-drill-down-dataset.json';
import { data } from 'tests/data/mock-dataset.json';

import type {
ViewMeta,
SortMethod,
CustomHeaderField,
} from '@/common/interface';
import { EXTRA_FIELD, ORIGIN_FIELD, TOTAL_VALUE } from '@/common/constant';
import {
EXTRA_FIELD,
ORIGIN_FIELD,
TOTAL_VALUE,
SERIES_NUMBER_FIELD,
} from '@/common/constant';
import type { S2DataConfig } from '@/common/interface';
import { PivotSheet } from '@/sheet-type';
import { PivotDataSet } from '@/data-set/pivot-data-set';
Expand Down Expand Up @@ -45,6 +49,7 @@ describe('Pivot Dataset Test', () => {

mockSheet.store = new Store();
mockSheet.interaction = new MockRootInteraction(mockSheet);
mockSheet.getSeriesNumberText = () => '序号';
dataSet = new PivotDataSet(mockSheet);
dataSet.setDataCfg(dataCfg);
});
Expand Down Expand Up @@ -483,6 +488,7 @@ describe('Pivot Dataset Test', () => {
expect(dataSet.getFieldName('not-found-field')).toEqual(
'not-found-field',
);
expect(dataSet.getFieldName(SERIES_NUMBER_FIELD)).toStrictEqual('序号');
// 异常情况
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
13 changes: 8 additions & 5 deletions packages/s2-core/__tests__/unit/data-set/table-data-set-spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/**
* table mode data-set test.
*/
import { assembleDataCfg } from 'tests/util';
import type { S2DataConfig } from '@/common/interface';
import { type S2DataConfig, SERIES_NUMBER_FIELD } from '@/common';
import { SpreadSheet, TableSheet } from '@/sheet-type';
import { TableDataSet } from '@/data-set/table-data-set';

jest.mock('@/sheet-type');
jest.mock('@/facet/layout/node');

const MockTableSheet = TableSheet as any as jest.Mock<TableSheet>;

describe('Table Mode Dataset Test', () => {
Expand Down Expand Up @@ -63,6 +61,7 @@ describe('Table Mode Dataset Test', () => {
MockTableSheet.mockClear();

s2 = new MockTableSheet();
s2.getSeriesNumberText = () => '序号';
dataSet = new TableDataSet(s2);

dataSet.setDataCfg(dataCfg);
Expand All @@ -77,7 +76,7 @@ describe('Table Mode Dataset Test', () => {
${'city'} | ${'城市'} | ${'城市描述'} | ${mockCityFormatter}
${'type'} | ${'类型'} | ${'类型描述'} | ${mockTypeFormatter}
${'sub_type'} | ${'子类型'} | ${'子类型描述'} | ${mockSubTypeFormatter}
${'number'} | ${'数量'} | ${'数量描述'} | ${mockProvinceFormatter}
${'number'} | ${'数量'} | ${'数量描述'} | ${mockNumberFormatter}
`(
'should return correct filed meta when field=$field',
({ field, name, description }) => {
Expand All @@ -86,6 +85,10 @@ describe('Table Mode Dataset Test', () => {
expect(dataSet.getFieldFormatter(field)(null)).toStrictEqual(field);
},
);

test('should get series number field name', () => {
expect(dataSet.getFieldName(SERIES_NUMBER_FIELD)).toStrictEqual('序号');
});
});

describe('test base dataset structure', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Corner Tests', () => {
});

test('should get custom series number text', () => {
s2.options.seriesNumber!.text = 'test';
s2.getSeriesNumberText = () => 'test';
const cornerNode = createCornerNode();

expect(cornerNode.value).toEqual('test');
Expand Down
117 changes: 117 additions & 0 deletions packages/s2-core/__tests__/unit/facet/layout/layout-hooks-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { createFakeSpreadSheet } from 'tests/util/helpers';
import {
layoutArrange,
layoutCoordinate,
layoutHierarchy,
} from '@/facet/layout/layout-hooks';
import { Hierarchy, Node } from '@/index';

describe('layout-hooks test', () => {
const s2 = createFakeSpreadSheet({
width: 600,
height: 480,
});

const node = new Node({
id: '',
key: '',
value: '',
});

const hierarchy = new Hierarchy();

test('#layoutArrange()', () => {
const layoutArrangeFn = jest.fn(() => ['b']);

expect(layoutArrange(s2, ['a'], node, '')).toEqual(['a']);

expect(
layoutArrange(
{ options: { layoutArrange: layoutArrangeFn } },
['a'],
node,
'',
),
).toEqual(['b']);
expect(layoutArrangeFn).toHaveBeenCalledTimes(1);
});

test('#layoutHierarchy()', () => {
const colNode = new Node({
id: 'test',
key: 'test',
value: 'test',
});

const layoutHierarchyFn = jest.fn(() => {
return {
push: [node],
unshift: [colNode],
};
});

expect(
layoutHierarchy(
{
options: {
layoutHierarchy: layoutHierarchyFn,
},
facet: {
getHiddenColumnsInfo: () => colNode,
},
columns: [colNode.id],
},
node,
colNode,
hierarchy,
),
).toEqual(true);

expect(
layoutHierarchy(
{
options: {
layoutHierarchy: layoutHierarchyFn,
},
facet: {
getHiddenColumnsInfo: () => {},
},
columns: [],
},
node,
colNode,
hierarchy,
),
).toEqual(true);
});

test('#layoutCoordinate()', () => {
const layoutCoordinateFn = jest.fn();
const rowNode = new Node({
id: 'rowNode',
key: 'rowNode',
value: 'rowNode',
});
const colNode = new Node({
id: 'colNode',
key: 'colNode',
value: 'colNode',
});

layoutCoordinate(
{ options: { layoutCoordinate: layoutCoordinateFn } },
rowNode,
colNode,
);

expect(layoutCoordinateFn).toHaveBeenCalledTimes(0);

layoutCoordinate(
{ options: { layoutCoordinate: layoutCoordinateFn } },
{ ...rowNode, isLeaf: true },
{ ...colNode, isLeaf: true },
);

expect(layoutCoordinateFn).toHaveBeenCalledTimes(1);
});
});
55 changes: 49 additions & 6 deletions packages/s2-core/__tests__/unit/facet/layout/node-spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
/**
* Node test.
*/

import { SERIES_NUMBER_FIELD } from '../../../../src';
import { Node } from '@/facet/layout/node';

describe('Node Test', () => {
const root = new Node({ id: `root`, field: '', value: '', children: [] });
const root = new Node({
id: 'root',
key: 'root',
value: 'root',
children: [],
});
const child = new Node({
id: 'child',
key: 'child',
value: 'child',
isLeaf: true,
children: [],
});
const node = new Node({
id: `root[&]country`,
id: 'root[&]country',
key: '',
value: '',
field: 'country',
parent: root,
});

node.children = [child];

test('should get correct field path', () => {
expect(Node.getFieldPath(node)).toEqual(['country']);
});

test('#getAllLeaveNodes()', () => {
expect(Node.getAllLeaveNodes(node)).toHaveLength(1);
});

test('#getAllChildrenNodes()', () => {
expect(Node.getAllChildrenNodes(node)).toHaveLength(1);
});

test('#getAllBranch()', () => {
expect(Node.getBranchNodes(node)).toHaveLength(1);
});

test('#rootNode()', () => {
expect(Node.rootNode().id).toEqual('root');
});

test('#getHeadLeafChild()', () => {
expect(node.getHeadLeafChild().id).toEqual('child');
});

test('#getTotalHeightForTreeHierarchy()', () => {
expect(node.getTotalHeightForTreeHierarchy()).toEqual(0);
});

test('#isSeriesNumberNode()', () => {
expect(node.isSeriesNumberNode()).toBeFalsy();

node.field = SERIES_NUMBER_FIELD;
expect(node.isSeriesNumberNode()).toBeTruthy();
});
});
5 changes: 4 additions & 1 deletion packages/s2-core/__tests__/unit/facet/pivot-facet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Canvas, Group, Rect, type CanvasConfig } from '@antv/g';
import { Renderer } from '@antv/g-canvas';
import { find, size } from 'lodash';
import { assembleDataCfg, assembleOptions } from 'tests/util';
import { getDefaultSeriesNumberText } from '../../../src';
import { createFakeSpreadSheet } from '../../util/helpers';
import { getMockPivotMeta } from './util';
import { CornerCell, DataCell } from '@/cell';
Expand Down Expand Up @@ -54,7 +55,8 @@ jest.mock('@/sheet-type', () => {
return {
dataCfg: assembleDataCfg(),
options: assembleOptions({
dataCell: (viewMeta) => new DataCell(viewMeta, viewMeta.spreadsheet),
dataCell: (viewMeta, spreadsheet) =>
new DataCell(viewMeta, spreadsheet),
}),
container,
theme: getTheme({}),
Expand Down Expand Up @@ -96,6 +98,7 @@ jest.mock('@/sheet-type', () => {
},
measureTextWidth:
jest.fn() as unknown as SpreadSheet['measureTextWidth'],
getSeriesNumberText: jest.fn(() => getDefaultSeriesNumberText()),
};
}),
};
Expand Down
24 changes: 2 additions & 22 deletions packages/s2-core/__tests__/unit/facet/table-facet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assembleDataCfg, assembleOptions } from 'tests/util';
import { pick } from 'lodash';
import { data } from '../../data/mock-dataset.json';
import { createFakeSpreadSheet } from '../../util/helpers';
import { getDefaultSeriesNumberText } from '../../../src';
import { LayoutWidthType, ROOT_NODE_ID } from '@/common/constant';
import { Store } from '@/common/store';
import { TableDataSet } from '@/data-set/table-data-set';
Expand Down Expand Up @@ -82,6 +83,7 @@ jest.mock('@/sheet-type', () => {
isCustomColumnFields: jest.fn(),
measureTextWidthRoughly: jest.fn(),
measureTextWidth: jest.fn(),
getSeriesNumberText: jest.fn(() => getDefaultSeriesNumberText()),
};
}),
};
Expand Down Expand Up @@ -152,28 +154,6 @@ describe('Table Mode Facet Test', () => {
expect(rowsHierarchy.getIndexNodes()).toHaveLength(0);
});

test('should get default seriesNumberText', () => {
const { facet } = createMockTableFacet({
seriesNumber: {
enable: true,
},
});

expect(facet.getColLeafNodes()[0].value).toEqual('序号');
});

test('should get custom seriesNumberText', () => {
const seriesNumberText = 'test';
const { facet } = createMockTableFacet({
seriesNumber: {
enable: true,
text: seriesNumberText,
},
});

expect(facet.getColLeafNodes()[0].value).toEqual(seriesNumberText);
});

describe('should get none layer when dataCfg.fields is empty', () => {
const spreadsheet = createFakeSpreadSheet({
s2DataConfig: {
Expand Down
21 changes: 21 additions & 0 deletions packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,27 @@ describe('PivotSheet Tests', () => {
expect(s2.options.seriesNumber?.enable).toBeTruthy();
});

test.each([
{
enable: true,
text: '测试',
result: '测试',
},
{
enable: false,
text: '测试',
result: '',
},
])(
'should get correctly series number text by %o',
({ result, ...options }) => {
s2.setOptions({
seriesNumber: options,
});
expect(s2.getSeriesNumberText()).toEqual(result);
},
);

test('should init new tooltip', () => {
const tooltipDestroySpy = jest
.spyOn(s2.tooltip, 'destroy')
Expand Down
Loading

0 comments on commit c5c20e7

Please sign in to comment.