-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DatasourceEditor and DatasourcePayloadEditor (#73)
* editors with hooks move to packages * lint issues fix * Update CHANGELOG.md * update version --------- Co-authored-by: Mikhail Volkov <[email protected]>
- Loading branch information
1 parent
6576094
commit 60f975e
Showing
21 changed files
with
950 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
56 changes: 56 additions & 0 deletions
56
packages/components/src/components/DatasourceEditor/DatasourceEditor.test.tsx
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,56 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { getJestSelectors } from '@volkovlabs/jest-selectors'; | ||
import React from 'react'; | ||
|
||
import { TEST_IDS } from '../../constants'; | ||
import { useDatasources } from '../../hooks'; | ||
|
||
import { DatasourceEditor } from './DatasourceEditor'; | ||
|
||
/** | ||
* Mock hooks | ||
*/ | ||
jest.mock('../../hooks', () => ({ | ||
useDatasources: jest.fn(() => []), | ||
})); | ||
|
||
describe('Select Datasource Editor', () => { | ||
const onChange = jest.fn(); | ||
|
||
/** | ||
* Selectors | ||
*/ | ||
const getSelectors = getJestSelectors(TEST_IDS.datasourceEditor); | ||
const selectors = getSelectors(screen); | ||
|
||
/** | ||
* Get Tested Component | ||
* @param value | ||
* @param context | ||
* @param restProps | ||
*/ | ||
const getComponent = ({ value = null, context = {}, ...restProps }: any) => { | ||
return <DatasourceEditor onChange={onChange} {...restProps} value={value} context={context} />; | ||
}; | ||
|
||
it('Should update value', () => { | ||
jest.mocked(useDatasources).mockImplementationOnce( | ||
() => | ||
[ | ||
{ | ||
name: '123', | ||
uid: 'ds1', | ||
}, | ||
{ | ||
name: 'abc', | ||
uid: 'ds2', | ||
}, | ||
] as any | ||
); | ||
render(getComponent({})); | ||
|
||
fireEvent.change(selectors.fieldSelect(), { target: { value: 'ds1' } }); | ||
|
||
expect(onChange).toHaveBeenCalledWith('ds1'); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/components/src/components/DatasourceEditor/DatasourceEditor.tsx
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,45 @@ | ||
import { Select } from '@grafana/ui'; | ||
import React, { useMemo } from 'react'; | ||
|
||
import { TEST_IDS } from '../../constants'; | ||
import { useDatasources } from '../../hooks'; | ||
import { EditorProps } from '../../types'; | ||
|
||
/** | ||
* Properties | ||
*/ | ||
interface Props extends EditorProps<string> {} | ||
|
||
/** | ||
* Data Source Editor | ||
*/ | ||
export const DatasourceEditor: React.FC<Props> = ({ value, onChange }) => { | ||
/** | ||
* Data Sources | ||
*/ | ||
const datasources = useDatasources(); | ||
|
||
/** | ||
* Options | ||
*/ | ||
const datasourceOptions = useMemo(() => { | ||
return datasources.map((datasource) => ({ | ||
label: datasource.name, | ||
value: datasource.uid, | ||
})); | ||
}, [datasources]); | ||
|
||
/** | ||
* Return | ||
*/ | ||
return ( | ||
<Select | ||
onChange={(item) => { | ||
onChange(item.value!); | ||
}} | ||
options={datasourceOptions} | ||
value={value} | ||
{...TEST_IDS.datasourceEditor.fieldSelect.apply()} | ||
/> | ||
); | ||
}; |
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 @@ | ||
export * from './DatasourceEditor'; |
Oops, something went wrong.