Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for HelmInstallUpgradeForm
- Loading branch information
1 parent
c2ba6a2
commit fe0cb4e
Showing
5 changed files
with
143 additions
and
109 deletions.
There are no files selected for viewing
32 changes: 21 additions & 11 deletions
32
frontend/packages/console-shared/src/components/form-utils/FormHeader.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 |
---|---|---|
@@ -1,21 +1,31 @@ | ||
import * as React from 'react'; | ||
import { Title, FormHelperText } from '@patternfly/react-core'; | ||
|
||
type SpacerSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl'; | ||
|
||
type FormHeaderProps = { | ||
title: React.ReactNode; | ||
helpText?: React.ReactNode; | ||
flexLayout?: boolean; | ||
marginTop?: SpacerSize; | ||
marginBottom?: SpacerSize; | ||
}; | ||
|
||
const FormHeader: React.FC<FormHeaderProps> = ({ title, helpText, flexLayout }) => ( | ||
<div style={flexLayout ? { marginBottom: 'var(--pf-global--spacer--lg)' } : {}}> | ||
<Title headingLevel="h1" size="2xl"> | ||
{title} | ||
</Title> | ||
<FormHelperText isHidden={false} style={{ marginTop: 'var(--pf-global--spacer--xs)' }}> | ||
{helpText} | ||
</FormHelperText> | ||
</div> | ||
); | ||
const FormHeader: React.FC<FormHeaderProps> = ({ title, helpText, marginTop, marginBottom }) => { | ||
const marginStyles = { | ||
...(marginTop ? { marginTop: `var(--pf-global--spacer--${marginTop})` } : {}), | ||
...(marginBottom ? { marginBottom: `var(--pf-global--spacer--${marginBottom})` } : {}), | ||
}; | ||
|
||
return ( | ||
<div style={marginStyles}> | ||
<Title headingLevel="h1" size="2xl"> | ||
{title} | ||
</Title> | ||
<FormHelperText isHidden={false} style={{ marginTop: 'var(--pf-global--spacer--xs)' }}> | ||
{helpText} | ||
</FormHelperText> | ||
</div> | ||
); | ||
}; | ||
|
||
export default FormHeader; |
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
202 changes: 112 additions & 90 deletions
202
...d/packages/dev-console/src/components/helm/form/__tests__/HelmInstallUpgradeForm.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 |
---|---|---|
@@ -1,104 +1,126 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import { shallow } from 'enzyme'; | ||
import { InputField, SyncedEditorField } from '@console/shared'; | ||
import { InputField, SyncedEditorField, FormHeader } from '@console/shared'; | ||
import { EditorType } from '@console/shared/src/components/synced-editor/editor-toggle'; | ||
import HelmInstallUpgradeForm from '../HelmInstallUpgradeForm'; | ||
import { HelmActionType } from '../../helm-types'; | ||
import { coFetchJSON } from '@console/internal/co-fetch'; | ||
import { formikFormProps } from '@console/shared/src/test-utils/formik-props-utils'; | ||
|
||
let helmInstallUpgradeFormProps: React.ComponentProps<typeof HelmInstallUpgradeForm>; | ||
|
||
describe('HelmInstallUpgradeForm', () => { | ||
helmInstallUpgradeFormProps = { | ||
chartHasValues: true, | ||
helmAction: 'Install', | ||
onVersionChange: jest.fn(), | ||
chartMetaDescription: <p>Some chart meta</p>, | ||
values: { | ||
releaseName: 'helm-release', | ||
chartName: 'helm-release', | ||
chartVersion: '', | ||
yamlData: 'chart-yaml-values', | ||
formData: { | ||
test: 'data', | ||
}, | ||
formSchema: { | ||
type: 'object', | ||
required: ['test'], | ||
properties: { | ||
test: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
editorType: EditorType.Form, | ||
}, | ||
errors: {}, | ||
touched: {}, | ||
isValid: true, | ||
initialValues: { | ||
releaseName: 'helm-release', | ||
chartName: 'helm-release', | ||
chartVersion: '0.3', | ||
yamlData: 'chart-yaml-values', | ||
formData: { | ||
test: 'data', | ||
}, | ||
formSchema: { | ||
type: 'object', | ||
required: ['test'], | ||
properties: { | ||
test: { | ||
type: 'string', | ||
}, | ||
}, | ||
const formValues = { | ||
releaseName: 'helm-release', | ||
chartName: 'helm-release', | ||
chartVersion: '0.3', | ||
chartReadme: 'some-readme', | ||
yamlData: 'chart-yaml-values', | ||
formData: { | ||
test: 'data', | ||
}, | ||
formSchema: { | ||
type: 'object', | ||
required: ['test'], | ||
properties: { | ||
test: { | ||
type: 'string', | ||
}, | ||
editorType: EditorType.Form, | ||
}, | ||
isSubmitting: true, | ||
isValidating: true, | ||
status: {}, | ||
submitCount: 0, | ||
dirty: false, | ||
getFieldProps: jest.fn(), | ||
handleBlur: jest.fn(), | ||
handleChange: jest.fn(), | ||
handleReset: jest.fn(), | ||
handleSubmit: jest.fn(), | ||
initialErrors: {}, | ||
initialStatus: {}, | ||
initialTouched: {}, | ||
registerField: jest.fn(), | ||
resetForm: jest.fn(), | ||
setErrors: jest.fn(), | ||
setFieldError: jest.fn(), | ||
setFieldTouched: jest.fn(), | ||
setFieldValue: jest.fn(), | ||
setFormikState: jest.fn(), | ||
setStatus: jest.fn(), | ||
setSubmitting: jest.fn(), | ||
setTouched: jest.fn(), | ||
setValues: jest.fn(), | ||
submitForm: jest.fn(), | ||
unregisterField: jest.fn(), | ||
validateField: jest.fn(), | ||
validateForm: jest.fn(), | ||
getFieldMeta: jest.fn(), | ||
validateOnBlur: true, | ||
validateOnChange: true, | ||
}; | ||
}, | ||
editorType: EditorType.Form, | ||
}; | ||
|
||
const helmConfig = { | ||
type: HelmActionType.Install, | ||
title: 'Install Helm Chart', | ||
subTitle: { | ||
form: 'Mock form help text', | ||
yaml: 'Mock yaml help text', | ||
}, | ||
helmReleaseApi: `/api/helm/chart?url=mock-chart-url`, | ||
fetch: coFetchJSON.post, | ||
redirectURL: 'mock-redirect-url', | ||
}; | ||
|
||
const componentProps = { | ||
chartHasValues: true, | ||
helmActionConfig: helmConfig, | ||
onVersionChange: jest.fn(), | ||
chartMetaDescription: <p>Some chart meta</p>, | ||
}; | ||
|
||
const props: React.ComponentProps<typeof HelmInstallUpgradeForm> = { | ||
...componentProps, | ||
...formikFormProps, | ||
values: formValues, | ||
}; | ||
|
||
describe('HelmInstallUpgradeForm', () => { | ||
it('should render the SyncedEditorField component', () => { | ||
const helmInstallUpgradeForm = shallow( | ||
<HelmInstallUpgradeForm {...helmInstallUpgradeFormProps} />, | ||
); | ||
expect(helmInstallUpgradeForm.find(SyncedEditorField).exists()).toBe(true); | ||
const wrapper = shallow(<HelmInstallUpgradeForm {...props} />); | ||
expect(wrapper.find(SyncedEditorField).exists()).toBe(true); | ||
}); | ||
|
||
it('should render FormHeader with correct title for the form', () => { | ||
const wrapper = shallow(<HelmInstallUpgradeForm {...props} />); | ||
const header = wrapper.find(FormHeader); | ||
expect(header.exists()).toBe(true); | ||
expect(header.props().title).toBe(helmConfig.title); | ||
}); | ||
|
||
it('should render FormHeader with form help text', () => { | ||
const wrapper = shallow(<HelmInstallUpgradeForm {...props} />); | ||
const header = wrapper.find(FormHeader); | ||
const helpText = header.props().helpText as any; | ||
expect(header.exists()).toBe(true); | ||
expect(helpText.props.children[0].props.children).toContain(helmConfig.subTitle.form); | ||
}); | ||
|
||
it('should render FormHeader with yaml help text', () => { | ||
const newProps = _.cloneDeep(props); | ||
newProps.values.editorType = EditorType.YAML; | ||
const wrapper = shallow(<HelmInstallUpgradeForm {...newProps} />); | ||
const header = wrapper.find(FormHeader); | ||
const helpText = header.props().helpText as any; | ||
expect(header.exists()).toBe(true); | ||
expect(helpText.props.children[0].props.children).toContain(helmConfig.subTitle.yaml); | ||
}); | ||
|
||
it('should not render form helm text if there are no chart values', () => { | ||
const newProps = _.cloneDeep(props); | ||
newProps.chartHasValues = false; | ||
const wrapper = shallow(<HelmInstallUpgradeForm {...newProps} />); | ||
const header = wrapper.find(FormHeader); | ||
const helpText = header.props().helpText as any; | ||
expect(header.exists()).toBe(true); | ||
expect(helpText.props.children[0]).toBeFalsy(); | ||
expect(helpText.props.children[1]).toBeTruthy(); | ||
}); | ||
|
||
it('should not render readme button in help text if there is no readme', () => { | ||
const newProps = _.cloneDeep(props); | ||
newProps.values.chartReadme = null; | ||
const wrapper = shallow(<HelmInstallUpgradeForm {...newProps} />); | ||
const header = wrapper.find(FormHeader); | ||
const helpText = header.props().helpText as any; | ||
expect(header.exists()).toBe(true); | ||
expect(helpText.props.children[0]).toBeTruthy(); | ||
expect(helpText.props.children[1]).toBeFalsy(); | ||
}); | ||
|
||
it('should disable form editor if there is no formSchema', () => { | ||
const newProps = _.cloneDeep(props); | ||
newProps.values.formSchema = null; | ||
const wrapper = shallow(<HelmInstallUpgradeForm {...newProps} />); | ||
const editor = wrapper.find(SyncedEditorField); | ||
expect(editor.exists()).toBe(true); | ||
expect(editor.props().formContext.isDisabled).toBe(true); | ||
}); | ||
|
||
it('should have the Release Name field disabled in the Helm Upgrade Form', () => { | ||
helmInstallUpgradeFormProps.helmAction = 'Upgrade'; | ||
const helmInstallUpgradeForm = shallow( | ||
<HelmInstallUpgradeForm {...helmInstallUpgradeFormProps} />, | ||
); | ||
expect(helmInstallUpgradeForm.find(InputField).props().label).toBe('Release Name'); | ||
expect(helmInstallUpgradeForm.find(InputField).props().isDisabled).toBe(true); | ||
const newProps = _.cloneDeep(props); | ||
newProps.helmActionConfig.type = HelmActionType.Upgrade; | ||
const wrapper = shallow(<HelmInstallUpgradeForm {...newProps} />); | ||
expect(wrapper.find(InputField).props().label).toBe('Release Name'); | ||
expect(wrapper.find(InputField).props().isDisabled).toBe(true); | ||
}); | ||
}); |
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