Skip to content

Commit

Permalink
fix: unit test problems
Browse files Browse the repository at this point in the history
  • Loading branch information
zamhown committed Oct 24, 2023
1 parent 91fe3c0 commit 14b7bc9
Show file tree
Hide file tree
Showing 20 changed files with 65 additions and 68 deletions.
5 changes: 1 addition & 4 deletions packages/vchart/__tests__/unit/chart/bar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { GlobalScale } from '../../../src/scale/global-scale';
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import type { BarSeries, IChartSpec } from '../../../src';
// eslint-disable-next-line no-duplicate-imports
import { ThemeManager } from '../../../src';
// eslint-disable-next-line no-duplicate-imports
import { BarChart } from '../../../src';
import { DataSet } from '@visactor/vdataset';
import { createCanvas, removeDom } from '../../util/dom';
Expand Down Expand Up @@ -117,8 +115,7 @@ describe('Bar chart test', () => {
}
} as any;
},
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme()
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any)
} as any
);
chart.created();
Expand Down
5 changes: 1 addition & 4 deletions packages/vchart/__tests__/unit/chart/histogram.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import type { BarSeries } from '../../../src';
// eslint-disable-next-line no-duplicate-imports
import { ThemeManager } from '../../../src';
// eslint-disable-next-line no-duplicate-imports
import { HistogramChart } from '../../../src';
import { DataSet, DataView, csvParser, dataViewParser } from '@visactor/vdataset';
import { createCanvas, removeDom } from '../../util/dom';
Expand Down Expand Up @@ -63,8 +61,7 @@ describe('histogram chart test', () => {
container: null,
mode: 'desktop-browser',
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme()
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any)
} as any);
chart.created();
chart.init();
Expand Down
9 changes: 3 additions & 6 deletions packages/vchart/__tests__/unit/chart/line.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { ILineChartSpec } from '../../../src/chart/line/interface';
import { GlobalScale } from '../../../src/scale/global-scale';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import type { LineSeries } from '../../../src/series/line/line';
import { DataSet, DataView, csvParser } from '@visactor/vdataset';
import { LineChart } from '../../../src/chart/line/line';
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import { getTestCompiler } from '../../util/factory/compiler';
import { ThemeManager } from '../../../src';
import { initChartDataSet } from '../../util/context';
import VChart from '../../../src';

