-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82d2726
commit 9e4d258
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...ui/src/screens/Inventory/shared/InventorySourceSubForms/OpenShiftVirtualizationSubForm.js
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,60 @@ | ||
import React, { useCallback } from 'react'; | ||
import { useField, useFormikContext } from 'formik'; | ||
import { t } from '@lingui/macro'; | ||
import { useConfig } from 'contexts/Config'; | ||
import getDocsBaseUrl from 'util/getDocsBaseUrl'; | ||
import CredentialLookup from 'components/Lookup/CredentialLookup'; | ||
import { required } from 'util/validators'; | ||
import { | ||
OptionsField, | ||
VerbosityField, | ||
EnabledVarField, | ||
EnabledValueField, | ||
HostFilterField, | ||
SourceVarsField, | ||
} from './SharedFields'; | ||
import getHelpText from '../Inventory.helptext'; | ||
|
||
const VirtualizationSubForm = ({ autoPopulateCredential }) => { | ||
const helpText = getHelpText(); | ||
const { setFieldValue, setFieldTouched } = useFormikContext(); | ||
const [credentialField, credentialMeta, credentialHelpers] = | ||
useField('credential'); | ||
const config = useConfig(); | ||
|
||
const handleCredentialUpdate = useCallback( | ||
(value) => { | ||
setFieldValue('credential', value); | ||
setFieldTouched('credential', true, false); | ||
}, | ||
[setFieldValue, setFieldTouched] | ||
); | ||
|
||
const docsBaseUrl = getDocsBaseUrl(config); | ||
return ( | ||
<> | ||
<CredentialLookup | ||
credentialTypeNamespace="openshift_virtualization" | ||
label={t`Credential`} | ||
helperTextInvalid={credentialMeta.error} | ||
isValid={!credentialMeta.touched || !credentialMeta.error} | ||
onBlur={() => credentialHelpers.setTouched()} | ||
onChange={handleCredentialUpdate} | ||
value={credentialField.value} | ||
required | ||
autoPopulate={autoPopulateCredential} | ||
validate={required(t`Select a value for this field`)} | ||
/> | ||
<VerbosityField /> | ||
<HostFilterField /> | ||
<EnabledVarField /> | ||
<EnabledValueField /> | ||
<OptionsField /> | ||
<SourceVarsField | ||
popoverContent={helpText.sourceVars(docsBaseUrl, 'openshift_virtualization')} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default OpenShiftVirtualizationSubForm; |
65 changes: 65 additions & 0 deletions
65
...c/screens/Inventory/shared/InventorySourceSubForms/OpenShiftVirtualizationSubForm.test.js
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,65 @@ | ||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { Formik } from 'formik'; | ||
import { CredentialsAPI } from 'api'; | ||
import { mountWithContexts } from '../../../../../testUtils/enzymeHelpers'; | ||
import VirtualizationSubForm from './VirtualizationSubForm'; | ||
|
||
jest.mock('../../../../api'); | ||
|
||
const initialValues = { | ||
credential: null, | ||
overwrite: false, | ||
overwrite_vars: false, | ||
source_path: '', | ||
source_project: null, | ||
source_script: null, | ||
source_vars: '---\n', | ||
update_cache_timeout: 0, | ||
update_on_launch: true, | ||
verbosity: 1, | ||
}; | ||
|
||
describe('<VirtualizationSubForm />', () => { | ||
let wrapper; | ||
|
||
beforeEach(async () => { | ||
CredentialsAPI.read.mockResolvedValue({ | ||
data: { count: 0, results: [] }, | ||
}); | ||
|
||
await act(async () => { | ||
wrapper = mountWithContexts( | ||
<Formik initialValues={initialValues}> | ||
<VirtualizationSubForm /> | ||
</Formik> | ||
); | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should render subform fields', () => { | ||
expect(wrapper.find('FormGroup[label="Credential"]')).toHaveLength(1); | ||
expect(wrapper.find('FormGroup[label="Verbosity"]')).toHaveLength(1); | ||
expect(wrapper.find('FormGroup[label="Update options"]')).toHaveLength(1); | ||
expect( | ||
wrapper.find('FormGroup[label="Cache timeout (seconds)"]') | ||
).toHaveLength(1); | ||
expect( | ||
wrapper.find('VariablesField[label="Source variables"]') | ||
).toHaveLength(1); | ||
}); | ||
|
||
test('should make expected api calls', () => { | ||
expect(CredentialsAPI.read).toHaveBeenCalledTimes(1); | ||
expect(CredentialsAPI.read).toHaveBeenCalledWith({ | ||
credential_type__namespace: 'rhv', | ||
order_by: 'name', | ||
page: 1, | ||
page_size: 5, | ||
}); | ||
}); | ||
}); |