-
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.
chore(table): add missing tests (#215)
- render correct data based on different viewports - render spinner when a datastream is loading - render error icon and message when a datastream in error state - render icon and apply styles when it breaches thresholds
- Loading branch information
Showing
5 changed files
with
199 additions
and
3 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,5 @@ | ||
// Don't process node_modules except for AWS UI. | ||
module.exports = { | ||
ignore: [/node_modules\/?!@awsui\/components-react/], | ||
presets: ['@babel/preset-env'], | ||
}; |
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
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,179 @@ | ||
import React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { DataStream, Viewport } from '@iot-app-kit/core'; | ||
import { Annotations, COMPARISON_OPERATOR, getThresholds, StatusIcon } from '@synchro-charts/core'; | ||
import { ColumnDefinition, createTableItems, DefaultTableMessages, Item } from '../utils'; | ||
import { Table } from './index'; | ||
|
||
globalThis.IS_REACT_ACT_ENVIRONMENT = true; | ||
const dataStreamId = 'datastream-1'; | ||
const dataStream: DataStream = { | ||
id: dataStreamId, | ||
data: [ | ||
{ x: new Date(2021, 1, 1).getTime(), y: 100 }, | ||
{ x: new Date(2022, 1, 1).getTime(), y: 200 }, | ||
], | ||
resolution: 0, | ||
}; | ||
|
||
const items: Item[] = [ | ||
{ | ||
value: { | ||
$cellRef: { | ||
id: dataStreamId, | ||
resolution: 0, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
const columnDefinitions: ColumnDefinition[] = [ | ||
{ | ||
header: 'value', | ||
key: 'value', | ||
}, | ||
]; | ||
|
||
let container: HTMLDivElement; | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
act(() => { | ||
ReactDOM.unmountComponentAtNode(container); | ||
container.remove(); | ||
}); | ||
}); | ||
|
||
it('renders correct data when viewport defined by duration', async () => { | ||
const tableItems = createTableItems( | ||
{ | ||
dataStreams: [dataStream], | ||
items, | ||
viewport: { | ||
duration: '1000', | ||
}, | ||
}, | ||
DefaultTableMessages | ||
); | ||
|
||
act(() => { | ||
ReactDOM.render( | ||
<Table columnDefinitions={columnDefinitions} messageOverrides={DefaultTableMessages} items={tableItems} />, | ||
container | ||
); | ||
}); | ||
|
||
const cell = container.getElementsByClassName('iot-table-cell').item(0) as HTMLDivElement; | ||
expect(cell.innerHTML).toMatch('200'); | ||
}); | ||
|
||
it('renders correct data when the viewport defined by start and end time', async () => { | ||
const viewport: Viewport = { | ||
start: new Date(2021, 0, 0, 0, 0, 0), | ||
end: new Date(2021, 12, 30, 0, 0, 0), | ||
}; | ||
const tableItems = createTableItems({ dataStreams: [dataStream], items, viewport }, DefaultTableMessages); | ||
act(() => { | ||
ReactDOM.render( | ||
<Table columnDefinitions={columnDefinitions} messageOverrides={DefaultTableMessages} items={tableItems} />, | ||
container | ||
); | ||
}); | ||
|
||
const cell = container.getElementsByClassName('iot-table-cell').item(0) as HTMLDivElement; | ||
expect(cell.innerHTML).toMatch('100'); | ||
}); | ||
|
||
it('renders loading circle when datastream is in loading state', async () => { | ||
const loadingDatastream: DataStream = { | ||
isLoading: true, | ||
data: [], | ||
id: dataStreamId, | ||
resolution: 0, | ||
}; | ||
const tableItems = createTableItems( | ||
{ dataStreams: [loadingDatastream], items, viewport: { duration: '1000' } }, | ||
DefaultTableMessages | ||
); | ||
|
||
act(() => { | ||
ReactDOM.render( | ||
<Table columnDefinitions={columnDefinitions} messageOverrides={DefaultTableMessages} items={tableItems} />, | ||
container | ||
); | ||
}); | ||
|
||
const svg = container.getElementsByTagName('svg').item(0) as SVGElement; | ||
expect(svg).toBeTruthy(); | ||
}); | ||
|
||
it('renders icon and applies style when a datastream breaches threshold', async () => { | ||
const annotations: Annotations = { | ||
y: [ | ||
{ | ||
color: 'red', | ||
value: 30, | ||
comparisonOperator: COMPARISON_OPERATOR.GREATER_THAN, | ||
icon: StatusIcon.ERROR, | ||
dataStreamIds: [dataStreamId], | ||
}, | ||
], | ||
}; | ||
const tableItems = createTableItems( | ||
{ dataStreams: [dataStream], items, viewport: { duration: '1000' }, thresholds: getThresholds(annotations) }, | ||
DefaultTableMessages | ||
); | ||
|
||
act(() => { | ||
ReactDOM.render( | ||
<Table columnDefinitions={columnDefinitions} messageOverrides={DefaultTableMessages} items={tableItems} />, | ||
container | ||
); | ||
}); | ||
|
||
const cell = container.getElementsByClassName('iot-table-cell').item(0) as HTMLDivElement; | ||
expect(cell.style.color).toEqual('red'); | ||
const iconContainer = cell.childNodes.item(0); | ||
expect(iconContainer).toBeTruthy(); | ||
const text = cell.childNodes.item(1) as Text; | ||
expect(text.wholeText).toMatch('200'); | ||
}); | ||
|
||
it('renders icon and displays error message when datastream is in error state', () => { | ||
const tableItems = createTableItems( | ||
{ | ||
dataStreams: [ | ||
{ | ||
data: [], | ||
id: dataStreamId, | ||
error: { | ||
msg: 'Error', | ||
}, | ||
resolution: 0, | ||
}, | ||
], | ||
items, | ||
viewport: { duration: '1000' }, | ||
}, | ||
DefaultTableMessages | ||
); | ||
|
||
act(() => { | ||
ReactDOM.render( | ||
<Table columnDefinitions={columnDefinitions} messageOverrides={DefaultTableMessages} items={tableItems} />, | ||
container | ||
); | ||
}); | ||
|
||
const cell = container.getElementsByClassName('iot-table-cell').item(0) as HTMLDivElement; | ||
const iconContainer = cell.childNodes.item(0); | ||
expect(iconContainer).toBeTruthy(); | ||
|
||
const text = cell.childNodes.item(1) as Text; | ||
expect(text.wholeText).toMatch('Error'); | ||
}); |
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