const VChartClass = VChart; // 确保引用 vchart 以确保注册所需的图表
const dataSet = new DataSet();
initChartDataSet(dataSet);
dataSet.registerParser('csv', csvParser);
Expand Down Expand Up @@ -48,7 +47,6 @@ describe('line chart test', () => {
mode: 'desktop-browser',
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme(),
onError: () => {}
} as any);
chart.created();
Expand Down Expand Up @@ -120,8 +118,7 @@ describe('line chart test', () => {
container: null,
mode: 'mobile-browser',
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme()
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any)
} as any
);
chart.created();
Expand Down
5 changes: 1 addition & 4 deletions packages/vchart/__tests__/unit/chart/linearProgress.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { DataSet, csvParser, dataViewParser } from '@visactor/vdataset';
import { DataSet, csvParser } from '@visactor/vdataset';
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import type { LinearProgressSeries } from '../../../src';
// eslint-disable-next-line no-duplicate-imports
import { ThemeManager } from '../../../src';
// eslint-disable-next-line no-duplicate-imports
import { LinearProgressChart } from '../../../src';
import { getTestCompiler } from '../../util/factory/compiler';
import { GlobalScale } from '../../../src/scale/global-scale';
Expand Down Expand Up @@ -52,7 +50,6 @@ describe('linearProgress chart test', () => {
mode: 'desktop-browser',
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme(),
animation: false
} as any);
chart.created();
Expand Down
5 changes: 2 additions & 3 deletions packages/vchart/__tests__/unit/chart/rangeColumn.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataSet, DataView, csvParser, dataViewParser } from '@visactor/vdataset';
import { RangeColumnChart, ThemeManager } from '../../../src';
import { DataSet, DataView, csvParser } from '@visactor/vdataset';
import { RangeColumnChart } from '../../../src';
import type { RangeColumnSeries } from '../../../src/series/range-column/range-column';
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import { getTestCompiler } from '../../util/factory/compiler';
Expand Down Expand Up @@ -63,7 +63,6 @@ describe('rangeColumn chart test', () => {
mode: 'desktop-browser',
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme(),
animation: false
} as any);
chart.created();
Expand Down
5 changes: 2 additions & 3 deletions packages/vchart/__tests__/unit/chart/treemap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { GlobalScale } from '../../../src/scale/global-scale';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import { ThemeManager, default as VChart } from '../../../src';
import { DataSet, csvParser, dataViewParser } from '@visactor/vdataset';
import { default as VChart } from '../../../src';
import { DataSet, csvParser } from '@visactor/vdataset';
import { createCanvas, removeDom } from '../../util/dom';
import { getTestCompiler } from '../../util/factory/compiler';
import { disk } from '../../data/disk';
Expand Down Expand Up @@ -66,7 +66,6 @@ describe('treemap chart test', () => {
mode: 'desktop-browser',
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme(),
onError: () => {}
} as any);
chart.created();
Expand Down
7 changes: 3 additions & 4 deletions packages/vchart/__tests__/unit/chart/word-cloud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { GlobalScale } from '../../../src/scale/global-scale';
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import type { IWordCloudChartSpec } from '../../../src';
// eslint-disable-next-line no-duplicate-imports
import { ThemeManager, default as VChart } from '../../../src';
import { DataSet, csvParser, dataViewParser } from '@visactor/vdataset';
import { default as VChart } from '../../../src';
import { DataSet, csvParser } from '@visactor/vdataset';
import { WordCloudChart } from '../../../src/chart/word-cloud/word-cloud';
import type { WordCloudSeries } from '../../../src/series/word-cloud/word-cloud';
import { dataWordCloud } from '../../data/data-wordcloud';
Expand Down Expand Up @@ -73,8 +73,7 @@ describe('wordCloud chart test', () => {
container: null,
mode: 'desktop-browser',
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme()
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any)
} as any);
chart.created();
chart.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { GlobalScale } from '../../../../../src/scale/global-scale';
import type { IEventDispatcher } from '../../../../../src/event/interface';
/* eslint-disable no-lone-blocks */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { DataSet, csvParser, dataViewParser } from '@visactor/vdataset';
import { DataSet, csvParser } from '@visactor/vdataset';
import { dimensionStatistics } from '../../../../../src/data/transforms/dimension-statistics';
import type { CartesianLinearAxis } from '../../../../../src/index';
// eslint-disable-next-line no-duplicate-imports
import { CartesianAxis } from '../../../../../src/index';
import type { IComponent, IComponentOption } from '../../../../../src/component/interface';
import { EventDispatcher } from '../../../../../src/event/event-dispatcher';
import { lightTheme } from '../../../../../src/theme/builtin/light';
import { getTestCompiler } from '../../../../util/factory/compiler';
import { initChartDataSet } from '../../../../util/context';

