Skip to content

Commit

Permalink
test: 修复 s2-react 下所有 单测
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Jan 18, 2024
1 parent 5636b3b commit c0503df
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 771 deletions.
8 changes: 3 additions & 5 deletions packages/s2-react/__tests__/spreadsheet/drill-down-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ const partDrillDownParams: SheetComponentsProps['partDrillDown'] = {
}),
};

const findDrillDownIcon = (instance: SpreadSheet) => {
const rowHeaderActionIcons = instance.facet
const findDrillDownIcon = (s2: SpreadSheet) => {
const rowHeaderActionIcons = s2.facet
?.getRowCells()
?.find((cell) => cell.getActualText() === '杭州')
?.getActionIcons();

return rowHeaderActionIcons?.find(
(icon) => icon.attr('name') === 'DrillDownIcon',
);
return rowHeaderActionIcons?.find((icon) => icon.name === 'DrillDownIcon');
};

describe('Spread Sheet Drill Down Tests', () => {
Expand Down
141 changes: 73 additions & 68 deletions packages/s2-react/__tests__/spreadsheet/filter-sheet-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { DeviceType, S2Event, SpreadSheet, type S2DataConfig } from '@antv/s2';
import { Button, Space } from 'antd';
import React from 'react';
import { waitFor } from '@testing-library/react';
import type { Root } from 'react-dom/client';
import {
getContainer,
getMockData,
Expand Down Expand Up @@ -120,103 +122,106 @@ function MainLayout() {

describe('table sheet filter spec', () => {
let container: HTMLDivElement;
let unmount: Root['unmount'];

const filterKey = 'customer_type';
const filteredValue = '消费者';

beforeEach(() => {
container = getContainer();

renderComponent(<MainLayout />);
unmount = renderComponent(<MainLayout />);
});

afterEach(() => {
container?.remove();
unmount?.();
});

test('filter customer_type values', async () => {
await sleep(1000);

s2.emit(S2Event.RANGE_FILTER, {
filterKey,
filteredValues: [filteredValue],
});

expect(s2.facet.getCellRange()).toStrictEqual({
end: 465,
start: 0,
});
expect(s2.dataSet.getDisplayDataSet()).toHaveLength(466);
expect(
s2.dataSet
.getDisplayDataSet()
.some((item) => item['customer_type'] === filteredValue),
).toBeFalsy();
});

test('reset filter params on customer_type', async () => {
await sleep(1000);

s2.emit(S2Event.RANGE_FILTER, {
filterKey,
filteredValues: [filteredValue],
});

s2.emit(S2Event.RANGE_FILTER, {
filterKey,
filteredValues: [],
});

expect(s2.facet.getCellRange()).toStrictEqual({
end: 999,
start: 0,
});
expect(s2.dataSet.getDisplayDataSet()).toHaveLength(1000);
});

test('filtered event fired with new data', async () => {
let dataLength = 0;

await sleep(1000);

s2.on(S2Event.RANGE_FILTERED, (data) => {
dataLength = data.length;
expect(data.length).toStrictEqual(466);
await waitFor(() => {
s2.emit(S2Event.RANGE_FILTER, {
filterKey,
filteredValues: [filteredValue],
});

expect(s2.facet.getCellRange()).toStrictEqual({
end: 465,
start: 0,
});
expect(s2.dataSet.getDisplayDataSet()).toHaveLength(466);
expect(
s2.dataSet
.getDisplayDataSet()
.some((item) => item['customer_type'] === filteredValue),
).toBeFalsy();
});
});

s2.emit(S2Event.RANGE_FILTER, {
filterKey,
filteredValues: [filteredValue],
test('reset filter params on customer_type', async () => {
await waitFor(() => {
s2.emit(S2Event.RANGE_FILTER, {
filterKey,
filteredValues: [filteredValue],
});

s2.emit(S2Event.RANGE_FILTER, {
filterKey,
filteredValues: [],
});

expect(s2.facet.getCellRange()).toStrictEqual({
end: 999,
start: 0,
});
expect(s2.dataSet.getDisplayDataSet()).toHaveLength(1000);
});
});

await sleep(50);

expect(dataLength).toStrictEqual(468);
test('filtered event fired with new data', async () => {
await waitFor(async () => {
let dataLength = 0;

s2.on(S2Event.RANGE_FILTERED, (data) => {
dataLength = data.length;
expect(data.length).toStrictEqual(466);
expect(s2.dataSet.getDisplayDataSet()).toHaveLength(466);
expect(
s2.dataSet
.getDisplayDataSet()
.some((item) => item['customer_type'] === filteredValue),
).toBeFalsy();
});

s2.emit(S2Event.RANGE_FILTER, {
filterKey,
filteredValues: [filteredValue],
});

await sleep(50);

expect(dataLength).toStrictEqual(466);
});
});

test('falsy/nullish data should not be filtered with irrelevant filter params', async () => {
let dataLength = 0;
await waitFor(async () => {
let dataLength = 0;

await sleep(1000);
s2.on(S2Event.RANGE_FILTERED, (data) => {
dataLength = data.length;
expect(data.length).toStrictEqual(1000);
expect(s2.dataSet.getDisplayDataSet()).toHaveLength(1000);
});

s2.on(S2Event.RANGE_FILTERED, (data) => {
dataLength = data.length;
expect(data.length).toStrictEqual(1000);
expect(s2.dataSet.getDisplayDataSet()).toHaveLength(1000);
});
s2.emit(S2Event.RANGE_FILTER, {
filterKey: 'express_type',
filteredValues: ['消费者'],
});

s2.emit(S2Event.RANGE_FILTER, {
filterKey: 'express_type',
filteredValues: ['消费者'],
});

await sleep(50);
await sleep(200);

expect(dataLength).toStrictEqual(468);
expect(dataLength).toStrictEqual(1000);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ exports[`<StrategySheet/> Tests StrategySheet Export Tests should export correct
指标E 自定义节点D "
`;

exports[`<StrategySheet/> Tests StrategySheet Export Tests should export correct data for custom corner text 1`] = `
" 日期 2022-09 2022-10 2022-11 2021年净增完成度 趋势 2022
指标 数值 环比 同比 数值 环比 数值 环比 同比 净增完成度 趋势 数值 环比
自定义节点A -
自定义节点A 指标A 377 3877 4324 42% - - 377
自定义节点A 指标A 指标B 377 324 377 324 -0.02 - - 377 324
自定义节点A 指标A 自定义节点B
自定义节点A 指标A 指标C 324 377 0 - - 324
自定义节点A 指标A 指标D 377 324 377 324 0.02 - - 377 324
自定义节点A 自定义节点E
指标E 377 324 0.02 - -
指标E 自定义节点C
指标E 自定义节点D "
`;

exports[`<StrategySheet/> Tests StrategySheet Export Tests should export correct data for default corner text 1`] = `
" 日期 2022-09 2022-10 2022-11 2021年净增完成度 趋势 2022
指标 数值 环比 同比 数值 环比 数值 环比 同比 净增完成度 趋势 数值 环比
自定义节点A -
自定义节点A 指标A 377 3877 4324 42% - - 377
自定义节点A 指标A 指标B 377 324 377 324 -0.02 - - 377 324
自定义节点A 指标A 自定义节点B
自定义节点A 指标A 指标C 324 377 0 - - 324
自定义节点A 指标A 指标D 377 324 377 324 0.02 - - 377 324
自定义节点A 自定义节点E
指标E 377 324 0.02 - -
指标E 自定义节点C
指标E 自定义节点D "
`;

exports[`<StrategySheet/> Tests StrategySheet Export Tests should export correct data for empty cell 1`] = `
" 日期 2022-09 2022-10 2022-11 2021年净增完成度 趋势 2022
指标 数值 环比 同比 数值 环比 数值 环比 同比 净增完成度 趋势 数值 环比
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,16 @@ describe('<StrategySheet/> Tests', () => {
});
});

test('should get custom corner extra field text', () => {
test('should get custom corner extra field text', async () => {
const cornerExtraFieldText = '自定义';
const s2DataCfg = {
const s2DataCfg: Partial<S2DataConfig> = {
fields: {
...StrategySheetDataConfig.fields,
valueInCols: false,
},
};
const s2Options = {

const s2Options: SheetComponentOptions = {
cornerExtraFieldText,
};

Expand All @@ -206,16 +207,17 @@ describe('<StrategySheet/> Tests', () => {
...s2DataCfg,
});

const cornerNode = s2.facet
.getCornerNodes()
.find((node) => node.cornerType === CornerNodeType.Row);
await waitFor(() => {
const cornerNode = s2.facet
.getCornerNodes()
.find((node) => node.cornerType === CornerNodeType.Row);

const textList = s2.facet
.getCornerCells()
.map((cell) => cell.getActualText());
const textList = s2.facet.getCornerNodes().map((node) => node.value);
const cornerText = `自定义节点A/指标E/${cornerExtraFieldText}`;

expect(textList).toEqual([cornerExtraFieldText, '日期']);
expect(cornerNode!.value).toEqual(cornerExtraFieldText);
expect(textList).toEqual([cornerText, '日期']);
expect(cornerNode!.value).toEqual(cornerText);
});
});

test('should format corner date field', async () => {
Expand All @@ -240,7 +242,7 @@ describe('<StrategySheet/> Tests', () => {
.getCornerCells()
.map((cell) => cell.getActualText());

expect(cornerTextList).toEqual(['自定义节点A/指标E/指标', '日期']);
expect(cornerTextList).toEqual(['自定义节点A/指标E/数值', '日期']);
});
});

Expand Down Expand Up @@ -316,6 +318,30 @@ describe('<StrategySheet/> Tests', () => {
});
});

test('should export correct data for default corner text', async () => {
await waitFor(() => {
s2.setOptions({
cornerText: undefined,
});

const result = strategyCopy(s2, '\t', true);

expect(result).toMatchSnapshot();
});
});

test('should export correct data for custom corner text', async () => {
await waitFor(() => {
s2.setOptions({
cornerText: '自定义',
});

const result = strategyCopy(s2, '\t', true);

expect(result).toMatchSnapshot();
});
});

test('should export correct data for multi different cycle compare data', async () => {
await waitFor(() => {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports[`Tooltip Common Components Tests render TooltipHead 1`] = `
<div
class="antv-s2-tooltip-head-info-list"
>
一二线城市,有信用卡 / 学生 / 20岁以下
一二线城市,有信用卡/学生/20岁以下
</div>
</DocumentFragment>
`;
Expand All @@ -54,7 +54,7 @@ exports[`Tooltip Common Components Tests render TooltipSummary 1`] = `
<span
class="antv-s2-tooltip-summary-key"
>
A人群 (总和)
A人群(总和
</span>
<span
class="antv-s2-tooltip-summary-val antv-s2-tooltip-bold"
Expand All @@ -68,7 +68,7 @@ exports[`Tooltip Common Components Tests render TooltipSummary 1`] = `
<span
class="antv-s2-tooltip-summary-key"
>
B人群 (总和)
B人群(总和
</span>
<span
class="antv-s2-tooltip-summary-val antv-s2-tooltip-bold"
Expand All @@ -82,7 +82,7 @@ exports[`Tooltip Common Components Tests render TooltipSummary 1`] = `
<span
class="antv-s2-tooltip-summary-key"
>
差值 (总和)
差值(总和
</span>
<span
class="antv-s2-tooltip-summary-val antv-s2-tooltip-bold"
Expand Down
Loading

0 comments on commit c0503df

Please sign in to comment.