Skip to content

Commit

Permalink
fix: 修复文本行数不一致时自动换行高度自适应失效 close #2594 (#2598)
Browse files Browse the repository at this point in the history
* fix: 修复文本行数不一致时自动换行高度自适应失效 close #2594

* test: 增加测试

* test: 修复测试

* test: 修复测试

* fix: 移除注释

* test: 修复测试
  • Loading branch information
lijinke666 authored Mar 15, 2024
1 parent 086c9e3 commit fae5496
Show file tree
Hide file tree
Showing 28 changed files with 8,461 additions and 2,840 deletions.
10,471 changes: 7,800 additions & 2,671 deletions packages/s2-core/__tests__/spreadsheet/__snapshots__/multi-line-text-spec.ts.snap

Large diffs are not rendered by default.

226 changes: 202 additions & 24 deletions packages/s2-core/__tests__/spreadsheet/multi-line-text-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable jest/expect-expect */
import { getContainer } from 'tests/util/helpers';
import { range } from 'lodash';
import { PivotSheet, TableSheet, type SpreadSheet } from '../../src';
import type {
CellTextWordWrapStyle,
Expand Down Expand Up @@ -47,7 +48,25 @@ describe('SpreadSheet Multi Line Text Tests', () => {
});
};

const expectHierarchyHeight = (
const expectRowHierarchyHeight = (
height: number,
lastLevelY: number = 0,
lastLevelHeight: number = 30,
sampleNodesForAllLevelsTotals = 2,
) => {
const { rowsHierarchy } = s2.facet.getLayoutResult();

expect(rowsHierarchy.height).toEqual(height);
expect(rowsHierarchy.sampleNodesForAllLevels).toHaveLength(
sampleNodesForAllLevelsTotals,
);
expect(rowsHierarchy.sampleNodeForLastLevel?.y).toEqual(lastLevelY);
expect(rowsHierarchy.sampleNodeForLastLevel?.height).toEqual(
lastLevelHeight,
);
};

const expectColHierarchyHeight = (
height: number,
lastLevelY: number = 60,
lastLevelHeight: number = 30,
Expand Down Expand Up @@ -122,28 +141,28 @@ describe('SpreadSheet Multi Line Text Tests', () => {
getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});
expectHierarchyHeight(90);
expectColHierarchyHeight(90);
});

test('should custom two max text lines', async () => {
test('should render two max text lines', async () => {
updateStyle(2);
await s2.render(false);

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});
expectHierarchyHeight(118, 80, 38);
expectColHierarchyHeight(118, 80, 38);
});

test('should custom three max text lines', async () => {
test('should render three max text lines', async () => {
updateStyle(3);
await s2.render(false);

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});

expectHierarchyHeight(165, 112, 53);
expectColHierarchyHeight(165, 112, 53);
});

test('should render custom text overflow text', async () => {
Expand All @@ -166,7 +185,7 @@ describe('SpreadSheet Multi Line Text Tests', () => {
getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});
expectHierarchyHeight(90);
expectColHierarchyHeight(90);
});

test('should not render word wrap text', async () => {
Expand All @@ -193,7 +212,7 @@ describe('SpreadSheet Multi Line Text Tests', () => {
expect(cell.getActualText()).not.toContain('...');
});
});
expectHierarchyHeight(90);
expectColHierarchyHeight(90);
});

test('should force adaptive adjust cell height if custom cell style less than actual text height', async () => {
Expand All @@ -220,7 +239,7 @@ describe('SpreadSheet Multi Line Text Tests', () => {
getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});
expectHierarchyHeight(118, 80, 38);
expectColHierarchyHeight(118, 80, 38);
});

test('should not adaptive adjust cell height if custom cell style more than actual text height', async () => {
Expand Down Expand Up @@ -256,36 +275,36 @@ describe('SpreadSheet Multi Line Text Tests', () => {
});
});

expectHierarchyHeight(210, 140, 70);
expectColHierarchyHeight(210, 140, 70);
});

test('should render correctly layout if enable totals', async () => {
setupTotalsOptions();
await s2.render(false);

expectHierarchyHeight(90, 60, 30);
expectColHierarchyHeight(90, 60, 30);
});

test('should render correctly layout if enable totals for multiple text lines', async () => {
setupTotalsOptions();
updateStyle(3);
await s2.render(false);

expectHierarchyHeight(165, 112, 53);
expectColHierarchyHeight(165, 112, 53);
});

test('should render correctly layout if only enable grand totals', async () => {
setupTotalsOptions(true, false);
await s2.render(false);

expectHierarchyHeight(90, 60, 30);
expectColHierarchyHeight(90, 60, 30);
});

test('should render correctly layout if only enable sub totals', async () => {
setupTotalsOptions(false, true);
await s2.render(false);

expectHierarchyHeight(90, 60, 30);
expectColHierarchyHeight(90, 60, 30);
});

test('should not adaptive adjust cell height if hidden col cell', async () => {
Expand All @@ -303,7 +322,69 @@ describe('SpreadSheet Multi Line Text Tests', () => {
getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});
expectHierarchyHeight(0, 0, 0);
expectColHierarchyHeight(0, 0, 0);
});

// https://github.com/antvis/S2/issues/2594
test('should calc correctly row cell height if actual text lines is difference', async () => {
updateStyle(4);
s2.changeSheetSize(800, 600);
s2.setDataCfg({
data: [
{
province:
'浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江',
city: '杭州杭州杭州杭州',
type: '纸张纸张纸张纸张纸张',
price: 2,
cost: 20,
},
...s2.dataCfg.data,
],
});

await s2.render();

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});

// 省份 4行文本, 叶子节点 (城市) 3行文本, 省份应该和城市高度一致, 才能展示所有文本 (maxLines: 4)
expectRowHierarchyHeight(568, 0, 72);
expectColHierarchyHeight(212, 144, 68);
});

test('should render three max text lines for tree mode', async () => {
updateStyle(3);
s2.setOptions({
hierarchyType: 'tree',
style: {
rowCell: {
width: 100,
},
},
});
s2.changeSheetSize(800, 600);
s2.setDataCfg({
data: [
{
province:
'浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江',
city: '杭州杭州杭州杭州',
type: '纸张纸张纸张纸张纸张',
price: 2,
cost: 20,
},
...s2.dataCfg.data,
],
});
await s2.render();

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});

expect(s2.facet.getLayoutResult().rowsHierarchy.height).toEqual(760);
});
});

Expand Down Expand Up @@ -333,28 +414,28 @@ describe('SpreadSheet Multi Line Text Tests', () => {
getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});
expectHierarchyHeight(30, 0, 30, 1);
expectColHierarchyHeight(30, 0, 30, 1);
});

test('should custom two max text lines', async () => {
test('should render two max text lines', async () => {
updateStyle(2);
await s2.render(false);

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});
expectHierarchyHeight(40, 0, 40, 1);
expectColHierarchyHeight(40, 0, 40, 1);
});

test('should custom three max text lines', async () => {
test('should render three max text lines', async () => {
updateStyle(3);
await s2.render(false);

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});

expectHierarchyHeight(56, 0, 56, 1);
expectColHierarchyHeight(56, 0, 56, 1);
});

test('should force adaptive adjust cell height if custom cell style less than actual text height', async () => {
Expand All @@ -375,7 +456,7 @@ describe('SpreadSheet Multi Line Text Tests', () => {
getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});
expectHierarchyHeight(40, 0, 40, 1);
expectColHierarchyHeight(40, 0, 40, 1);
});

test('should not adaptive adjust cell height if custom cell style more than actual text height', async () => {
Expand All @@ -400,18 +481,115 @@ describe('SpreadSheet Multi Line Text Tests', () => {
});
});

expectHierarchyHeight(70, 0, 70, 1);
expectColHierarchyHeight(70, 0, 70, 1);
});

test('should calc correctly col cell height if actual text lines is difference', async () => {
updateStyle(4);
s2.setDataCfg({
meta: [
{
field: 'province',
name: '省份'.repeat(20),
},
],
});

await s2.render();

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});

expectColHierarchyHeight(72, 0, 72, 1);
});

// https://github.com/antvis/S2/issues/2594
test('should calc correctly data cell height if actual text lines is difference', async () => {
updateStyle(4);

await s2.render();

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});

const rowDataCells = s2.facet
.getDataCells()
.filter((cell) => cell.getMeta().rowIndex === 1);

expect(
rowDataCells.every((cell) => cell.getMeta().height === 76),
).toBeTruthy();
});

test('should force adaptive adjust row height if custom cell style less than actual text height', async () => {
updateStyle(3);

s2.setOptions({
style: {
rowCell: {
heightByField: {
1: 20,
},
},
},
});
await s2.render();

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});

const rowDataCells = s2.facet
.getDataCells()
.filter((cell) => cell.getMeta().rowIndex === 1);

expect(
rowDataCells.every((cell) => cell.getMeta().height === 61),
).toBeTruthy();
});

test('should calc correctly row cell height if actual text lines is difference', async () => {
updateStyle(4);
s2.changeSheetSize(800, 600);
s2.setDataCfg({
data: [
{
province: '浙江浙江浙江浙江浙江浙江浙江浙江浙江浙江',
city: '杭州杭州杭州杭州',
type: '纸张纸张纸张纸张纸张',
price: 2,
cost: 20,
},
...s2.dataCfg.data,
],
});

await s2.render();

getCells().forEach((cells) => {
expect(mapCells(cells)).toMatchSnapshot();
});

const rowDataCells = s2.facet
.getDataCells()
.filter((cell) => cell.getMeta().rowIndex === 0);

expect(
rowDataCells.every((cell) => cell.getMeta().height === 46),
).toBeTruthy();
});

test.skip.each([1, 2, 3, 4, 5])(
test.skip.each(range(1, 6))(
'should always render default cell height when set %s line, but actual text not wrap',
async (maxLines) => {
updateStyle(maxLines);

s2.setDataCfg(SimpleDataCfg);
await s2.render();

expectHierarchyHeight(30, 0, 30, 1);
expectColHierarchyHeight(30, 0, 30, 1);
},
);
});
Expand Down
Loading

0 comments on commit fae5496

Please sign in to comment.