Expand Down Expand Up @@ -90,7 +89,6 @@ const ctx: IComponentOption = {
getChartViewRect: () => {
return { width: 500, height: 500 } as any;
},
getTheme: () => lightTheme,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getComponentByUserId: function (user_id: string | number): IComponent | undefined {
throw new Error('Function not implemented.');
Expand Down
9 changes: 3 additions & 6 deletions packages/vchart/__tests__/unit/data/fields.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { LineChart } from '../../../src/chart/line/line';
import { DataSet, dataViewParser } from '@visactor/vdataset';
import { DataSet } from '@visactor/vdataset';
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import * as bt from '../../../src/vchart-all';
import { getTestCompiler } from '../../util/factory/compiler';
import { GlobalScale } from '../../../src/scale/global-scale';
import { ThemeManager } from '../../../src';
import { initChartDataSet } from '../../util/context';
bt;

Expand Down Expand Up @@ -68,8 +67,7 @@ describe('data fields test', () => {
container: null,
mode: 'desktop-browser',
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme()
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any)
} as any
);
chart.created();
Expand Down Expand Up @@ -143,8 +141,7 @@ describe('data fields test', () => {
container: null,
mode: 'desktop-browser',
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
getTheme: () => ThemeManager.getCurrentTheme()
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any)
} as any
);
chart.created();
Expand Down
7 changes: 2 additions & 5 deletions packages/vchart/__tests__/unit/morph/morph.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BarChart, CommonChart, ThemeManager } from '../../../src';
import { DataSet, dataViewParser, DataView, csvParser } from '@visactor/vdataset';
import { BarChart, CommonChart } from '../../../src';
import { DataSet, DataView, csvParser } from '@visactor/vdataset';
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import { createCanvas, removeDom } from '../../util/dom';
import { getTestCompiler } from '../../util/factory/compiler';
Expand Down Expand Up @@ -126,7 +126,6 @@ describe('Bar chart test', () => {
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
animation: true,
getTheme: () => ThemeManager.getCurrentTheme(),
onError: () => {}
} as any);
scatterChart.created();
Expand All @@ -147,7 +146,6 @@ describe('Bar chart test', () => {
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
animation: true,
getTheme: () => ThemeManager.getCurrentTheme(),
onError: () => {}
} as any);
barChart.created();
Expand Down Expand Up @@ -184,7 +182,6 @@ describe('Bar chart test', () => {
getCompiler: getTestCompiler,
globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any),
animation: true,
getTheme: () => ThemeManager.getCurrentTheme(),
onError: () => {}
} as any
);
Expand Down
11 changes: 4 additions & 7 deletions packages/vchart/__tests__/unit/scale/global-scale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { CommonChart } from '../../../src/chart/common/common';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { EventDispatcher } from '../../../src/event/event-dispatcher';
import type { IChartSpec, ScatterSeries } from '../../../src';
// eslint-disable-next-line no-duplicate-imports
import { ThemeManager } from '../../../src';
import { VChart, type IChartSpec, type ScatterSeries } from '../../../src';
// eslint-disable-next-line no-duplicate-imports
import { DataSet, dataViewParser, DataView } from '@visactor/vdataset';
import { createCanvas, removeDom } from '../../util/dom';
import type { IAttrs, VisualScaleType } from '../../../src/mark/interface';
import { dimensionStatistics } from '../../../src/data/transforms/dimension-statistics';

const VChartClass = VChart; // 确保引用 vchart 以确保注册所需的图表
// 保证引入执行 Build-in
let dataSet: DataSet;
const data = [
Expand Down Expand Up @@ -315,8 +314,7 @@ describe('global-scale test', () => {
};
}
} as any;
},
getTheme: () => ThemeManager.getCurrentTheme()
}
} as any
);
chart.created();
Expand Down Expand Up @@ -470,8 +468,7 @@ describe('global-scale test', () => {
};
}
} as any;
},
getTheme: () => ThemeManager.getCurrentTheme()
}
} as any
);
chart.created();
Expand Down
6 changes: 2 additions & 4 deletions packages/vchart/__tests__/unit/theme/line.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataSet, DataView, csvParser, dataViewParser } from '@visactor/vdataset';
import { DataSet, DataView, csvParser } from '@visactor/vdataset';
import { VChart } from '../../../src/vchart-all';
import type { ITheme } from '../../../src/theme';
// eslint-disable-next-line no-duplicate-imports
Expand Down Expand Up @@ -137,8 +137,7 @@ describe('theme switch test', () => {
await vchart.renderAsync();
// await vchart.setCurrentTheme('light');
// sepc
expect(vchart.getCurrentTheme().background).toBe('red');
expect(vchart.getCurrentThemeName()).toBe('newTheme');
expect(vchart.getCompiler().getVGrammarView().background()).toBe('red');
});

