-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(D3 plugin): add basic data validation (#366)
* feat(D3 plugin): add basic data validation * fix: review fixes
- Loading branch information
Showing
10 changed files
with
362 additions
and
35 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
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
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
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,47 @@ | ||
import {ChartKitWidgetData} from '../../../../../types'; | ||
|
||
export const XY_SERIES: Record<string, ChartKitWidgetData> = { | ||
INVALID_CATEGORY_X: { | ||
series: { | ||
data: [{type: 'scatter', data: [{x: undefined, y: 1}], name: 'Series'}], | ||
}, | ||
xAxis: {type: 'category'}, | ||
}, | ||
INVALID_CATEGORY_Y: { | ||
series: { | ||
data: [{type: 'scatter', data: [{x: 1, y: undefined}], name: 'Series'}], | ||
}, | ||
yAxis: [{type: 'category'}], | ||
}, | ||
INVALID_DATETIME_X: { | ||
series: { | ||
data: [{type: 'scatter', data: [{x: undefined, y: 1}], name: 'Series'}], | ||
}, | ||
xAxis: {type: 'datetime'}, | ||
}, | ||
INVALID_DATETIME_Y: { | ||
series: { | ||
data: [{type: 'scatter', data: [{x: undefined, y: 1}], name: 'Series'}], | ||
}, | ||
yAxis: [{type: 'datetime'}], | ||
}, | ||
INVALID_LINEAR_X: { | ||
series: { | ||
data: [{type: 'scatter', data: [{x: 'str', y: 1}], name: 'Series'}], | ||
}, | ||
}, | ||
INVALID_LINEAR_Y: { | ||
series: { | ||
data: [{type: 'scatter', data: [{x: 1, y: 'str'}], name: 'Series'}], | ||
}, | ||
}, | ||
}; | ||
|
||
export const PIE_SERIES: Record<string, ChartKitWidgetData> = { | ||
INVALID_VALUE: { | ||
series: { | ||
// @ts-expect-error | ||
data: [{type: 'pie', data: [{value: undefined, name: 'Series'}]}], | ||
}, | ||
}, | ||
}; |
73 changes: 73 additions & 0 deletions
73
src/plugins/d3/renderer/validation/__tests__/validation.test.ts
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,73 @@ | ||
import {ChartKitError, CHARTKIT_ERROR_CODE} from '../../../../../libs'; | ||
import {ChartKitWidgetData} from '../../../../../types'; | ||
import {validateData} from '../'; | ||
import {PIE_SERIES, XY_SERIES} from '../__mocks__'; | ||
|
||
describe('plugins/d3/validation', () => { | ||
test.each<any>([undefined, null, {}, {series: {}}, {series: {data: []}}])( | ||
'validateData should throw an error in case of empty data (data: %j)', | ||
(data) => { | ||
let error: ChartKitError | null = null; | ||
|
||
try { | ||
validateData(data); | ||
} catch (e) { | ||
error = e as ChartKitError; | ||
} | ||
|
||
expect(error?.code).toEqual(CHARTKIT_ERROR_CODE.NO_DATA); | ||
}, | ||
); | ||
|
||
test.each<any>([ | ||
{series: {data: [{data: [{x: 1, y: 1}]}]}}, | ||
{series: {data: [{type: 'invalid-type', data: [{x: 1, y: 1}]}]}}, | ||
])('validateData should throw an error in case of incorrect series type (data: %j)', (data) => { | ||
let error: ChartKitError | null = null; | ||
|
||
try { | ||
validateData(data); | ||
} catch (e) { | ||
error = e as ChartKitError; | ||
} | ||
|
||
expect(error?.code).toEqual(CHARTKIT_ERROR_CODE.INVALID_DATA); | ||
}); | ||
|
||
test.each<ChartKitWidgetData>([ | ||
XY_SERIES.INVALID_CATEGORY_X, | ||
XY_SERIES.INVALID_CATEGORY_Y, | ||
XY_SERIES.INVALID_DATETIME_X, | ||
XY_SERIES.INVALID_DATETIME_Y, | ||
XY_SERIES.INVALID_LINEAR_X, | ||
XY_SERIES.INVALID_LINEAR_Y, | ||
])( | ||
'[XY Series] validateData should throw an error in case of invalid data (data: %j)', | ||
(data) => { | ||
let error: ChartKitError | null = null; | ||
|
||
try { | ||
validateData(data); | ||
} catch (e) { | ||
error = e as ChartKitError; | ||
} | ||
|
||
expect(error?.code).toEqual(CHARTKIT_ERROR_CODE.INVALID_DATA); | ||
}, | ||
); | ||
|
||
test.each<ChartKitWidgetData>([PIE_SERIES.INVALID_VALUE])( | ||
'[Pie Series] validateData should throw an error in case of invalid data (data: %j)', | ||
(data) => { | ||
let error: ChartKitError | null = null; | ||
|
||
try { | ||
validateData(data); | ||
} catch (e) { | ||
error = e as ChartKitError; | ||
} | ||
|
||
expect(error?.code).toEqual(CHARTKIT_ERROR_CODE.INVALID_DATA); | ||
}, | ||
); | ||
}); |
Oops, something went wrong.