Skip to content

Commit

Permalink
test: 提交单测
Browse files Browse the repository at this point in the history
  • Loading branch information
Harvey Wang authored and wyh888 committed May 14, 2024
1 parent 84007d1 commit b28aa12
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
37 changes: 37 additions & 0 deletions packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
TAB_SEPARATOR,
} from '../../../../src/common/constant';
import { asyncGetAllPlainData, PivotSheet } from '../../../../src';
import { generateRawData } from '../../../util';
import { CopyMIMEType } from '@/common/interface/export';

describe('PivotSheet Export Test', () => {
Expand Down Expand Up @@ -464,4 +465,40 @@ describe('PivotSheet Export Test', () => {
expect(data.split(LINE_SEPARATOR)).toMatchSnapshot();
},
);

// https://github.com/antvis/S2/issues/2718
it('should export the correct amount of data and have no duplicate data', async () => {
const typeCount = 100;
const subTypeCount = 100;

const bigData = generateRawData(
{ province: 10, city: 1 },
{ type: typeCount, sub_type: subTypeCount },
);

const sheet = new PivotSheet(
getContainer(),
assembleDataCfg({
data: bigData,
fields: {
rows: ['type', 'subType'],
columns: ['province', 'city'],
values: ['number'],
},
}),
assembleOptions({}),
);

await sheet.render();
const data = await asyncGetAllPlainData({
sheetInstance: sheet,
split: CSV_SEPARATOR,
formatOptions: true,
});

// row header count + data count
const count = typeCount * subTypeCount + 3;

expect(data.split(LINE_SEPARATOR)).toHaveLength(count);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* eslint-disable jest/expect-expect */
import { slice } from 'lodash';
import { data as originData } from 'tests/data/mock-dataset.json';
import { assembleDataCfg, assembleOptions } from '../../../util';
import {
assembleDataCfg,
assembleOptions,
generateRawData,
} from '../../../util';
import {
createTableSheet,
expectMatchSnapshot,
Expand Down Expand Up @@ -433,4 +437,33 @@ describe('TableSheet Export Test', () => {
formatData: false,
});
});

// https://github.com/antvis/S2/issues/2718
it('should export the correct amount of data and have no duplicate data', async () => {
const bigData = generateRawData(
{ province: 10, city: 10 },
{ type: 10, sub_type: 10 },
);

const tableSheet = new TableSheet(
getContainer(),
assembleDataCfg({
data: bigData,
fields: {
columns: ['province', 'city', 'type', 'sub_type', 'number'],
},
}),
assembleOptions(),
);

await tableSheet.render();
const data = await asyncGetAllPlainData({
sheetInstance: tableSheet,
split: CSV_SEPARATOR,
formatOptions: true,
});

// The first line is the header, so the number of lines should be the same as the number of data plus one
expect(data.split(LINE_SEPARATOR)).toHaveLength(bigData.length + 1);
});
});
28 changes: 28 additions & 0 deletions packages/s2-core/__tests__/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ export const assembleDataCfg = (...dataCfg: Partial<S2DataConfig>[]) => {
return customMerge<S2DataConfig>(DEFAULT_DATA_CONFIG, s2DataCfg, ...dataCfg);
};

export const generateRawData = (
row: Record<string, number>,
col: Record<string, number>,
) => {
const res: Record<string, any>[] = [];

const rowKeys = Object.keys(row);
const colKeys = Object.keys(col);

for (let i = 0; i < row[rowKeys[0]]; i++) {
for (let j = 0; j < row[rowKeys[1]]; j++) {
for (let m = 0; m < col[colKeys[0]]; m++) {
for (let n = 0; n < col[colKeys[1]]; n++) {
res.push({
province: `p${i}`,
city: `c${j}`,
type: `type${m}`,
subType: `subType${n}`,
number: i * n,
});
}
}
}
}

return res;
};

export const TOTALS_OPTIONS: S2Options['totals'] = {
row: {
showGrandTotals: true,
Expand Down

0 comments on commit b28aa12

Please sign in to comment.