it('set theme in spec and theme is an object', async () => {
Expand Down Expand Up @@ -179,7 +178,6 @@ describe('theme switch test', () => {

await vchart.renderAsync();
// spec
expect(vchart.getCurrentTheme().background).toBe('red');
expect(vchart.getCompiler().getVGrammarView().background()).toBe('red');
expect(vchart.getCurrentThemeName()).toBe('light');
});
Expand Down
15 changes: 8 additions & 7 deletions packages/vchart/src/chart/base-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import { calculateChartSize, mergeUpdateResult } from './util';
import { isDiscrete } from '@visactor/vscale';
import { updateDataViewInData } from '../data/initialize';
import { getThemeFromOption } from '../theme/util';
import { defaultChartLevelTheme } from '../theme';

export class BaseChart extends CompilableBase implements IChart {
readonly type: string = 'chart';
Expand Down Expand Up @@ -484,13 +485,13 @@ export class BaseChart extends CompilableBase implements IChart {
if (this._spec.zField || (this._spec.series && this._spec.series.some((s: any) => s.zField))) {
use3dLayout = true;
}
const layout = new (Factory.getLayoutInKey(this._spec.layout?.type ?? (use3dLayout ? 'layout3d' : 'base')))(
this._spec.layout,
{
const constructor = Factory.getLayoutInKey(this._spec.layout?.type ?? (use3dLayout ? 'layout3d' : 'base'));
if (constructor) {
const layout = new constructor(this._spec.layout, {
onError: this._option?.onError
}
);
this._layoutFunc = layout.layoutItems.bind(layout);
});
this._layoutFunc = layout.layoutItems.bind(layout);
}
}
}

Expand Down Expand Up @@ -1401,6 +1402,6 @@ export class BaseChart extends CompilableBase implements IChart {
}

getColorScheme() {
return this._option.getThemeConfig?.().chartLevelTheme?.colorScheme;
return (this._option.getThemeConfig?.().chartLevelTheme ?? defaultChartLevelTheme).colorScheme;
}
}
10 changes: 8 additions & 2 deletions packages/vchart/src/component/tooltip/handler/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import type { IContainerSize, TooltipAttributes } from '@visactor/vrender-compon
import { getTooltipAttributes } from './utils/attribute';
import type { DimensionEventParams } from '../../../event/events/dimension/interface';
import type { IChartOption } from '../../../chart/interface';
import type { IChartLevelTheme } from '../../../core/interface';
import { defaultChartLevelTheme } from '../../../theme';

type ChangeTooltipFunc = (
visible: boolean,
Expand Down Expand Up @@ -669,8 +671,12 @@ export abstract class BaseTooltipHandler implements ITooltipHandler {
// 计算 tooltip 内容区域的宽高,并缓存结果
protected _getTooltipBoxSize(actualTooltip: IToolTipActual, changePositionOnly: boolean): IContainerSize | undefined {
if (!changePositionOnly || isNil(this._attributes)) {
const { chartLevelTheme } = this._chartOption.getThemeConfig?.() ?? {};
this._attributes = getTooltipAttributes(actualTooltip, this._component.getSpec(), chartLevelTheme);
const { chartLevelTheme = defaultChartLevelTheme } = this._chartOption.getThemeConfig?.() ?? {};
this._attributes = getTooltipAttributes(
actualTooltip,
this._component.getSpec(),
chartLevelTheme as IChartLevelTheme
);
}
return {
width: this._attributes?.panel?.width,
Expand Down
5 changes: 3 additions & 2 deletions packages/vchart/src/model/base-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Factory } from '../core/factory';
import type { SeriesTypeEnum } from '../series/interface';
import { MarkSet } from '../mark/mark-set';
import { getThemeFromOption } from '../theme/util';
import { defaultChartLevelTheme } from '../theme';

export abstract class BaseModel<T extends IModelSpec> extends LayoutItem<T> implements IModel {
readonly type: string = 'null';
Expand Down Expand Up @@ -430,10 +431,10 @@ export abstract class BaseModel<T extends IModelSpec> extends LayoutItem<T> impl
}

getColorScheme() {
return this._option.getThemeConfig?.().chartLevelTheme?.colorScheme;
return (this._option.getThemeConfig?.().chartLevelTheme ?? defaultChartLevelTheme).colorScheme;
}

protected _getChartLevelTheme() {
return this._option.getThemeConfig?.().chartLevelTheme;
return this._option.getThemeConfig?.().chartLevelTheme ?? defaultChartLevelTheme;
}
}
8 changes: 8 additions & 0 deletions packages/vchart/src/theme/builtin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ export * from './light';
/** 历史弃用主题 */
export * from './common/legacy';

import type { IChartLevelTheme } from '../../core/interface';
import { mergeTheme } from '../../util/spec/merge-theme';
import { getActualColor } from '../color-scheme/util';
import type { ITheme } from '../interface';
import { darkTheme } from './dark';
import { lightTheme } from './light';
Expand All @@ -24,3 +26,9 @@ export const hasThemeMerged: Map<string, boolean> = new Map(

/** 使新主题基于默认主题扩展,保证基础值 */
export const getMergedTheme = (theme: Partial<ITheme>): ITheme => mergeTheme({}, themes.get(defaultThemeName), theme);

export const defaultChartLevelTheme: IChartLevelTheme = {
background: getActualColor(builtinThemes[defaultThemeName].background, builtinThemes[defaultThemeName].colorScheme),
fontFamily: builtinThemes[defaultThemeName].fontFamily,
colorScheme: builtinThemes[defaultThemeName].colorScheme
};
Loading

0 comments on commit 14b7bc9

Please sign in to comment.