This is an automated email from the ASF dual-hosted git repository. shenyi pushed a commit to branch enhance-missing-components-log in repository https://gitbox.apache.org/repos/asf/echarts.git
commit b8c6add10f3ceefe08c4ae6e3cfaa830372106e9 Author: pissang <[email protected]> AuthorDate: Wed Mar 31 15:41:52 2021 +0800 test: add missing component ut. fix some warnings --- test/ut/core/extendExpect.ts | 4 +- test/ut/core/utHelper.ts | 14 +++- test/ut/index.d.ts | 13 +++ test/ut/spec/model/componentMissing.test.ts | 118 ++++++++++++++++++++++++++++ test/ut/spec/scale/interval.test.ts | 10 +-- 5 files changed, 149 insertions(+), 10 deletions(-) diff --git a/test/ut/core/extendExpect.ts b/test/ut/core/extendExpect.ts index c71b53e..5f89c18 100644 --- a/test/ut/core/extendExpect.ts +++ b/test/ut/core/extendExpect.ts @@ -18,7 +18,9 @@ * under the License. */ -import { isValueFinite } from './utHelper'; +function isValueFinite(val: unknown): boolean { + return val != null && val !== '' && isFinite(val as number); +} // Setup expectes diff --git a/test/ut/core/utHelper.ts b/test/ut/core/utHelper.ts index ae23689..d933feb 100755 --- a/test/ut/core/utHelper.ts +++ b/test/ut/core/utHelper.ts @@ -46,6 +46,16 @@ export function createChart(params?: { 'bottom:0', 'right:0' ].join(';'); + Object.defineProperty(el, 'clientWidth', { + get() { + return params.width || 500; + } + }); + Object.defineProperty(el, 'clientHeight', { + get() { + return params.height || 400; + } + }); const chart = init(el, params.theme, params.opts); return chart; }; @@ -80,10 +90,6 @@ export const curry = zrUtilCurry; export const bind = zrUtilBind; -export function isValueFinite(val: unknown): boolean { - return val != null && val !== '' && isFinite(val as number); -} - // /** // * @public // * @param {Array.<string>} deps diff --git a/test/ut/index.d.ts b/test/ut/index.d.ts new file mode 100644 index 0000000..0d3604a --- /dev/null +++ b/test/ut/index.d.ts @@ -0,0 +1,13 @@ +export {}; +declare global { + namespace jest { + interface Matchers<R> { + toBeFinite(): R + // Greater than or equal + toBeGeaterThanOrEqualTo(bound: number): R + // Greater than + toBeGreaterThan(bound: number): R + toBeEmptyArray(): R + } + } +} \ No newline at end of file diff --git a/test/ut/spec/model/componentMissing.test.ts b/test/ut/spec/model/componentMissing.test.ts new file mode 100644 index 0000000..70a6aa4 --- /dev/null +++ b/test/ut/spec/model/componentMissing.test.ts @@ -0,0 +1,118 @@ +import { init, use, EChartsType } from '../../../../src/export/core'; +import { + PieChart +} from '../../../../src/export/charts'; +import { + TitleComponent +} from '../../../../src/export/components'; +import { + CanvasRenderer +} from '../../../../src/export/renderers'; +use([PieChart, TitleComponent, CanvasRenderer]); +import { EChartsOption } from '../../../../src/export/option'; + + +function createChart(): EChartsType { + const el = document.createElement('div'); + Object.defineProperty(el, 'clientWidth', { + get() { + return 500; + } + }); + Object.defineProperty(el, 'clientHeight', { + get() { + return 400; + } + }); + const chart = init(el); + return chart; +}; + +function makeComponentError(componentName: string, componentImportName: string) { + return `[ECharts] Component ${componentName} is used but not imported. +import { ${componentImportName} } from 'echarts/components'; +echarts.use([${componentImportName}]);`; +} + +function makeSerieError(seriesName: string, seriesImportName: string) { + return `[ECharts] Series ${seriesName} is used but not imported. +import { ${seriesImportName} } from 'echarts/charts'; +echarts.use([${seriesImportName}]);`; +} + + + +describe('model_componentMissing', function () { + it('Should report grid component missing error', function () { + const chart = createChart(); + const oldConsoleErr = console.error; + console.error = jest.fn(); + chart.setOption<EChartsOption>({ + xAxis: {}, + yAxis: {}, + series: [] + }); + expect(console.error).toHaveBeenCalledWith( + makeComponentError('xAxis', 'GridComponent') + ); + + console.error = oldConsoleErr; + }); + + it('Should report dataZoom component missing error', function () { + const chart = createChart(); + const oldConsoleErr = console.error; + console.error = jest.fn(); + chart.setOption<EChartsOption>({ + dataZoom: {} + }); + expect(console.error).toHaveBeenCalledWith( + makeComponentError('dataZoom', 'DataZoomComponent') + ); + + console.error = oldConsoleErr; + }); + + it('Should not report title component missing error', function () { + const chart = createChart(); + const oldConsoleErr = console.error; + console.error = jest.fn(); + chart.setOption<EChartsOption>({ + title: {}, + series: [] + }); + expect(console.error).not.toBeCalled(); + + console.error = oldConsoleErr; + }); + + it('Should report funnel series missing error', function () { + const chart = createChart(); + const oldConsoleErr = console.error; + console.error = jest.fn(); + chart.setOption<EChartsOption>({ + series: [{ + type: 'funnel' + }] + }); + expect(console.error).toHaveBeenCalledWith( + makeSerieError('funnel', 'FunnelChart') + ); + + console.error = oldConsoleErr; + }); + + it('Should not report pie series missing error', function () { + const chart = createChart(); + const oldConsoleErr = console.error; + console.error = jest.fn(); + chart.setOption<EChartsOption>({ + series: [{ + type: 'pie' + }] + }); + expect(console.error).not.toBeCalled(); + + console.error = oldConsoleErr; + }); +}); \ No newline at end of file diff --git a/test/ut/spec/scale/interval.test.ts b/test/ut/spec/scale/interval.test.ts index 81aed8f..aec5999 100755 --- a/test/ut/spec/scale/interval.test.ts +++ b/test/ut/spec/scale/interval.test.ts @@ -18,7 +18,7 @@ * under the License. */ -import { createChart, isValueFinite, getECModel } from '../../core/utHelper'; +import { createChart, getECModel } from '../../core/utHelper'; import { EChartsType } from '../../../../src/echarts'; import CartesianAxisModel from '../../../../src/coord/cartesian/AxisModel'; import IntervalScale from '../../../../src/scale/Interval'; @@ -124,10 +124,10 @@ describe('scale_interval', function () { const resultInterval = result.interval; const niceTickExtent = result.niceTickExtent; - expect(isValueFinite(resultInterval)).toEqual(true); - expect(isValueFinite(intervalPrecision)).toEqual(true); - expect(isValueFinite(niceTickExtent[0])).toEqual(true); - expect(isValueFinite(niceTickExtent[1])).toEqual(true); + expect(resultInterval).toBeFinite(); + expect(intervalPrecision).toBeFinite(); + expect(niceTickExtent[0]).toBeFinite(); + expect(niceTickExtent[1]).toBeFinite(); expect(niceTickExtent[0]).toBeGreaterThanOrEqual(extent[0]); expect(niceTickExtent[1]).not.toBeGreaterThan(extent[1]); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
