Skip to content

Commit

Permalink
fix: 修复 lint 和 test
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Oct 25, 2023
1 parent 89fb2bf commit dcf81e6
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 139 deletions.
13 changes: 6 additions & 7 deletions packages/s2-core/__tests__/unit/cell/data-cell-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { createPivotSheet, createTableSheet } from 'tests/util/helpers';
import { DataCell } from '@/cell';
import type { TextAlign } from '@/common';
import {
CellType,
GuiIcon,
type Formatter,
type ViewMeta,
S2Event,
type Formatter,
type OriginalEvent,
type S2CellType,
CellType,
type ViewMeta,
} from '@/common';
import { EXTRA_FIELD, VALUE_FIELD } from '@/common/constant/basic';
import {
Expand All @@ -21,7 +21,6 @@ import {
import { PivotDataSet } from '@/data-set';
import type { PivotFacet } from '@/facet';
import { PivotSheet, SpreadSheet } from '@/sheet-type';
import { renderText } from '@/utils/g-renders';

const MockPivotSheet = PivotSheet as unknown as jest.Mock<PivotSheet>;
const MockPivotDataSet = PivotDataSet as unknown as jest.Mock<PivotDataSet>;
Expand Down Expand Up @@ -89,7 +88,7 @@ describe('Data Cell Tests', () => {
const linkLength = maxX - minX;

expect(
Math.abs(linkLength - get(dataCell, 'actualTextWidth')),
Math.abs(linkLength - dataCell.getActualTextWidth()),
).toBeLessThanOrEqual(2);

// link shape 的中点坐标与 text 中点对齐
Expand Down Expand Up @@ -189,9 +188,8 @@ describe('Data Cell Tests', () => {

test('should add text shape', () => {
const dataCell = new DataCell(meta, s2);
const textShape = renderText(dataCell, [], { x: 0, y: 0, text: 'test' });

dataCell.addTextShape(textShape);
dataCell.renderTextShape({ x: 0, y: 0, text: 'test' });

expect(dataCell.getTextShapes()).toHaveLength(2);
});
Expand All @@ -209,6 +207,7 @@ describe('Data Cell Tests', () => {
expect(dataCell.getConditionIconShapes()).toBeEmpty();
});
});

describe('Condition by formattedValue Tests', () => {
const s2 = createPivotSheet(
{
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/__tests__/unit/cell/row-cell-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Row Cell Tests', () => {
const linkLength = maxX - minX;

expect(
Math.abs(linkLength - get(provinceCell, 'actualTextWidth')),
Math.abs(linkLength - provinceCell.getActualTextWidth()),
).toBeLessThanOrEqual(2);

// link shape 的中点坐标与 text 中点对齐
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('Interaction Hover Tests', () => {

return mockCell;
},
isTextOverflowing: jest.fn(),
getActualText: () => ELLIPSIS_SYMBOL,
getFieldValue: () => '',
cellType: 'dataCell',
Expand Down Expand Up @@ -306,6 +307,7 @@ describe('Interaction Hover Tests', () => {
},
getActualText: () => 'test',
getFieldValue: () => 'test',
isTextOverflowing: jest.fn(),
cellType: 'dataCell',
} as any);

Expand Down
25 changes: 1 addition & 24 deletions packages/s2-core/__tests__/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,30 +154,6 @@ export const createFakeSpreadSheet = () => {
return s2;
};

// 可借助 tiny gradient 完成功能更全面的颜色过渡
export function getGradient(
rate: number,
startColor: string,
endColor: string,
) {
function toGgb(color: string) {
color = color.slice(1);
const r = parseInt(color.substring(0, 2), 16);
const g = parseInt(color.substring(2, 4), 16);
const b = parseInt(color.substring(4, 6), 16);

return [r, g, b];
}

const start = toGgb(startColor);
const end = toGgb(endColor);
const r = start[0] + (end[0] - start[0]) * rate;
const g = start[1] + (end[1] - start[1]) * rate;
const b = start[2] + (end[2] - start[2]) * rate;

return `rgb(${r},${g},${b})`;
}

export const createMockCellInfo = (
cellId: string,
{ colIndex = 0, rowIndex = 0, colId = '0', level = 0 } = {},
Expand Down Expand Up @@ -227,6 +203,7 @@ export const createMockCellInfo = (
getFieldValue: jest.fn(),
hideInteractionShape: jest.fn(),
updateByState: jest.fn(),
isTextOverflowing: jest.fn(),
} as unknown as S2CellType;

const getNode = () => mockCellViewMeta as unknown as Node;
Expand Down
4 changes: 1 addition & 3 deletions packages/s2-core/src/common/constant/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ export const DEFAULT_TREE_ROW_CELL_WIDTH = 120;
export const DEFAULT_STYLE: S2Style = {
layoutWidthType: LayoutWidthTypes.Adaptive,
rowCell: {
width: 120,
// width: 120,
showTreeLeafNodeAlignDot: false,
widthByField: null,
heightByField: null,
},
colCell: {
// height: 30,
height: 40,
widthByField: null,
heightByField: null,
},
dataCell: {
width: 96,
// height: 30,
height: 40,
},
} as const;
Expand Down
4 changes: 2 additions & 2 deletions packages/s2-core/src/facet/header/col.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class ColHeader extends BaseHeader<ColHeaderConfig> {
}

protected getCellInstance(node: Node): S2CellType {
const { spreadsheet } = node;
const { colCell } = node.spreadsheet.options;
const { spreadsheet } = this.headerConfig;
const { colCell } = spreadsheet.options;

return (
colCell?.(node, spreadsheet, this.headerConfig) ||
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/header/corner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class CornerHeader extends BaseHeader<CornerHeaderConfig> {
}

protected getCellInstance(node: Node): S2CellType {
const { spreadsheet } = node;
const { spreadsheet } = this.headerConfig;
const { cornerCell } = spreadsheet.options;

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/header/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class RowHeader extends BaseHeader<RowHeaderConfig> {
}

protected getCellInstance(node: Node): S2CellType {
const { spreadsheet } = node;
const { spreadsheet } = this.headerConfig;
const { rowCell } = spreadsheet.options;

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/header/series-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class SeriesNumberHeader extends BaseHeader<BaseHeaderConfig> {
}

protected getCellInstance(node: Node): S2CellType {
const { spreadsheet } = node;
const { spreadsheet } = this.headerConfig;
const { seriesNumberCell } = spreadsheet.options;

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/header/table-col.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class TableColHeader extends ColHeader {
}

protected getCellInstance(node: Node) {
const { spreadsheet } = node;
const { spreadsheet } = this.headerConfig;
const { seriesNumberCell, colCell } = spreadsheet.options;

const args: [Node, SpreadSheet, ColHeaderConfig] = [
Expand Down
3 changes: 3 additions & 0 deletions packages/s2-core/src/utils/g-renders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export function renderCircle(group: Group, style: CircleStyleProps): Circle {
);
}

/**
* @description 如果在单元格内绘制, 是使用 cell.renderTextShape(options)
*/
export function renderText<T>(options: {
group: Group;
textShape?: DisplayObject;
Expand Down
1 change: 1 addition & 0 deletions packages/s2-core/src/utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ export const drawObjectText = (
contentBoxes[i][j],
curStyle!.textBaseline!,
);

const iconY = getVerticalIconPosition(
iconStyle.size!,
textY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ describe('<StrategySheet/> Tests', () => {
const fn = jest.fn();

class CustomRowCell extends RowCell {
protected drawTextShape() {
public drawTextShape() {
fn();

return super.drawTextShape();
Expand All @@ -405,7 +405,7 @@ describe('<StrategySheet/> Tests', () => {
const fn = jest.fn();

class CustomColCell extends StrategySheetColCell {
protected drawTextShape() {
public drawTextShape() {
fn();

return super.drawTextShape();
Expand All @@ -427,7 +427,7 @@ describe('<StrategySheet/> Tests', () => {
const fn = jest.fn();

class CustomDataCell extends StrategySheetDataCell {
protected drawTextShape() {
public drawTextShape() {
fn();

return super.drawTextShape();
Expand Down
67 changes: 1 addition & 66 deletions packages/s2-react/__tests__/util/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
/* eslint-disable import/no-extraneous-dependencies */
import fs from 'fs';
import path from 'path';
import { PivotSheet, SpreadSheet, type ViewMeta } from '@antv/s2';
import { dsvFormat } from 'd3-dsv';
import EE from '@antv/event-emitter';
import type { Canvas } from '@antv/g';
import {
Store,
type S2Options,
SpreadSheet,
PivotSheet,
BaseTooltip,
customMerge,
DEFAULT_OPTIONS,
RootInteraction,
type ViewMeta,
} from '@antv/s2';
import { omit } from 'lodash';

export const parseCSV = (csv: string, header?: string[]) => {
Expand Down Expand Up @@ -47,59 +35,6 @@ export const sleep = async (timeout = 0) => {
});
};

export const createFakeSpreadSheet = () => {
class FakeSpreadSheet extends EE {
public options: S2Options;

public setOptions(options: S2Options) {
this.options = customMerge(this.options, options);
}
}

const s2 = new FakeSpreadSheet() as unknown as SpreadSheet;

s2.options = DEFAULT_OPTIONS;
const interaction = new RootInteraction(s2 as unknown as SpreadSheet);

s2.store = new Store();
s2.interaction = interaction;
s2.tooltip = {
container: {} as HTMLElement,
} as BaseTooltip;
s2.container = {
draw: jest.fn(),
} as unknown as Canvas;
s2.getCellType = jest.fn();
s2.render = jest.fn();
s2.hideTooltip = jest.fn();
s2.showTooltipWithInfo = jest.fn();
s2.isTableMode = jest.fn();

return s2;
};
// 可借助 tinygradient 完成功能更全面的颜色过渡
export function getGradient(
rate: number,
startColor: string,
endColor: string,
) {
function toGgb(color: string) {
color = color.slice(1);
const r = parseInt(color.substring(0, 2), 16);
const g = parseInt(color.substring(2, 4), 16);
const b = parseInt(color.substring(4, 6), 16);

return [r, g, b];
}
const start = toGgb(startColor);
const end = toGgb(endColor);
const r = start[0] + (end[0] - start[0]) * rate;
const g = start[1] + (end[1] - start[1]) * rate;
const b = start[2] + (end[2] - start[2]) * rate;

return `rgb(${r},${g},${b})`;
}

export function getMockSheetInstance(Sheet: typeof SpreadSheet = PivotSheet) {
const instance = Object.create(Sheet.prototype);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataCell } from '@antv/s2';

export class CustomCell extends DataCell {
protected drawTextShape() {
public drawTextShape() {
// TODO 暂时留下个扩展位,不知道后面会有什么需求
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class StrategySheetColCell extends ColCell {
super(meta, spreadsheet, headerConfig);
}

protected drawTextShape() {
public drawTextShape() {
const meta = this.getMeta();
const value = safeJsonParse(meta?.value) as MultiData;

Expand Down
47 changes: 20 additions & 27 deletions s2-site/examples/case/comparison/demo/measure-comparison.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,34 +203,27 @@ class CustomDataCell extends DataCell {
this.meta.colId?.includes(`root${ID_SEPARATOR}${item}${ID_SEPARATOR}`),
);

if (tagName) {
const { getCustomFormattedValue, getCustomTextStyle } =
this.textConfig[tagName] || {};
const { formattedValue: defaultFormattedValue } =
this.getFormattedFieldValue();
let formattedValue = defaultFormattedValue;

if (getCustomFormattedValue) {
formattedValue = getCustomFormattedValue(fieldValue);
}

const textStyle = this.getTextStyle();

this.actualTextWidth = measureTextWidth(formattedValue, textStyle);
const position = this.getTextPosition();

this.textShape = this.addShape('text', {
attrs: {
x: position.x,
y: position.y,
text: formattedValue,
...textStyle,
...(getCustomTextStyle(fieldValue, textStyle) || {}),
},
});
} else {
super.drawTextShape();
if (!tagName) {
return super.drawTextShape();
}

const { getCustomFormattedValue, getCustomTextStyle } =
this.textConfig[tagName] || {};
const { formattedValue: defaultFormattedValue } =
this.getFormattedFieldValue();

const formattedValue =
getCustomFormattedValue?.(fieldValue) ?? defaultFormattedValue;
const textStyle = this.getTextStyle();
const position = this.getTextPosition();

this.renderTextShape({
x: position.x,
y: position.y,
text: formattedValue,
...textStyle,
...(getCustomTextStyle(fieldValue, textStyle) || {}),
});
}

drawLeftBorder() {
Expand Down

0 comments on commit dcf81e6

Please sign in to comment.