Unverified Commit c6321020 authored by Marcos Iglesias's avatar Marcos Iglesias Committed by GitHub

chore: Testing types for table input (#699)

Signed-off-by: 's avatarMarcos Iglesias Valle <golodhros@gmail.com>
parent 0bc7b690
...@@ -99,9 +99,6 @@ exports[`strict null compilation`] = { ...@@ -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"], [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"] [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": [ "js/components/common/Tags/TagInput/index.tsx:3754832290": [
[63, 22, 6, "Type \'undefined\' is not assignable to type \'GetAllTagsRequest\'.", "1979467425"], [63, 22, 6, "Type \'undefined\' is not assignable to type \'GetAllTagsRequest\'.", "1979467425"],
[66, 4, 4, "Type \'undefined\' is not assignable to type \'Tag[]\'.", "2087952548"], [66, 4, 4, "Type \'undefined\' is not assignable to type \'Tag[]\'.", "2087952548"],
......
...@@ -6,7 +6,7 @@ addons.setConfig({ ...@@ -6,7 +6,7 @@ addons.setConfig({
isFullscreen: false, isFullscreen: false,
showNav: true, showNav: true,
showPanel: true, showPanel: true,
panelPosition: 'bottom', panelPosition: 'right',
sidebarAnimations: true, sidebarAnimations: true,
enableShortcuts: true, enableShortcuts: true,
isToolshown: true, isToolshown: true,
......
...@@ -76,8 +76,8 @@ type FormattedDataType = { ...@@ -76,8 +76,8 @@ type FormattedDataType = {
usage: number | null; usage: number | null;
stats: StatType | null; stats: StatType | null;
action: string; action: string;
editText?: string; editText: string | null;
editUrl?: string; editUrl: string | null;
index: number; index: number;
name: string; name: string;
sort_order: string; sort_order: string;
...@@ -168,8 +168,8 @@ const ExpandedRowComponent: React.FC<ExpandedRowProps> = ( ...@@ -168,8 +168,8 @@ const ExpandedRowComponent: React.FC<ExpandedRowProps> = (
<EditableSection <EditableSection
title={EDITABLE_SECTION_TITLE} title={EDITABLE_SECTION_TITLE}
readOnly={!rowValue.isEditable} readOnly={!rowValue.isEditable}
editText={rowValue.editText} editText={rowValue.editText || undefined}
editUrl={rowValue.editUrl} editUrl={rowValue.editUrl || undefined}
> >
<ColumnDescEditableText <ColumnDescEditableText
columnIndex={rowValue.index} columnIndex={rowValue.index}
...@@ -219,8 +219,8 @@ const ColumnList: React.FC<ColumnListProps> = ({ ...@@ -219,8 +219,8 @@ const ColumnList: React.FC<ColumnListProps> = ({
action: item.name, action: item.name,
name: item.name, name: item.name,
isEditable: item.is_editable, isEditable: item.is_editable,
editText, editText: editText || null,
editUrl, editUrl: editUrl || null,
index, index,
}; };
}); });
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import * as React from 'react'; import * as React from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import { mocked } from 'ts-jest/utils';
import Table, { TableProps } from '.'; import Table, { TableProps } from '.';
...@@ -400,6 +401,28 @@ describe('Table', () => { ...@@ -400,6 +401,28 @@ describe('Table', () => {
}); });
}); });
}); });
describe('when columns specify fields not in the data', () => {
const { columns, data } = dataBuilder.withWrongData().build();
beforeEach(() => {
jest.spyOn(console, 'error');
mocked(console.error).mockImplementation(jest.fn);
});
afterEach(() => {
mocked(console.error).mockRestore();
});
it('throws an error', () => {
expect(() => {
setup({
data,
columns,
});
}).toThrow();
});
});
}); });
describe('options', () => { describe('options', () => {
......
...@@ -24,6 +24,12 @@ export interface TableColumn { ...@@ -24,6 +24,12 @@ export interface TableColumn {
width?: number; width?: number;
// sortable?: bool (false) // sortable?: bool (false)
} }
type Some = string | number | boolean | symbol | bigint | object;
type ValidData = Record<string, Some | null>; // Removes the undefined values
interface RowData {
[key: string]: Some | null;
}
export interface TableOptions { export interface TableOptions {
tableClassName?: string; tableClassName?: string;
...@@ -37,13 +43,25 @@ export interface TableOptions { ...@@ -37,13 +43,25 @@ export interface TableOptions {
} }
export interface TableProps { export interface TableProps {
data: RowData[];
columns: TableColumn[]; columns: TableColumn[];
data: any[];
options?: TableOptions; options?: TableOptions;
} }
type RowStyles = {
height: string;
};
type EmptyRowProps = {
colspan: number;
rowStyles: RowStyles;
emptyMessage?: string;
};
const DEFAULT_EMPTY_MESSAGE = 'No Results'; const DEFAULT_EMPTY_MESSAGE = 'No Results';
const EXPAND_ROW_TEXT = 'Expand Row'; const EXPAND_ROW_TEXT = 'Expand Row';
const INVALID_DATA_ERROR_MESSAGE =
'Invalid data! Your data does not contain the fields specified on the columns property.';
const DEFAULT_LOADING_ITEMS = 3; const DEFAULT_LOADING_ITEMS = 3;
const DEFAULT_ROW_HEIGHT = 30; const DEFAULT_ROW_HEIGHT = 30;
const EXPANDING_CELL_WIDTH = '70px'; const EXPANDING_CELL_WIDTH = '70px';
...@@ -55,19 +73,26 @@ const ALIGNEMENT_TO_CLASS_MAP = { ...@@ -55,19 +73,26 @@ const ALIGNEMENT_TO_CLASS_MAP = {
center: 'is-center-aligned', center: 'is-center-aligned',
}; };
type RowStyles = {
height: string;
};
type EmptyRowProps = {
colspan: number;
rowStyles: RowStyles;
emptyMessage?: string;
};
const getCellAlignmentClass = (alignment: TextAlignmentValues) => const getCellAlignmentClass = (alignment: TextAlignmentValues) =>
ALIGNEMENT_TO_CLASS_MAP[alignment]; ALIGNEMENT_TO_CLASS_MAP[alignment];
const fieldIsDefined = (field, row) => row[field] !== undefined;
const checkIfValidData = (
data: unknown[],
fields: string[]
): data is ValidData[] => {
let isValid = true;
for (let i = 0; i < fields.length; i++) {
if (!data.some(fieldIsDefined.bind(null, fields[i]))) {
isValid = false;
break;
}
}
return isValid;
};
const EmptyRow: React.FC<EmptyRowProps> = ({ const EmptyRow: React.FC<EmptyRowProps> = ({
colspan, colspan,
rowStyles, rowStyles,
...@@ -182,6 +207,10 @@ const Table: React.FC<TableProps> = ({ ...@@ -182,6 +207,10 @@ const Table: React.FC<TableProps> = ({
); );
if (data.length) { if (data.length) {
if (!checkIfValidData(data, fields)) {
throw new Error(INVALID_DATA_ERROR_MESSAGE);
}
body = data.map((item, index) => { body = data.map((item, index) => {
return ( return (
<React.Fragment key={`index:${index}`}> <React.Fragment key={`index:${index}`}>
......
...@@ -39,8 +39,6 @@ const expandRowComponent = (rowValue, index) => ( ...@@ -39,8 +39,6 @@ const expandRowComponent = (rowValue, index) => (
</strong> </strong>
); );
// const stories = storiesOf('Components/Table', module);
export const TableStates = () => ( export const TableStates = () => (
<> <>
<StorySection title="Basic Table"> <StorySection title="Basic Table">
......
...@@ -44,6 +44,36 @@ function TestDataBuilder(config = {}) { ...@@ -44,6 +44,36 @@ function TestDataBuilder(config = {}) {
...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',
},
],
};
return new this.Klass(attr);
};
this.withUsageRow = () => { this.withUsageRow = () => {
const attr = { const attr = {
data: [ data: [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment