Skip to content

Commit

Permalink
test: 单测修复
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Nov 10, 2023
1 parent 304dbf9 commit 6f9f083
Show file tree
Hide file tree
Showing 30 changed files with 417 additions and 534 deletions.
50 changes: 15 additions & 35 deletions packages/s2-core/__tests__/bugs/issue-725-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,11 @@ import { assembleDataCfg } from '../util';
import type { S2DataConfig } from '@/common/interface';
import { PivotSheet } from '@/sheet-type';
import { PivotDataSet } from '@/data-set';
import { EXTRA_FIELD } from '@/common/constant';
import { getDimensionsWithoutPathPre } from '@/utils/dataset/pivot-data-set';

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

const MockPivotSheet = PivotSheet as unknown as jest.Mock<PivotSheet>;
let dataSet: PivotDataSet;

describe('Multi Measure Correct Render Tests1', () => {
const dataCfg: S2DataConfig = assembleDataCfg({
...mockDataConfig,
});

beforeEach(() => {
MockPivotSheet.mockClear();
const mockSheet = new MockPivotSheet();
dataSet = new PivotDataSet(mockSheet);
dataSet.setDataCfg(dataCfg);
});

test('should get correct values', () => {
expect(
getDimensionsWithoutPathPre(dataSet.getDimensionValues(EXTRA_FIELD)),
).toEqual(['price', 'cost']);
});
});

describe('Group Sort When Have Same Child Measure', () => {
const dataCfg: S2DataConfig = assembleDataCfg({
...mockDataConfig,
Expand All @@ -51,22 +29,24 @@ describe('Group Sort When Have Same Child Measure', () => {
});

beforeEach(() => {
MockPivotSheet.mockClear();
const mockSheet = new MockPivotSheet();
dataSet = new PivotDataSet(mockSheet);
dataSet.setDataCfg(dataCfg);
});

test('should get correct group sort', () => {
expect(
getDimensionsWithoutPathPre(dataSet.getDimensionValues('type')),
).toEqual(['办公用品', '家具产品', '家具产品', '办公用品']);
expect(
getDimensionsWithoutPathPre(
dataSet.getDimensionValues('type', { city: '白山' }),
),
).toEqual(['办公用品', '家具产品']);
expect(
getDimensionsWithoutPathPre(
dataSet.getDimensionValues('type', { city: '抚顺' }),
),
).toEqual(['家具产品', '办公用品']);
expect(dataSet.getDimensionValues('type')).toEqual([
'办公用品',
'家具产品',
]);
expect(dataSet.getDimensionValues('type', { city: '白山' })).toEqual([
'办公用品',
'家具产品',
]);
expect(dataSet.getDimensionValues('type', { city: '抚顺' })).toEqual([
'办公用品',
'家具产品',
]);
});
});
4 changes: 2 additions & 2 deletions packages/s2-core/__tests__/bugs/issue-860-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Empty String Row Value Tests', () => {

test('should get correct row hierarchy with empty row node', () => {
const layoutResult = s2.facet.layoutResult;
expect(layoutResult.rowNodes).toHaveLength(8);
expect(layoutResult.rowLeafNodes).toHaveLength(5);
expect(layoutResult.rowNodes).toHaveLength(7);
expect(layoutResult.rowLeafNodes).toHaveLength(4);
});
});
44 changes: 2 additions & 42 deletions packages/s2-core/__tests__/data/data-custom-trees.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,22 @@
export const dataCustomTrees = [
{
'measure-a': 1,
type: '家具',
sub_type: '桌子',
},
{
'measure-a': 1,
'measure-b': 2,
type: '家具',
sub_type: '桌子',
},
{
'measure-c': 3,
type: '家具',
sub_type: '桌子',
},
{
'measure-d': 4,
type: '家具',
sub_type: '桌子',
},
{
'measure-e': 5,
type: '家具',
sub_type: '桌子',
},
{
'measure-f': 6,
type: '家具',
sub_type: '桌子',
},
{
'measure-a': 11,
type: '家具',
sub_type: '椅子',
},
{
'measure-a': 11,
'measure-b': 22,
type: '家具',
sub_type: '椅子',
},
{
'measure-c': 33,
type: '家具',
sub_type: '椅子',
},
{
'measure-d': 44,
type: '家具',
sub_type: '椅子',
},
{
'measure-e': 55,
type: '家具',
sub_type: '椅子',
},
{
'measure-f': 66,
type: '家具',
sub_type: '椅子',
},
];
19 changes: 1 addition & 18 deletions packages/s2-core/__tests__/data/data-issue-860.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@
"date": "2021-12-01",
"city": "A市",
"type": "电子",
"a_sum": 6
},
{
"date": "2021-12-01",
"city": "A市",
"type": "电子",
"a_sum": 6,
"b_sum": 6
},
{
Expand All @@ -46,18 +41,6 @@
"type": "",
"b_sum": 6
},
{
"date": "2021-12-01",
"city": "A市",
"type": "undefined",
"b_sum": 6
},
{
"date": "2021-12-01",
"city": "A市",
"type": null,
"b_sum": 6
},
{
"date": "2021-12-01",
"city": "B市",
Expand Down
9 changes: 2 additions & 7 deletions packages/s2-core/__tests__/spreadsheet/row-link-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ const s2DataCfg = {
city: '义乌1',
type: '笔',
price: 1,
},

{
province: '浙江',
city: '义乌1',
type: '笔',
cost: 2,
},
{
Expand Down Expand Up @@ -123,14 +117,15 @@ describe('Row Text Link Tests', () => {
},
} as any);

expect(linkFieldJump).toBeCalledWith({
expect(linkFieldJump).toHaveBeenLastCalledWith({
key: 'city',
cellData: rowNode,
record: {
province: '浙江',
city: '义乌1',
type: '笔',
price: 1,
cost: 2,
$$extra$$: 'price',
$$value$$: 1,
rowIndex: 1,
Expand Down
31 changes: 1 addition & 30 deletions packages/s2-core/__tests__/spreadsheet/spread-sheet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,6 @@ describe('SpreadSheet Tests', () => {

expect(s2.dataSet.totalData).toEqual([
{
$$extra$$: 'price',
$$value$$: 3,
cost: 6,
price: 3,
province: '浙江',
type: '笔',
},
{
$$extra$$: 'cost',
$$value$$: 6,
cost: 6,
price: 3,
province: '浙江',
Expand Down Expand Up @@ -472,30 +462,11 @@ describe('SpreadSheet Tests', () => {
);
s2.render();

const totalDataSet = [
{
$$extra$$: 'price',
$$value$$: 3,
cost: 6,
price: 3,
province: '浙江',
type: '笔',
},
{
$$extra$$: 'cost',
$$value$$: 6,
cost: 6,
price: 3,
province: '浙江',
type: '笔',
},
];

// 改变 totalData 为 undefined 再次渲染
s2.setDataCfg({ ...mockDataConfig, totalData: undefined }, false);
s2.render();

expect(s2.dataSet.totalData).toEqual(totalDataSet);
expect(s2.dataSet.totalData).toEqual(totalData);
expect(s2.dataCfg.fields).toEqual({
...mockDataConfig.fields,
customTreeItems: [],
Expand Down
1 change: 0 additions & 1 deletion packages/s2-core/__tests__/spreadsheet/total-group-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('Total Group Dimension Test', () => {
container = getContainer();
s2 = new PivotSheet(container, dataCfg, s2Options as S2Options);
s2.render();
window.s2 = s2;
});

test('should get correct result', () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/s2-core/__tests__/unit/cell/data-cell-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { find, get } from 'lodash';
import { find, get, keys } from 'lodash';
import { createPivotSheet, createTableSheet } from 'tests/util/helpers';
import { renderText } from '@/utils/g-renders';
import { DataCell } from '@/cell';
Expand Down Expand Up @@ -387,9 +387,11 @@ describe('Data Cell Tests', () => {
field: 'cost',
mapping(value, dataInfo) {
const originData = s2.dataSet.originData;
const resultData = find(originData, dataInfo);
expect(resultData).toEqual(dataInfo);
const resultData = find(originData, (item) =>
keys(item).every((key) => item[key] === dataInfo[key]),
);
expect(value).toEqual(resultData.cost);
expect(value).toEqual(dataInfo[VALUE_FIELD]);
return {
fill: '#fffae6',
};
Expand Down
29 changes: 12 additions & 17 deletions packages/s2-core/__tests__/unit/data-process/pivot-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { flattenDeep, get, size, uniq } from 'lodash';
import { assembleDataCfg, assembleOptions } from '../../util';
import { getContainer } from '../../util/helpers';
import { data } from '../../data/mock-dataset.json';
import { EXTRA_FIELD, VALUE_FIELD } from '@/common/constant';
import { VALUE_FIELD } from '@/common/constant';
import type { PivotDataSet } from '@/data-set/pivot-data-set';
import { PivotSheet } from '@/sheet-type';

Expand Down Expand Up @@ -41,52 +41,47 @@ describe('Pivot Table Core Data Process', () => {
});

test('should get correct indexes data', () => {
const prefix = 'province[&]city[&]type[&]sub_type';

const indexesData = ds.indexesData;
expect(flattenDeep(indexesData)).toHaveLength(data.length);
expect(get(indexesData, '0.0.0.0.0')).toEqual({
expect(flattenDeep(indexesData[prefix]).filter(Boolean)).toHaveLength(
data.length,
);

expect(get(indexesData, [prefix, 1, 1, 1, 1])).toEqual({
province: '浙江省',
city: '杭州市',
type: '家具',
sub_type: '桌子',
number: 7789,
[VALUE_FIELD]: 7789,
[EXTRA_FIELD]: 'number',
}); // 左上角
expect(get(indexesData, '0.0.1.1.0')).toEqual({
expect(get(indexesData, [prefix, 1, 1, 2, 2])).toEqual({
province: '浙江省',
city: '杭州市',
type: '办公用品',
sub_type: '纸张',
number: 1343,
[VALUE_FIELD]: 1343,
[EXTRA_FIELD]: 'number',
}); // 右上角
expect(get(indexesData, '1.3.0.0.0')).toEqual({
expect(get(indexesData, [prefix, 2, 4, 1, 1])).toEqual({
province: '四川省',
city: '乐山市',
type: '家具',
sub_type: '桌子',
number: 2330,
[VALUE_FIELD]: 2330,
[EXTRA_FIELD]: 'number',
}); // 左下角
expect(get(indexesData, '1.3.1.1.0')).toEqual({
expect(get(indexesData, [prefix, 2, 4, 2, 2])).toEqual({
province: '四川省',
city: '乐山市',
type: '办公用品',
sub_type: '纸张',
number: 352,
[VALUE_FIELD]: 352,
[EXTRA_FIELD]: 'number',
}); // 右下角
expect(get(indexesData, '0.3.1.0.0')).toEqual({
expect(get(indexesData, [prefix, 1, 4, 2, 1])).toEqual({
province: '浙江省',
city: '舟山市',
type: '办公用品',
sub_type: '笔',
number: 1432,
[VALUE_FIELD]: 1432,
[EXTRA_FIELD]: 'number',
}); // 中间
});
});
Expand Down
Loading

0 comments on commit 6f9f083

Please sign in to comment.