From 03b030418682322d45d787480f389acb96086647 Mon Sep 17 00:00:00 2001 From: asimonok Date: Thu, 1 Feb 2024 17:19:45 +0300 Subject: [PATCH] Fix any warnings --- .eslintrc | 2 +- src/@types/global.d.ts | 16 ++++++++++ .../EchartsPanel/EchartsPanel.test.tsx | 32 ++++++++++++++++--- src/components/EchartsPanel/EchartsPanel.tsx | 10 +++--- src/components/SeriesEditor/SeriesEditor.tsx | 2 +- src/maps.ts | 4 +-- src/utils/visual-editor.ts | 6 ++-- 7 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 src/@types/global.d.ts diff --git a/.eslintrc b/.eslintrc index bd53992..99c3ab5 100644 --- a/.eslintrc +++ b/.eslintrc @@ -8,7 +8,7 @@ }, "overrides": [ { - "files": ["**/*.test.tsx", "**/*.test.ts"], + "files": ["**/*.test.tsx", "**/*.test.ts", "src/__testUtils__/**/*", "**/__mocks__/**/*"], "rules": { "@typescript-eslint/no-explicit-any": "off" } diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts new file mode 100644 index 0000000..7a4f72b --- /dev/null +++ b/src/@types/global.d.ts @@ -0,0 +1,16 @@ +/** + * Extend window interface + */ +interface Window { + /** + * Gaode Maps + */ + // eslint-disable-next-line @typescript-eslint/naming-convention + AMap?: unknown; + + /** + * Baidu Maps + */ + // eslint-disable-next-line @typescript-eslint/naming-convention + BMap?: unknown; +} diff --git a/src/components/EchartsPanel/EchartsPanel.test.tsx b/src/components/EchartsPanel/EchartsPanel.test.tsx index d5dfb93..04d61ff 100644 --- a/src/components/EchartsPanel/EchartsPanel.test.tsx +++ b/src/components/EchartsPanel/EchartsPanel.test.tsx @@ -348,10 +348,15 @@ describe('Panel', () => { * Error handling section */ describe('Error handling', () => { - const error = { - message: 'some error', - stack: 'some stack', - }; + class CustomError extends Error { + constructor( + public name: string, + public stack: string + ) { + super(name); + } + } + const error = new CustomError('some error', 'stack'); beforeEach(() => { jest.mocked(registerMaps).mockImplementationOnce(() => { @@ -360,11 +365,28 @@ describe('Panel', () => { }); it('Should show errors if unable to register maps', () => { - render(getComponent({ options: { map: Map.JSON } })); + jest.mocked(registerMaps).mockImplementationOnce(() => { + throw error; + }); + + render( + getComponent({ + options: { + map: Map.JSON, + getOption: `return { + series: [] + }`, + }, + }) + ); expect(screen.getByText(error.message)).toBeInTheDocument(); }); it('Should show stack if unable to register maps', () => { + jest.mocked(registerMaps).mockImplementationOnce(() => { + throw error; + }); + render(getComponent({ options: { map: Map.JSON } })); expect(screen.getByText(error.stack)).toBeInTheDocument(); }); diff --git a/src/components/EchartsPanel/EchartsPanel.tsx b/src/components/EchartsPanel/EchartsPanel.tsx index 5f051f2..f53d07c 100644 --- a/src/components/EchartsPanel/EchartsPanel.tsx +++ b/src/components/EchartsPanel/EchartsPanel.tsx @@ -56,7 +56,7 @@ export const EchartsPanel: React.FC = ({ options, data, width, height, re /** * Transformations */ - const ecStat: any = echartsStat; + const ecStat = echartsStat; /** * Initialize Chart @@ -92,8 +92,8 @@ export const EchartsPanel: React.FC = ({ options, data, width, height, re const themeConfig = JSON.parse(options.themeEditor.config); echartsTheme = Theme.CUSTOM; echarts.registerTheme(echartsTheme, themeConfig); - } catch (e: any) { - setThemeError(e); + } catch (e: unknown) { + setThemeError(e instanceof Error ? e : new Error(`${e}`)); } } @@ -244,7 +244,7 @@ export const EchartsPanel: React.FC = ({ options, data, width, height, re /** * Chart option */ - let chartOption = {}; + let chartOption; /** * Default Option Config with merge disabled @@ -290,7 +290,7 @@ export const EchartsPanel: React.FC = ({ options, data, width, height, re chartOptionConfig ); } catch (err) { - setError(err as any); + setError(err instanceof Error ? err : new Error(`${err}`)); } return unsubscribeFn; diff --git a/src/components/SeriesEditor/SeriesEditor.tsx b/src/components/SeriesEditor/SeriesEditor.tsx index 08e7533..335c8c7 100644 --- a/src/components/SeriesEditor/SeriesEditor.tsx +++ b/src/components/SeriesEditor/SeriesEditor.tsx @@ -112,7 +112,7 @@ export const SeriesEditor: React.FC = ({ value, onChange, dataset }) => { */ const onAddNewItem = useCallback(() => { setNewItem(''); - const addedItem = getSeriesWithNewType({ id: newItem, name: '', uid: getSeriesUniqueId() } as any, SeriesType.LINE); + const addedItem = getSeriesWithNewType({ id: newItem, name: '', uid: getSeriesUniqueId() }, SeriesType.LINE); onChangeItems(items.concat(addedItem)); onToggleItem(addedItem); }, [items, newItem, onChangeItems, onToggleItem]); diff --git a/src/maps.ts b/src/maps.ts index f14efbe..dfc22df 100644 --- a/src/maps.ts +++ b/src/maps.ts @@ -22,7 +22,7 @@ export const registerMaps = () => { * Load Baidu Maps */ export const loadBaidu = (options: BaiduOptions) => { - if ((window as any).BMap) { + if (window.BMap) { return; } @@ -37,7 +37,7 @@ export const loadBaidu = (options: BaiduOptions) => { * Load Gaode Maps */ export const loadGaode = (options: GaodeOptions) => { - if ((window as any).AMap) { + if (window.AMap) { return; } diff --git a/src/utils/visual-editor.ts b/src/utils/visual-editor.ts index 352fd34..66f5471 100644 --- a/src/utils/visual-editor.ts +++ b/src/utils/visual-editor.ts @@ -32,7 +32,7 @@ export const getDatasetItemUniqueName = (item: DatasetItem) => { * @param items */ export const getDatasetSource = (frames: DataFrame[], items: DatasetItem[]): [string[], ...unknown[]] => { - const itemValuesMap = items.reduce((acc: Map, item) => { + const itemValuesMap = items.reduce((acc: Map, item) => { const frame = frames.find((frame) => item.source ? frame.refId === item.source : frame.fields.some((field) => field.name === item.name) ); @@ -62,8 +62,8 @@ export const getDatasetSource = (frames: DataFrame[], items: DatasetItem[]): [st * @param item * @param newType */ -export const getSeriesWithNewType = ( - item: SeriesItem, +export const getSeriesWithNewType = >( + item: TItem, newType: SeriesType ): SeriesByType => { const commonValues = {