-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5061 from karthikjeeyar/container-source-form
Add container source form in event source add flow
- Loading branch information
Showing
7 changed files
with
231 additions
and
2 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
89 changes: 89 additions & 0 deletions
89
frontend/packages/knative-plugin/src/components/add/event-sources/ContainerSourceSection.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,89 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import { useFormikContext, FormikValues } from 'formik'; | ||
import { TextInputTypes, FormGroup } from '@patternfly/react-core'; | ||
import { InputField, MultiColumnField } from '@console/shared'; | ||
import { AsyncComponent } from '@console/internal/components/utils'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
import { getSuggestedName } from '@console/dev-console/src/utils/imagestream-utils'; | ||
|
||
const containerPaths = { | ||
Image: 'data.containersource.containers[0].image', | ||
Name: 'data.containersource.containers[0].name', | ||
Env: 'data.containersource.containers[0].env', | ||
Args: 'data.containersource.containers[0].args', | ||
}; | ||
|
||
const ContainerSourceSection: React.FC = () => { | ||
const { values, setFieldValue } = useFormikContext<FormikValues>(); | ||
const { | ||
data: { | ||
containersource: { | ||
containers: [{ env: envs, args }], | ||
}, | ||
}, | ||
} = values; | ||
const initialEnvValues = !_.isEmpty(envs) ? _.map(envs, (env) => _.values(env)) : [['', '']]; | ||
const [nameValue, setNameValue] = React.useState(initialEnvValues); | ||
const handleNameValuePairs = React.useCallback( | ||
({ nameValuePairs }) => { | ||
const updatedNameValuePairs = _.compact( | ||
nameValuePairs.map(([name, value]) => (value.length ? { name, value } : null)), | ||
); | ||
setNameValue(nameValuePairs); | ||
setFieldValue(containerPaths.Env, updatedNameValuePairs); | ||
}, | ||
[setFieldValue], | ||
); | ||
return ( | ||
<FormSection title="ContainerSource"> | ||
<h3 className="co-section-heading-tertiary">Container</h3> | ||
<InputField | ||
data-test-id="container-image-field" | ||
type={TextInputTypes.text} | ||
name={containerPaths.Image} | ||
label="Image" | ||
required | ||
onChange={(e) => { | ||
setFieldValue(containerPaths.Name, getSuggestedName(e.target.value)); | ||
}} | ||
/> | ||
<InputField | ||
data-test-id="container-name-field" | ||
type={TextInputTypes.text} | ||
name={containerPaths.Name} | ||
label="Name" | ||
/> | ||
<MultiColumnField | ||
data-test-id="container-arg-field" | ||
name={containerPaths.Args} | ||
addLabel="Add args" | ||
label="Arguments" | ||
headers={[]} | ||
emptyValues={{ name: '' }} | ||
disableDeleteRow={args?.length === 1} | ||
emptyMessage="No args are associated with the container." | ||
> | ||
<InputField name="name" type={TextInputTypes.text} placeholder="args" /> | ||
</MultiColumnField> | ||
<FormGroup fieldId="containersource-env" label="Environment variables"> | ||
<AsyncComponent | ||
loader={() => | ||
import('@console/internal/components/utils/name-value-editor').then( | ||
(c) => c.NameValueEditor, | ||
) | ||
} | ||
data-test-id="container-env-field" | ||
nameValuePairs={nameValue} | ||
valueString="Value" | ||
nameString="Name" | ||
readOnly={false} | ||
allowSorting={false} | ||
updateParentData={handleNameValuePairs} | ||
/> | ||
</FormGroup> | ||
</FormSection> | ||
); | ||
}; | ||
|
||
export default ContainerSourceSection; |
60 changes: 60 additions & 0 deletions
60
...knative-plugin/src/components/add/event-sources/__tests__/ContainerSourceSection.spec.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,60 @@ | ||
import * as React from 'react'; | ||
import { shallow, ShallowWrapper } from 'enzyme'; | ||
import { MultiColumnField } from '@console/shared'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
import { AsyncComponent } from '@console/internal/components/utils/async'; | ||
import ContainerSourceSection from '../ContainerSourceSection'; | ||
import { EventSources } from '../../import-types'; | ||
|
||
type ContainerSourceSectionProps = React.ComponentProps<typeof ContainerSourceSection>; | ||
|
||
jest.mock('formik', () => ({ | ||
useField: jest.fn(() => [{}, {}]), | ||
useFormikContext: jest.fn(() => ({ | ||
setFieldValue: jest.fn(), | ||
setFieldTouched: jest.fn(), | ||
validateForm: jest.fn(), | ||
values: { | ||
type: 'ContainerSource', | ||
data: { | ||
containersource: { | ||
containers: [ | ||
{ | ||
args: [], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
})), | ||
})); | ||
describe('ContainerSourceSection', () => { | ||
let wrapper: ShallowWrapper<ContainerSourceSectionProps>; | ||
beforeEach(() => { | ||
wrapper = shallow(<ContainerSourceSection />); | ||
}); | ||
|
||
it('should render ContainerSource FormSection', () => { | ||
expect(wrapper.find(FormSection)).toHaveLength(1); | ||
expect(wrapper.find(FormSection).props().title).toBe(EventSources.ContainerSource); | ||
}); | ||
|
||
it('should render Container image and name input fields', () => { | ||
const imageInputField = wrapper.find('[data-test-id="container-image-field"]'); | ||
const nameInputField = wrapper.find('[data-test-id="container-name-field"]'); | ||
expect(imageInputField).toHaveLength(1); | ||
expect(nameInputField).toHaveLength(1); | ||
}); | ||
|
||
it('should render Container args field', () => { | ||
const argsField = wrapper.find(MultiColumnField); | ||
expect(argsField).toHaveLength(1); | ||
}); | ||
|
||
it('should render environment variables section', () => { | ||
const nameValueEditorField = wrapper.find(AsyncComponent); | ||
expect(nameValueEditorField).toHaveLength(1); | ||
expect(nameValueEditorField.props().nameString).toBe('Name'); | ||
expect(nameValueEditorField.props().valueString).toBe('Value'); | ||
}); | ||
}); |
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