Skip to content

Commit

Permalink
chore: add type checking in package tests directory (#19320)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenLYZ authored Mar 23, 2022
1 parent 54c521b commit b0397be
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const ADHOC_COLUMN: AdhocColumn = {
hasCustomLabel: true,
label: 'Adhoc column',
sqlExpression: 'case when 1 = 1 then 1 else 2 end',
expressionType: 'SQL',
};
const COLUMN_META: ColumnMeta = {
column_name: 'my_col',
Expand Down
3 changes: 2 additions & 1 deletion superset-frontend/packages/superset-ui-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"@types/math-expression-evaluator": "^1.2.1",
"@types/rison": "0.0.6",
"@types/seedrandom": "^2.4.28",
"@types/tinycolor2": "^1.4.3",
"@types/fetch-mock": "^7.3.3",
"@types/enzyme": "^3.10.5",
"@types/prop-types": "^15.7.2",
Expand Down Expand Up @@ -71,6 +70,8 @@
"@emotion/styled": "^11.3.0",
"@types/react": "*",
"@types/react-loadable": "*",
"@types/tinycolor2": "*",
"tinycolor2": "*",
"react": "^16.13.1",
"react-loadable": "^5.5.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export type QueryFormColumn = PhysicalColumn | AdhocColumn;
* Order query results by columns.
* Format: [metric/column, is_ascending].
*/
export type QueryFormOrderBy = [QueryFormColumn | QueryFormMetric, boolean];
export type QueryFormOrderBy =
| [QueryFormColumn | QueryFormMetric | {}, boolean]
| [];

export interface FormDataResidual {
[key: string]: any;
Expand Down Expand Up @@ -201,7 +203,7 @@ export interface SqlaFormData extends BaseFormData {
* Form data for Druid datasources.
*/
export interface DruidFormData extends BaseFormData {
granularity: string;
granularity?: string;
having_druid?: string;
druid_time_origin?: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('ChartDataProvider', () => {
setup({ loadDatasource: false });
setTimeout(() => {
expect(mockLoadDatasource.mock.calls).toHaveLength(0);
done();
done(undefined);
}, 0);
}));

Expand All @@ -132,7 +132,7 @@ describe('ChartDataProvider', () => {
expect(mockLoadDatasource.mock.calls[0][0]).toEqual(
props.formData.datasource,
);
done();
done(undefined);
}, 0);
}));

Expand All @@ -144,7 +144,7 @@ describe('ChartDataProvider', () => {
setTimeout(() => {
expect(mockLoadDatasource.mock.calls).toHaveLength(1);
expect(mockLoadDatasource.mock.calls[0][1]).toEqual(options);
done();
done(undefined);
}, 0);
}));

Expand All @@ -164,7 +164,7 @@ describe('ChartDataProvider', () => {
props.formData.datasource,
);
expect(mockLoadDatasource.mock.calls[1][0]).toEqual(newDatasource);
done();
done(undefined);
}, 0);
}));
});
Expand All @@ -177,7 +177,7 @@ describe('ChartDataProvider', () => {
setTimeout(() => {
expect(mockLoadQueryData.mock.calls).toHaveLength(1);
expect(mockLoadQueryData.mock.calls[0][0]).toEqual(props.formData);
done();
done(undefined);
}, 0);
}));

Expand All @@ -189,7 +189,7 @@ describe('ChartDataProvider', () => {
setTimeout(() => {
expect(mockLoadQueryData.mock.calls).toHaveLength(1);
expect(mockLoadQueryData.mock.calls[0][1]).toEqual(options);
done();
done(undefined);
}, 0);
}));

Expand All @@ -204,7 +204,7 @@ describe('ChartDataProvider', () => {
expect(mockLoadQueryData.mock.calls).toHaveLength(2);
expect(mockLoadQueryData.mock.calls[0][0]).toEqual(props.formData);
expect(mockLoadQueryData.mock.calls[1][0]).toEqual(newFormData);
done();
done(undefined);
}, 0);
}));
});
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('ChartDataProvider', () => {
queriesData: [props.formData],
},
});
done();
done(undefined);
}, 0);
}));

Expand All @@ -251,7 +251,7 @@ describe('ChartDataProvider', () => {
expect(children.mock.calls[1][0]).toEqual({
error: new Error('error'),
});
done();
done(undefined);
}, 0);
}));

Expand All @@ -271,7 +271,7 @@ describe('ChartDataProvider', () => {
expect(children.mock.calls[1][0]).toEqual({
error: new Error('non-async error'),
});
done();
done(undefined);
}, 0);
}));
});
Expand All @@ -290,7 +290,7 @@ describe('ChartDataProvider', () => {
datasource: props.formData.datasource,
queriesData: [props.formData],
});
done();
done(undefined);
}, 0);
}));

