Skip to content

Commit

Permalink
Added unit tests for HelmInstallUpgradeForm
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitkrai03 committed Jul 2, 2020
1 parent c2ba6a2 commit fe0cb4e
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 109 deletions.
@@ -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;
Expand Up @@ -7,7 +7,7 @@ import {
getChartURL,
getChartVersions,
flattenReleaseResources,
getHelmChartReadme,
getChartReadme,
} from '../helm-utils';
import { HelmReleaseStatus } from '../helm-types';
import {
Expand Down Expand Up @@ -121,6 +121,6 @@ describe('Helm Releases Utils', () => {
});

it('should return the readme for the chart provided', () => {
expect(getHelmChartReadme(mockHelmReleases[0].chart)).toEqual('example readme content');
expect(getChartReadme(mockHelmReleases[0].chart)).toEqual('example readme content');
});
});
Expand Up @@ -80,14 +80,14 @@ const HelmInstallUpgradeForm: React.FC<FormikProps<FormikValues> & HelmInstallUp

const formHelpText = (
<>
{formSubTitle}
{chartHasValues && <>{formSubTitle} &nbsp;</>}
{readmeText}
</>
);

return (
<FlexForm onSubmit={handleSubmit}>
<FormHeader title={title} helpText={formHelpText} flexLayout />
<FormHeader title={title} helpText={formHelpText} marginBottom="lg" />
<FormSection fullWidth>
<Grid gutter={'md'}>
<GridItem xl={6} lg={6} md={12} sm={12}>
Expand Down
@@ -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);
});
});
10 changes: 6 additions & 4 deletions frontend/packages/dev-console/src/components/helm/helm-utils.ts
Expand Up @@ -137,9 +137,9 @@ export const getHelmActionConfig = (
title: 'Install Helm Chart',
subTitle: {
form:
'The Helm chart can be installed by completing the form. Default values may be provided by the Helm chart authors. ',
'The Helm chart can be installed by completing the form. Default values may be provided by the Helm chart authors.',
yaml:
'The Helm chart can be installed by manually entering YAML or JSON definitions, or by dragging and dropping a file into the editor. ',
'The Helm chart can be installed by manually entering YAML or JSON definitions, or by dragging and dropping a file into the editor.',
},
helmReleaseApi: `/api/helm/chart?url=${chartURL}`,
fetch: coFetchJSON.post,
Expand All @@ -149,8 +149,10 @@ export const getHelmActionConfig = (
return {
type: HelmActionType.Upgrade,
title: 'Upgrade Helm Release',
subTitle:
'Upgrade by selecting a new chart version or manually changing the YAML shown in the editor below. ',
subTitle: {
form: 'Upgrade by selecting a new chart version or manually changing the form values.',
yaml: 'Upgrade by selecting a new chart version or manually changing YAML.',
},
helmReleaseApi: `/api/helm/release?ns=${namespace}&name=${releaseName}`,
fetch: coFetchJSON.put,
redirectURL: getOriginRedirectURL(actionOrigin, namespace, releaseName),
Expand Down

0 comments on commit fe0cb4e

Please sign in to comment.