Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Testing types for table input #699

Merged
merged 10 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions amundsen_application/static/.betterer.results
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ exports[`strict null compilation`] = {
[255, 6, 11, "No overload matches this call.\\n The last overload gave the following error.\\n Type \'(() => SubmitSearchRequest) | null\' is not assignable to type \'ActionCreator<any>\'.\\n Type \'null\' is not assignable to type \'ActionCreator<any>\'.", "2296208050"],
[270, 4, 18, "No overload matches this call.\\n The last overload gave the following error.\\n Argument of type \'(dispatch: any, ownProps: any) => ActionCreator<unknown>\' is not assignable to parameter of type \'DispatchFromProps\'.\\n Type \'(dispatch: any, ownProps: any) => ActionCreator<unknown>\' is missing the following properties from type \'DispatchFromProps\': submitSearch, onInputChange, onSelectInlineResult", "2926224796"]
],
"js/components/common/Table/index.tsx:489040313": [
[235, 22, 13, "Type \'unknown\' is not assignable to type \'ReactNode\'.\\n Type \'unknown\' is not assignable to type \'ReactPortal\'.", "971959308"]
],
"js/components/common/Tags/TagInput/index.tsx:3754832290": [
[63, 22, 6, "Type \'undefined\' is not assignable to type \'GetAllTagsRequest\'.", "1979467425"],
[66, 4, 4, "Type \'undefined\' is not assignable to type \'Tag[]\'.", "2087952548"],
Expand Down
2 changes: 1 addition & 1 deletion amundsen_application/static/.storybook/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ addons.setConfig({
isFullscreen: false,
showNav: true,
showPanel: true,
panelPosition: 'bottom',
panelPosition: 'right',
sidebarAnimations: true,
enableShortcuts: true,
isToolshown: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,19 @@ describe('Table', () => {
});
});
});

describe('when columns specify fields not in the data', () => {
const { columns, data } = dataBuilder.withWrongData().build();

it('throws an error', () => {
expect(() => {
setup({
data,
columns,
});
}).toThrow();
});
});
});

describe('options', () => {
Expand Down
51 changes: 40 additions & 11 deletions amundsen_application/static/js/components/common/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export interface TableColumn {
width?: number;
// sortable?: bool (false)
}
type Some = string | number | boolean | symbol | bigint | object;
type ValidData = Record<string, Some>; // Removes the undefined | null values

interface RowData {
[key: string]: Some;
}

export interface TableOptions {
tableClassName?: string;
Expand All @@ -37,13 +43,25 @@ export interface TableOptions {
}

export interface TableProps {
data: RowData[];
columns: TableColumn[];
data: any[];
options?: TableOptions;
}

type RowStyles = {
height: string;
};

type EmptyRowProps = {
colspan: number;
rowStyles: RowStyles;
emptyMessage?: string;
};

const DEFAULT_EMPTY_MESSAGE = 'No Results';
const EXPAND_ROW_TEXT = 'Expand Row';
const INVALID_DATA_ERROR_MESSAGE =
Golodhros marked this conversation as resolved.
Show resolved Hide resolved
'Invalid data! Your data does not contain the fields specified on the columns property.';
const DEFAULT_LOADING_ITEMS = 3;
const DEFAULT_ROW_HEIGHT = 30;
const EXPANDING_CELL_WIDTH = '70px';
Expand All @@ -55,19 +73,26 @@ const ALIGNEMENT_TO_CLASS_MAP = {
center: 'is-center-aligned',
};

type RowStyles = {
height: string;
};

type EmptyRowProps = {
colspan: number;
rowStyles: RowStyles;
emptyMessage?: string;
};

const getCellAlignmentClass = (alignment: TextAlignmentValues) =>
ALIGNEMENT_TO_CLASS_MAP[alignment];

const fieldIsDefined = (field, row) => row[field] !== undefined;

const checkIfValidData = (
data: unknown[],
fields: string[]
): data is ValidData[] => {
let isValid = true;

fields.forEach((field) => {
if (data.filter(fieldIsDefined.bind(null, field)).length === 0) {
isValid = false;
}
});
Golodhros marked this conversation as resolved.
Show resolved Hide resolved

return isValid;
};

const EmptyRow: React.FC<EmptyRowProps> = ({
colspan,
rowStyles,
Expand Down Expand Up @@ -182,6 +207,10 @@ const Table: React.FC<TableProps> = ({
);

if (data.length) {
if (!checkIfValidData(data, fields)) {
throw new Error(INVALID_DATA_ERROR_MESSAGE);
}

body = data.map((item, index) => {
return (
<React.Fragment key={`index:${index}`}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ const expandRowComponent = (rowValue, index) => (
</strong>
);

// const stories = storiesOf('Components/Table', module);

export const TableStates = () => (
<>
<StorySection title="Basic Table">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ function TestDataBuilder(config = {}) {
...config,
};

this.withWrongData = () => {
const attr = {
data: [
{ name: 'rowName', type: 'rowType', value: 1 },
{ name: 'rowName2', type: 'rowType2', value: 2 },
{ name: 'rowName3', type: 'rowType3', value: 3 },
],
columns: [
{
title: 'Name',
field: 'name',
},
{
title: 'Type',
field: 'type',
},
{
title: 'Value',
field: 'value',
},
{
title: 'Bad Field',
field: 'badfield',
},
Golodhros marked this conversation as resolved.
Show resolved Hide resolved
],
};

return new this.Klass(attr);
};

this.withUsageRow = () => {
const attr = {
data: [
Expand Down