-
Notifications
You must be signed in to change notification settings - Fork 916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor read-only component to cover more edge cases #6416
Conversation
a84b784
to
5aa4e7d
Compare
src/plugins/data_source_management/public/components/data_source_menu/types.ts
Outdated
Show resolved
Hide resolved
src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx
Outdated
Show resolved
Hide resolved
src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx
Outdated
Show resolved
Hide resolved
@@ -130,6 +142,9 @@ export async function getDataSourceById( | |||
savedObjectsClient: SavedObjectsClientContract | |||
) { | |||
return savedObjectsClient.get('data-source', id).then((response) => { | |||
if (!response || response.error) { | |||
throw new Error('Unable to find data source'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the get data source API throw exception when failed to get data source or do we need to handle it this way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not throw exception if we don't handle it. i have done test on this. So if we put an invalid id such as invalid id, it would return back something as below:
Object { client: {…}, attributes: {}, _version: undefined, id: "invalidID", type: "data-source", migrationVersion: undefined, error: {…}, references: [], updated_at: undefined }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's the case, then we don't need the try catch block, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure . would do
src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx
Outdated
Show resolved
Hide resolved
src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx
Outdated
Show resolved
Hide resolved
src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx
Outdated
Show resolved
Hide resolved
src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx
Outdated
Show resolved
Hide resolved
} | ||
|
||
try { | ||
const selectedDataSource = await getDataSourceById(optionId, this.props.savedObjectsClient!); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we only do this when label is not passed in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to use this DatasourceById to find if it is an invalid ID. If the ID is invalid, getDataSourceByID would fail and throw exception. This would be caught and return as a warning message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to check each time, when label is passed in, we will accept it, if it is not passed it, we will look for it. This component is used internally by developers, no need for over defensive programming
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it.
src/plugins/data_source_management/public/components/data_source_view/data_source_view.test.tsx
Outdated
Show resolved
Hide resolved
src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx
Outdated
Show resolved
Hide resolved
}); | ||
|
||
test('should add warning with error message when error is provided', () => { | ||
const error = 'Failed to fetch data'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const error = 'Failed to fetch data'; | |
const errorMessage= 'Failed to fetch data'; |
or const error = new Error("Filed to fetch data")
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it
src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx
Outdated
Show resolved
Hide resolved
Can we add the screenshots? Also test failed, could we address https://github.com/opensearch-project/OpenSearch-Dashboards/actions/runs/8667700544/job/23771246546?pr=6416 |
83368ff
to
57979c1
Compare
@@ -44,9 +44,11 @@ export const DataSourceComponentType = { | |||
export type DataSourceComponentType = typeof DataSourceComponentType[keyof typeof DataSourceComponentType]; | |||
|
|||
export interface DataSourceViewConfig extends DataSourceBaseConfig { | |||
onSelectedDataSources?: (dataSources: DataSourceOption[]) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move optional fields below required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
|
||
interface DataSourceViewProps { | ||
fullWidth: boolean; | ||
selectedOption: DataSourceOption[]; | ||
savedObjectsClient?: SavedObjectsClientContract; | ||
notifications?: ToastsStart; | ||
uiSettings?: IUiSettingsClient; | ||
hideLocalCluster: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move optional fields below required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay
} | ||
|
||
try { | ||
const selectedDataSource = await getDataSourceById(optionId, this.props.savedObjectsClient!); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to check each time, when label is passed in, we will accept it, if it is not passed it, we will look for it. This component is used internally by developers, no need for over defensive programming
@@ -130,6 +142,9 @@ export async function getDataSourceById( | |||
savedObjectsClient: SavedObjectsClientContract | |||
) { | |||
return savedObjectsClient.get('data-source', id).then((response) => { | |||
if (!response || response.error) { | |||
throw new Error('Unable to find data source'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's the case, then we don't need the try catch block, right?
|
||
export function handleNoAvailableDataSourceError(notifications: ToastsStart) { | ||
notifications.addWarning( | ||
i18n.translate('dataSource.fetchDataSourceError', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i18n.translate('dataSource.fetchDataSourceError', { | |
i18n.translate('dataSource.noAvailableDataSourceError', { |
fcf50dd
to
688df3c
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #6416 +/- ##
===========================================
- Coverage 49.55% 32.72% -16.84%
===========================================
Files 2670 2252 -418
Lines 54290 45574 -8716
Branches 8878 7150 -1728
===========================================
- Hits 26906 14913 -11993
- Misses 25720 29962 +4242
+ Partials 1664 699 -965
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
5a698ce
to
ae48c41
Compare
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
ae48c41
to
b33d06f
Compare
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
540c353
to
3a3257c
Compare
* Refactor read-only component to cover more edge case Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> * fix test Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> * fix snapshot Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> --------- Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> (cherry picked from commit b9acf57) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md
* Refactor read-only component to cover more edge case Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> * fix test Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> * fix snapshot Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> --------- Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> (cherry picked from commit b9acf57) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md
* Refactor read-only component to cover more edge case Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> * fix test Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> * fix snapshot Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> --------- Signed-off-by: Yuanqi(Ella) Zhu <[email protected]> (cherry picked from commit b9acf57) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Description
Refactor read-only component to cover more edge cases
Issues Resolved
#6393
Screenshot
Testing the changes
Toast: Data source is not available
Toast: Failed to fetch data source
Show local cluster
show selected cluster
show selected cluster with label name
activeOption: [{ id: '6304d1d0-f826-11ee-ab60-439c91fde80b', label:'show label' }],
dataSourceFilter: (ds) => {return ds.id != '6304d1d0-f826-11ee-ab60-439c91fde80b'},
toast: datasource is not available
Check List
yarn test:jest
yarn test:jest_integration