Expand All @@ -304,7 +304,7 @@ describe('ChartDataProvider', () => {
setTimeout(() => {
expect(onError.mock.calls).toHaveLength(1);
expect(onError.mock.calls[0][0]).toEqual(new Error('error'));
done();
done(undefined);
}, 0);
}));

Expand All @@ -323,7 +323,7 @@ describe('ChartDataProvider', () => {
expect(onError.mock.calls[0][0]).toEqual(
new Error('non-async error'),
);
done();
done(undefined);
}, 0);
}));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('createLoadableRenderer', () => {
expect(render).not.toHaveBeenCalled();
expect(onRenderSuccess).not.toHaveBeenCalled();
expect(onRenderFailure).toHaveBeenCalledTimes(1);
done();
done(undefined);
}, 10);
}));

Expand All @@ -126,7 +126,7 @@ describe('createLoadableRenderer', () => {
expect(loadChartFailure).toHaveBeenCalledTimes(1);
setTimeout(() => {
expect(render).not.toHaveBeenCalled();
done();
done(undefined);
}, 10);
}));

Expand All @@ -138,7 +138,7 @@ describe('createLoadableRenderer', () => {
setTimeout(() => {
// but rendered after the component is loaded.
expect(wrapper.find(TestComponent)).toHaveLength(1);
done();
done(undefined);
}, 10);
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('reactify(renderFn)', () => {
expect(renderFn).toHaveBeenCalledTimes(2);
expect(wrapper.html()).toEqual('<div id="test"><b>def</b></div>');
wrapper.unmount();
done();
done(undefined);
}, 20);
}));
describe('displayName', () => {
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('reactify(renderFn)', () => {
setTimeout(() => {
wrapper.unmount();
expect(willUnmountCb).toHaveBeenCalledTimes(1);
done();
done(undefined);
}, 20);
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('callApi()', () => {

await callApi(mockRequest);
const calls = fetchMock.calls(mockGetUrl);
const fetchParams = calls[0][1];
const fetchParams = calls[0][1] as RequestInit;
expect(calls).toHaveLength(1);
expect(fetchParams.mode).toBe(mockRequest.mode);
expect(fetchParams.cache).toBe(mockRequest.cache);
Expand All @@ -118,7 +118,7 @@ describe('callApi()', () => {
const calls = fetchMock.calls(mockPostUrl);
expect(calls).toHaveLength(1);

const fetchParams = calls[0][1];
const fetchParams = calls[0][1] as RequestInit;
const body = fetchParams.body as FormData;

Object.entries(postPayload).forEach(([key, value]) => {
Expand All @@ -135,7 +135,7 @@ describe('callApi()', () => {
const calls = fetchMock.calls(mockPostUrl);
expect(calls).toHaveLength(1);

const fetchParams = calls[0][1];
const fetchParams = calls[0][1] as RequestInit;
const body = fetchParams.body as FormData;
expect(body.get('key')).toBe(JSON.stringify(postPayload.key));
expect(body.get('noValue')).toBeNull();
Expand Down Expand Up @@ -166,10 +166,10 @@ describe('callApi()', () => {
const calls = fetchMock.calls(mockPostUrl);
expect(calls).toHaveLength(3);

const stringified = calls[0][1].body as FormData;
const unstringified = calls[1][1].body as FormData;
const stringified = (calls[0][1] as RequestInit).body as FormData;
const unstringified = (calls[1][1] as RequestInit).body as FormData;
const jsonRequestBody = JSON.parse(
calls[2][1].body as string,
(calls[2][1] as RequestInit).body as string,
) as JsonObject;

Object.entries(postPayload).forEach(([key, value]) => {
Expand All @@ -189,7 +189,7 @@ describe('callApi()', () => {
const calls = fetchMock.calls(mockPutUrl);
expect(calls).toHaveLength(1);

const fetchParams = calls[0][1];
const fetchParams = calls[0][1] as RequestInit;
const body = fetchParams.body as FormData;

Object.entries(postPayload).forEach(([key, value]) => {
Expand All @@ -206,7 +206,7 @@ describe('callApi()', () => {
const calls = fetchMock.calls(mockPutUrl);
expect(calls).toHaveLength(1);

const fetchParams = calls[0][1];
const fetchParams = calls[0][1] as RequestInit;
const body = fetchParams.body as FormData;
expect(body.get('key')).toBe(JSON.stringify(postPayload.key));
expect(body.get('noValue')).toBeNull();
Expand Down Expand Up @@ -236,8 +236,8 @@ describe('callApi()', () => {
const calls = fetchMock.calls(mockPutUrl);
expect(calls).toHaveLength(2);

const stringified = calls[0][1].body as FormData;
const unstringified = calls[1][1].body as FormData;
const stringified = (calls[0][1] as RequestInit).body as FormData;
const unstringified = (calls[1][1] as RequestInit).body as FormData;

Object.entries(postPayload).forEach(([key, value]) => {
expect(stringified.get(key)).toBe(JSON.stringify(value));
Expand All @@ -255,7 +255,7 @@ describe('callApi()', () => {
const calls = fetchMock.calls(mockPatchUrl);
expect(calls).toHaveLength(1);

const fetchParams = calls[0][1];
const fetchParams = calls[0][1] as RequestInit;
const body = fetchParams.body as FormData;

Object.entries(postPayload).forEach(([key, value]) => {
Expand All @@ -272,7 +272,7 @@ describe('callApi()', () => {
const calls = fetchMock.calls(mockPatchUrl);
expect(calls).toHaveLength(1);

const fetchParams = calls[0][1];
const fetchParams = calls[0][1] as RequestInit;
const body = fetchParams.body as FormData;
expect(body.get('key')).toBe(JSON.stringify(postPayload.key));
expect(body.get('noValue')).toBeNull();
Expand Down Expand Up @@ -302,8 +302,8 @@ describe('callApi()', () => {
const calls = fetchMock.calls(mockPatchUrl);
expect(calls).toHaveLength(2);

const stringified = calls[0][1].body as FormData;
const unstringified = calls[1][1].body as FormData;
const stringified = (calls[0][1] as RequestInit).body as FormData;
const unstringified = (calls[1][1] as RequestInit).body as FormData;

Object.entries(postPayload).forEach(([key, value]) => {
expect(stringified.get(key)).toBe(JSON.stringify(value));
Expand Down Expand Up @@ -366,7 +366,7 @@ describe('callApi()', () => {
url: mockCacheUrl,
method: 'GET',
});
const fetchParams = calls[1][1];
const fetchParams = calls[1][1] as RequestInit;
expect(calls).toHaveLength(2);
// second call should not have If-None-Match header
expect(fetchParams.headers).toBeUndefined();
Expand All @@ -386,7 +386,7 @@ describe('callApi()', () => {

// second call sends the Etag in the If-None-Match header
await callApi({ url: mockCacheUrl, method: 'GET' });
const fetchParams = calls[1][1];
const fetchParams = calls[1][1] as RequestInit;
const headers = { 'If-None-Match': 'etag' };
expect(calls).toHaveLength(2);
expect(fetchParams.headers).toEqual(
Expand Down Expand Up @@ -591,7 +591,7 @@ describe('callApi()', () => {
method: 'POST',
postPayload: payload,
});
expect(fetchMock.lastOptions().body).toBe(payload);
expect(fetchMock.lastOptions()?.body).toBe(payload);
});

it('should ignore "null" postPayload string', async () => {
Expand All @@ -602,6 +602,6 @@ describe('callApi()', () => {
method: 'POST',
postPayload: 'null',
});
expect(fetchMock.lastOptions().body).toBeUndefined();
expect(fetchMock.lastOptions()?.body).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('makeApi()', () => {

const expected = new FormData();
expected.append('request', JSON.stringify('test'));
const received = fetchMock.lastOptions().body as FormData;
const received = fetchMock.lastOptions()?.body as FormData;

expect(received).toBeInstanceOf(FormData);
expect(received.get('request')).toEqual(expected.get('request'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ describe('buildQueryObject', () => {
name: 'My Formula',
opacity: AnnotationOpacity.Low,
show: true,
showLabel: false,
style: AnnotationStyle.Solid,
value: '10*sin(x)',
width: 1,
Expand All @@ -235,6 +236,7 @@ describe('buildQueryObject', () => {
annotationType: AnnotationType.Interval,
color: null,
show: false,
showLabel: false,
name: 'My Interval',
sourceType: AnnotationSourceType.Native,
style: AnnotationStyle.Dashed,
Expand All @@ -253,6 +255,7 @@ describe('buildQueryObject', () => {
},
sourceType: AnnotationSourceType.Table,
show: false,
showLabel: false,
timeColumn: 'ds',
style: AnnotationStyle.Dashed,
value: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('getColumnLabel', () => {
getColumnLabel({
sqlExpression: "case when 1 then 'a' else 'b' end",
label: 'my col',
expressionType: 'SQL',
}),
).toEqual('my col');
});
Expand All @@ -36,6 +37,7 @@ describe('getColumnLabel', () => {
expect(
getColumnLabel({
sqlExpression: "case when 1 then 'a' else 'b' end",
expressionType: 'SQL',
}),
).toEqual("case when 1 then 'a' else 'b' end");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ describe('normalizeOrderBy', () => {
datasource: '5__table',
viz_type: 'table',
time_range: '1 year ago : 2013',
// @ts-ignore
orderby: [['count(*)', 'true']],
};
expect(normalizeOrderBy(query)).not.toHaveProperty('orderby');
Expand Down
Loading

0 comments on commit b0397be

Please sign in to comment.