Skip to content

Commit

Permalink
fix: hundreds lint warning (#447)
Browse files Browse the repository at this point in the history
* fix: lint

* fix: more lints

* fix: more lints

* fix: comment

* fix: any

* fix: ChartDataProvider test lint

* fix: lints

* fix: chartprops
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 26, 2021
1 parent 6d7de01 commit 67e15d0
Show file tree
Hide file tree
Showing 21 changed files with 188 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FunctionComponent, ComponentType } from 'react';
import { ComponentType } from 'react';
import { isRequired, Plugin } from '@superset-ui/core';
import { QueryFormData } from '@superset-ui/query';
import ChartMetadata from './ChartMetadata';
Expand All @@ -18,7 +18,7 @@ const EMPTY = {};

export type PromiseOrValue<T> = Promise<T> | T;
export type PromiseOrValueLoader<T> = () => PromiseOrValue<T>;
export type ChartType = ComponentType<any> | FunctionComponent<any>;
export type ChartType = ComponentType<any>;
type ValueOrModuleWithValue<T> = T | { default: T };

interface ChartPluginConfig<T extends QueryFormData> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import ChartClient from '../../src/clients/ChartClient';
import ChartDataProvider, { Props } from '../../src/components/ChartDataProvider';
import { bigNumberFormData } from '../fixtures/formData';

// Note: the mock implentatino of these function directly affects the expected results below
const defaultMockLoadFormData = jest.fn(allProps => Promise.resolve(allProps.formData));
// Note: the mock implementation of these function directly affects the expected results below
const defaultMockLoadFormData = jest.fn(({ formData }: { formData: unknown }) =>
Promise.resolve(formData),
);

type MockLoadFormData = typeof defaultMockLoadFormData | jest.Mock<Promise<unknown>, unknown[]>;

let mockLoadFormData: MockLoadFormData = defaultMockLoadFormData;

function createPromise<T>(input: T, ..._args: unknown[]) {
return Promise.resolve(input);
}

// coerce here else get: Type 'Mock<Promise<any>, []>' is not assignable to type 'Mock<Promise<any>, any[]>'
let mockLoadFormData = defaultMockLoadFormData as jest.Mock<Promise<any>, any>;
const mockLoadDatasource = jest.fn(datasource => Promise.resolve(datasource)) as jest.Mock<
Promise<any>,
any
>;
const mockLoadQueryData = jest.fn(input => Promise.resolve(input)) as jest.Mock<Promise<any>, any>;
const mockLoadDatasource = jest.fn<Promise<unknown>, unknown[]>(createPromise);
const mockLoadQueryData = jest.fn<Promise<unknown>, unknown[]>(createPromise);

// ChartClient is now a mock
jest.mock('../../src/clients/ChartClient', () =>
Expand Down Expand Up @@ -177,7 +182,7 @@ describe('ChartDataProvider', () => {

describe('children', () => {
it('calls children({ loading: true }) when loading', () => {
const children = jest.fn();
const children = jest.fn<React.ReactNode, unknown[]>();
setup({ children });

// during the first tick (before more promises resolve) loading is true
Expand All @@ -188,7 +193,7 @@ describe('ChartDataProvider', () => {
it('calls children({ payload }) when loaded', () => {
return new Promise(done => {
expect.assertions(2);
const children = jest.fn();
const children = jest.fn<React.ReactNode, unknown[]>();
setup({ children, loadDatasource: true });

setTimeout(() => {
Expand All @@ -208,7 +213,7 @@ describe('ChartDataProvider', () => {
it('calls children({ error }) upon request error', () => {
return new Promise(done => {
expect.assertions(2);
const children = jest.fn();
const children = jest.fn<React.ReactNode, unknown[]>();
mockLoadFormData = jest.fn(() => Promise.reject(new Error('error')));

setup({ children });
Expand All @@ -224,7 +229,7 @@ describe('ChartDataProvider', () => {
it('calls children({ error }) upon JS error', () => {
return new Promise(done => {
expect.assertions(2);
const children = jest.fn();
const children = jest.fn<React.ReactNode, unknown[]>();

mockLoadFormData = jest.fn(() => {
throw new Error('non-async error');
Expand All @@ -245,7 +250,7 @@ describe('ChartDataProvider', () => {
it('calls onLoad(payload) when loaded', () => {
return new Promise(done => {
expect.assertions(2);
const onLoaded = jest.fn();
const onLoaded = jest.fn<void, unknown[]>();
setup({ onLoaded, loadDatasource: true });

setTimeout(() => {
Expand All @@ -263,7 +268,7 @@ describe('ChartDataProvider', () => {
it('calls onError(error) upon request error', () => {
return new Promise(done => {
expect.assertions(2);
const onError = jest.fn();
const onError = jest.fn<void, unknown[]>();
mockLoadFormData = jest.fn(() => Promise.reject(new Error('error')));

setup({ onError });
Expand All @@ -278,7 +283,7 @@ describe('ChartDataProvider', () => {
it('calls onError(error) upon JS error', () => {
return new Promise(done => {
expect.assertions(2);
const onError = jest.fn();
const onError = jest.fn<void, unknown[]>();

mockLoadFormData = jest.fn(() => {
throw new Error('non-async error');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const TestComponent = ({
width,
height,
}: {
formData?: any;
formData?: unknown;
message?: string;
width?: number;
height?: number;
Expand Down Expand Up @@ -67,6 +67,10 @@ export class DiligentChartPlugin extends ChartPlugin<QueryFormData> {
}
}

function identity<T>(x: T) {
return x;
}

export class LazyChartPlugin extends ChartPlugin<QueryFormData> {
constructor() {
super({
Expand All @@ -77,7 +81,7 @@ export class LazyChartPlugin extends ChartPlugin<QueryFormData> {
// this mirrors `() => import(module)` syntax
loadChart: () => Promise.resolve({ default: TestComponent }),
// promise without .default
loadTransformProps: () => Promise.resolve((x: any) => x),
loadTransformProps: () => Promise.resolve(identity),
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('createLoadableRenderer', () => {
return <div className="test-component">test</div>;
}
let loadChartSuccess = jest.fn(() => Promise.resolve(TestComponent));
let render: (loaded: { [key: string]: any }) => JSX.Element;
let render: (loaded: { Chart: React.ComponentType }) => JSX.Element;
let loading: () => JSX.Element;
let LoadableRenderer: LoadableRendererType<{}, {}>;
let restoreConsole: RestoreConsole;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default function callApi({
Object.keys(postPayload).forEach(key => {
const value = postPayload[key];
if (typeof value !== 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
formData.append(key, stringify ? JSON.stringify(value) : value);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export type FetchRetryOptions = {
};
export type Headers = { [k: string]: string };
export type Host = string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Json = { [k: string]: any };
export type Method = RequestInit['method'];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type PostPayload = { [key: string]: any };
export type Mode = RequestInit['mode'];
export type Redirect = RequestInit['redirect'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ describe('SupersetClient', () => {
afterEach(SupersetClient.reset);

it('exposes reset, configure, init, get, post, isAuthenticated, and reAuthenticate methods', () => {
expect(SupersetClient.configure).toEqual(expect.any(Function));
expect(SupersetClient.init).toEqual(expect.any(Function));
expect(SupersetClient.get).toEqual(expect.any(Function));
expect(SupersetClient.post).toEqual(expect.any(Function));
expect(SupersetClient.isAuthenticated).toEqual(expect.any(Function));
expect(SupersetClient.reAuthenticate).toEqual(expect.any(Function));
expect(SupersetClient.request).toEqual(expect.any(Function));
expect(SupersetClient.reset).toEqual(expect.any(Function));
expect(typeof SupersetClient.configure).toBe('function');
expect(typeof SupersetClient.init).toBe('function');
expect(typeof SupersetClient.get).toBe('function');
expect(typeof SupersetClient.post).toBe('function');
expect(typeof SupersetClient.isAuthenticated).toBe('function');
expect(typeof SupersetClient.reAuthenticate).toBe('function');
expect(typeof SupersetClient.request).toBe('function');
expect(typeof SupersetClient.reset).toBe('function');
});

it('throws if you call init, get, post, isAuthenticated, or reAuthenticate before configure', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('SupersetClientClass', () => {
return new SupersetClientClass({})
.init()
.then(throwIfCalled)
.catch(error => {
.catch((error: { status: number }) => {
expect(error.status).toBe(403);

return true;
Expand All @@ -114,7 +114,7 @@ describe('SupersetClientClass', () => {
return new SupersetClientClass({})
.init()
.then(throwIfCalled)
.catch(error => {
.catch((error: unknown) => {
expect(error).toBeDefined();

return true;
Expand All @@ -129,7 +129,7 @@ describe('SupersetClientClass', () => {
return new SupersetClientClass({})
.init()
.then(throwIfCalled)
.catch(error => {
.catch((error: unknown) => {
expect(error).toBeDefined();

return true;
Expand Down Expand Up @@ -171,8 +171,10 @@ describe('SupersetClientClass', () => {
return client
.ensureAuth()
.then(throwIfCalled)
.catch(error => {
expect(error).toEqual(expect.objectContaining({ error: expect.any(String) }));
.catch((error: { error: string }) => {
expect(error).toEqual(
expect.objectContaining({ error: expect.any(String) }) as typeof error,
);
expect(client.isAuthenticated()).toBe(false);

return true;
Expand Down Expand Up @@ -209,14 +211,14 @@ describe('SupersetClientClass', () => {
return client
.init()
.then(throwIfCalled)
.catch(error => {
expect(error).toEqual(expect.objectContaining(rejectValue));
.catch((error: unknown) => {
expect(error).toEqual(expect.objectContaining(rejectValue) as unknown);

return client
.ensureAuth()
.then(throwIfCalled)
.catch(error2 => {
expect(error2).toEqual(expect.objectContaining(rejectValue));
.catch((error2: unknown) => {
expect(error2).toEqual(expect.objectContaining(rejectValue) as unknown);
expect(client.isAuthenticated()).toBe(false);

// reset
Expand Down Expand Up @@ -305,7 +307,7 @@ describe('SupersetClientClass', () => {
expect(fetchRequest.mode).toBe(clientConfig.mode);
expect(fetchRequest.credentials).toBe(clientConfig.credentials);
expect(fetchRequest.headers).toEqual(
expect.objectContaining(clientConfig.headers as Object),
expect.objectContaining(clientConfig.headers) as typeof fetchRequest.headers,
);

return true;
Expand Down Expand Up @@ -379,7 +381,7 @@ describe('SupersetClientClass', () => {
expect(fetchRequest.mode).toBe(overrideConfig.mode);
expect(fetchRequest.credentials).toBe(overrideConfig.credentials);
expect(fetchRequest.headers).toEqual(
expect.objectContaining(overrideConfig.headers as Object),
expect.objectContaining(overrideConfig.headers) as typeof fetchRequest.headers,
);

return true;
Expand Down Expand Up @@ -431,7 +433,7 @@ describe('SupersetClientClass', () => {
expect(fetchRequest.mode).toBe(overrideConfig.mode);
expect(fetchRequest.credentials).toBe(overrideConfig.credentials);
expect(fetchRequest.headers).toEqual(
expect.objectContaining(overrideConfig.headers as Object),
expect.objectContaining(overrideConfig.headers) as typeof fetchRequest.headers,
);

return true;
Expand All @@ -456,15 +458,15 @@ describe('SupersetClientClass', () => {
it('passes postPayload key,values in the body', () => {
expect.assertions(3);

const postPayload = { number: 123, array: [1, 2, 3] } as any;
const postPayload = { number: 123, array: [1, 2, 3] };
const client = new SupersetClientClass({ protocol, host });

return client.init().then(() =>
client.post({ url: mockPostUrl, postPayload }).then(() => {
const formData = fetchMock.calls(mockPostUrl)[0][1].body as FormData;
expect(fetchMock.calls(mockPostUrl)).toHaveLength(1);
Object.keys(postPayload).forEach(key => {
expect(formData.get(key)).toBe(JSON.stringify(postPayload[key]));
Object.entries(postPayload).forEach(([key, value]) => {
expect(formData.get(key)).toBe(JSON.stringify(value));
});

return true;
Expand All @@ -474,15 +476,15 @@ describe('SupersetClientClass', () => {

it('respects the stringify parameter for postPayload key,values', () => {
expect.assertions(3);
const postPayload = { number: 123, array: [1, 2, 3] } as any;
const postPayload = { number: 123, array: [1, 2, 3] };
const client = new SupersetClientClass({ protocol, host });

return client.init().then(() =>
client.post({ url: mockPostUrl, postPayload, stringify: false }).then(() => {
const formData = fetchMock.calls(mockPostUrl)[0][1].body as FormData;
expect(fetchMock.calls(mockPostUrl)).toHaveLength(1);
Object.keys(postPayload).forEach(key => {
expect(formData.get(key)).toBe(String(postPayload[key]));
Object.entries(postPayload).forEach(([key, value]) => {
expect(formData.get(key)).toBe(String(value));
});

return true;
Expand Down
Loading

0 comments on commit 67e15d0

Please sign in to comment.