-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use object to replace CellItem class
- add createCellItem function - add unit tests for createCellItem - rename SC_DataStream to SynchroChartsDataStream for better consistency
- Loading branch information
Showing
4 changed files
with
76 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters