Skip to content

Commit

Permalink
fix: use object to replace CellItem class
Browse files Browse the repository at this point in the history
- add createCellItem function
- add unit tests for createCellItem
- rename SC_DataStream to SynchroChartsDataStream for better consistency
  • Loading branch information
square-li committed Jun 15, 2022
1 parent b484a81 commit 7f1e632
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 34 deletions.
27 changes: 27 additions & 0 deletions packages/table/src/utils/createCellItem.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createCellItem } from './createCellItem';
import { CellItem } from './types';

describe('createCellItem', () => {
it('creates CellItem', () => {
const props = { value: 10, error: undefined, isLoading: false, threshold: undefined };
const item: CellItem = createCellItem(props);
expect(item).toMatchObject({ value: 10 });
expect(item.valueOf()).toEqual(10);
});
});

describe('CellItem', () => {
it('return error message when in error state', () => {
const props = { value: 10, error: { msg: 'Some error' }, isLoading: false, threshold: undefined };
const item: CellItem = createCellItem(props);

expect(`${item}`).toBe('Some error');
});

it('return "Loading" when in loading state', () => {
const props = { value: 10, isLoading: true, threshold: undefined };
const item: CellItem = createCellItem(props);

expect(`${item}`).toBe('Loading');
});
});
34 changes: 34 additions & 0 deletions packages/table/src/utils/createCellItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Primitive, Threshold } from '@synchro-charts/core';
import { ErrorDetails } from '@iot-app-kit/core';
import { CellItem } from './types';

type CellProps = {
value?: Primitive;
error?: ErrorDetails;
isLoading?: boolean;
threshold?: Threshold;
};
export const createCellItem: (props?: CellProps) => CellItem = ({ value, error, isLoading, threshold } = {}) => {
const valueOf = () => {
if (error) {
return error.msg;
}
if (isLoading) {
return 'Loading';
}
return value;
};

const toString = () => {
return `${valueOf()}`;
};

return {
value,
error,
isLoading,
threshold,
valueOf,
toString,
};
};
23 changes: 12 additions & 11 deletions packages/table/src/utils/createTableItems.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Viewport, DataStream } from '@iot-app-kit/core';
import { breachedThreshold, Primitive, Threshold, DataStream as SC_DataStream } from '@synchro-charts/core';
import { Viewport, DataStream, ErrorDetails } from '@iot-app-kit/core';
import { breachedThreshold, Primitive, Threshold, DataStream as SynchroChartsDataStream } from '@synchro-charts/core';
import { getDataBeforeDate } from './dataFilters';
import { getDataPoints } from './getDataPoints';
import { CellItem, Item, ItemRef, TableItem } from './types';
import { createCellItem } from './createCellItem';

export const createTableItems: (config: {
dataStreams: DataStream[];
Expand All @@ -12,7 +13,7 @@ export const createTableItems: (config: {
}) => TableItem[] = ({ dataStreams, viewport, items, thresholds = [] }) => {
return items.map((item) => {
const keys = Object.keys(item);
const keyDataPairs = keys.map((key) => {
const keyDataPairs = keys.map<{ key: string; data: CellItem }>((key) => {
if (typeof item[key] === 'object' && Object.prototype.hasOwnProperty.call(item[key], '$cellRef')) {
const { $cellRef } = item[key] as ItemRef;
const dataStream = dataStreams.find(({ id }) => id === $cellRef.id);
Expand All @@ -25,29 +26,29 @@ export const createTableItems: (config: {
const point = getDataBeforeDate(dataPoints, viewport.end).pop();
const value = point?.y;
const threshold = breachedThreshold({
dataStream: dataStream as SC_DataStream,
dataStreams: dataStreams as SC_DataStream[],
dataStream: dataStream as SynchroChartsDataStream,
dataStreams: dataStreams as SynchroChartsDataStream[],
value,
thresholds,
date: viewport.end,
});
return { key, data: new CellItem({ value, error, isLoading, threshold }) };
return { key, data: createCellItem({ value, error, isLoading, threshold }) };
}

const value = dataPoints.slice(-1)[0]?.y;
const threshold = breachedThreshold({
dataStream: dataStream as SC_DataStream,
dataStreams: dataStreams as SC_DataStream[],
dataStream: dataStream as SynchroChartsDataStream,
dataStreams: dataStreams as SynchroChartsDataStream[],
value,
thresholds,
date: new Date(Date.now()),
});

return { key, data: new CellItem({ value, error, isLoading, threshold }) };
return { key, data: createCellItem({ value, error, isLoading, threshold }) };
}
return { key, data: new CellItem() };
return { key, data: createCellItem() };
}
return { key, data: new CellItem({ value: item[key] as Primitive }) };
return { key, data: createCellItem({ value: item[key] as Primitive }) };
});

return keyDataPairs.reduce(
Expand Down
26 changes: 3 additions & 23 deletions packages/table/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,13 @@ export type CellItemProps = {
threshold?: Threshold;
};

export class CellItem {
export type CellItem = {
value?: Primitive;

error?: ErrorDetails;

isLoading?: boolean;

threshold?: Threshold;

valueOf: () => Primitive | undefined;

constructor(props?: CellItemProps) {
const { value, error, isLoading, threshold } = props || {};
this.value = value;
this.error = error;
this.isLoading = isLoading;
this.threshold = threshold;
this.valueOf = () => {
if (error) {
return error.msg;
}
if (isLoading) {
return 'Loading';
}
return value;
};
}
}
toString: () => string;
};

export type TableItem = { [k in string]: CellItem };

0 comments on commit 7f1e632

Please sign in to comment.