Skip to content

Commit

Permalink
fix: only omit undefined row cell (#861)
Browse files Browse the repository at this point in the history
* fix: only omit undefined values

* test: add unit test for empty string field

* test: add null and undefined test data
  • Loading branch information
wjgogogo authored Dec 3, 2021
1 parent 2f6d71e commit 55cea0b
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 3 deletions.
30 changes: 30 additions & 0 deletions packages/s2-core/__tests__/bugs/issue-860-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @description spec for issue #860
* https://github.com/antvis/S2/issues/860
* Column should not be formatted
*
*/
import { getContainer } from 'tests/util/helpers';
import dataCfg from '../data/data-issue-860.json';
import { PivotSheet, SpreadSheet } from '@/sheet-type';
import { S2Options } from '@/common/interface';

const s2options: S2Options = {
width: 600,
height: 400,
frozenRowHeader: false,
};

describe('Column Formatter Tests', () => {
let s2: SpreadSheet;

beforeEach(() => {
s2 = new PivotSheet(getContainer(), dataCfg, s2options);
s2.render();
});
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);
});
});
74 changes: 74 additions & 0 deletions packages/s2-core/__tests__/data/data-issue-860.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"fields": {
"rows": ["date", "city", "type"],
"columns": [],
"values": ["a_sum", "b_sum"],
"valueInCols": true
},
"meta": [
{
"field": "date",
"name": "日期"
},
{
"field": "city",
"name": "城市"
},
{
"field": "type",
"name": "类型"
},
{
"field": "a_sum",
"name": "A类求和"
},
{
"field": "b_sum",
"name": "B类求和"
}
],
"data": [
{
"date": "2021-12-01",
"city": "A市",
"type": "电子",
"a_sum": 6
},
{
"date": "2021-12-01",
"city": "A市",
"type": "电子",
"b_sum": 6
},
{
"date": "2021-12-01",
"city": "A市",
"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市",
"type": "日用",
"a_sum": 1
},
{
"date": "2021-12-01",
"city": "B市",
"type": "",
"b_sum": 1
}
]
}
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 @@ -706,6 +706,27 @@ describe('PivotSheet Tests', () => {
expect(sheet.dataCfg.fields.valueInCols).toBeTruthy();
});

it('should render row nodes if rows fields contain empty string value', () => {
const layoutDataCfg: S2DataConfig = {
fields: {
rows: ['row'],
},
data: [
{
row: 'a',
},
{
row: '',
},
],
} as S2DataConfig;
const sheet = new PivotSheet(getContainer(), layoutDataCfg, s2Options);
sheet.render();

const { layoutResult } = sheet.facet;
expect(layoutResult.rowNodes).toHaveLength(2);
});

it('should only render value nodes in column if rows & columns fields is empty', () => {
const layoutDataCfg: S2DataConfig = customMerge(dataCfg, {
fields: {
Expand Down
13 changes: 10 additions & 3 deletions packages/s2-core/src/facet/layout/build-gird-hierarchy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { compact, isEmpty } from 'lodash';
import { filter, isEmpty, isUndefined } from 'lodash';
import { FieldValue, GridHeaderParams } from '@/facet/layout/interface';
import { TotalMeasure } from '@/facet/layout/total-measure';
import { layoutArrange } from '@/facet/layout/layout-hooks';
Expand Down Expand Up @@ -39,6 +39,7 @@ export const buildGridHierarchy = (params: GridHeaderParams) => {
} = params;

const index = fields.indexOf(currentField);

const { dataSet, values, spreadsheet } = facetCfg;
const fieldValues: FieldValue[] = [];

Expand Down Expand Up @@ -72,7 +73,7 @@ export const buildGridHierarchy = (params: GridHeaderParams) => {
if (currentField === EXTRA_FIELD) {
fieldValues.push(...dataSet.fields?.values);
} else {
fieldValues.push(fieldName || currentField);
fieldValues.push(fieldName);
}
}
// hide measure in columns
Expand All @@ -86,10 +87,16 @@ export const buildGridHierarchy = (params: GridHeaderParams) => {
spreadsheet,
});
}

const omitUndefinedFieldValues = filter(
fieldValues,
(value) => !isUndefined(value),
);

generateHeaderNodes({
currentField,
fields,
fieldValues: compact(fieldValues),
fieldValues: omitUndefinedFieldValues,
facetCfg,
hierarchy,
parentNode,
Expand Down

0 comments on commit 55cea0b

Please sign in to comment.