Skip to content

Commit

Permalink
Merge pull request #799 from abhinandan13jan/its-param-support
Browse files Browse the repository at this point in the history
feat(Integrationtest): integration test param support
  • Loading branch information
openshift-ci[bot] authored Oct 4, 2023
2 parents e77e123 + c28d391 commit 256ea5d
Show file tree
Hide file tree
Showing 11 changed files with 752 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ export const sampleIntegrationTestScenarios: IntegrationTestScenarioKind[] = [
params: [
{
name: 'test-param',
value: ['test'],
value: 'test',
},
],
resolverRef: null,
Expand Down Expand Up @@ -786,7 +786,7 @@ export const sampleIntegrationTestScenarios: IntegrationTestScenarioKind[] = [
params: [
{
name: 'test-param',
value: ['test'],
value: 'test',
},
],
resolverRef: null,
Expand Down
258 changes: 258 additions & 0 deletions src/components/IntegrationTest/FormikParamsField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
import * as React from 'react';
import {
Flex,
FlexItem,
TextContent,
DataList,
DataListItem,
DataListToggle,
DataListContent,
Button,
Alert,
AlertVariant,
InputGroup,
FormGroup,
DataListItemRow,
DataListItemCells,
DataListCell,
CardHeader,
Card,
CardExpandableContent,
CardBody,
} from '@patternfly/react-core';
import { MinusCircleIcon } from '@patternfly/react-icons/dist/js/icons/minus-circle-icon';
import { PlusCircleIcon } from '@patternfly/react-icons/dist/js/icons/plus-circle-icon';
import { FieldArray, useField } from 'formik';
import { InputField } from '../../shared';
import { Param } from '../../types/coreBuildService';

interface IntegrationTestParamsProps {
heading?: string;
fieldName: string;
initExpanded?: boolean;
}

const IntegrationTestParams: React.FC<IntegrationTestParamsProps> = ({
heading,
fieldName,
initExpanded = false,
}) => {
const [, { value: parameters, error }] = useField<Param[]>(fieldName);

const initExpandedState = React.useMemo(() => {
const state = [];
for (let i = 0; i < parameters?.length; i++) {
state.push(false);
}
return state;
}, [parameters]);

const [expanded, setExpanded] = React.useState<boolean[]>(initExpandedState);
const [paramExpanded, setParamExpanded] = React.useState<boolean>(initExpanded);

const toggleExpandedState = (i) => {
const state = [...expanded];
state[i] = !state[i];
setExpanded(state);
};

const addParam = () => {
const state = [...expanded, true];
setExpanded(state);
};

const removeParam = (i) => {
const state = [...expanded];
state.splice(i, 1);
setExpanded(state);
};

return (
<div style={{ maxWidth: '750px' }}>
<Card
isSelected={paramExpanded}
isExpanded={paramExpanded}
data-test="its-param-field"
isCompact
isPlain
>
<CardHeader
onExpand={() => setParamExpanded((v) => !v)}
toggleButtonProps={{
id: `toggle-${name}`,
'aria-label': name,
'aria-labelledby': `review-${name} toggle-${name}`,
'aria-expanded': paramExpanded,
'data-test': `${name}-toggle-button`,
className: 'pf-v5-u-pr-xs pf-v5-u-pl-sm',
}}
className="pf-v5-u-pl-xs pf-v5-u-pr-xs"
>
{heading ?? 'Parameters'}
{error && (
<Alert data-test="its-param-error-alert" title={error} variant={AlertVariant.danger} />
)}
</CardHeader>
<CardExpandableContent>
<CardBody className="review-component-card__card-body">
<FieldArray
name={fieldName}
render={(paramArrayHelpers) => (
<>
<DataList aria-label="parameter-list" data-test="its-param-list">
{Array.isArray(parameters) &&
parameters.length > 0 &&
parameters.map((p, i) => {
return (
<DataListItem
key={`parameter-${i}`}
isExpanded={expanded[i]}
data-test={`its-param-${i + 1}`}
>
<DataListItemRow className="pf-v5-u-pl-0">
<DataListToggle
id={p.name}
data-testid={`${p.name}-toggle`}
onClick={() => toggleExpandedState(i)}
isExpanded={expanded[i]}
data-test={`expand-param-${i + 1}`}
className="pf-v5-u-mr-0"
/>
<DataListItemCells
dataListCells={[
<DataListCell key="param-title" width={5}>
<TextContent>{`Parameter${i + 1}`}</TextContent>
</DataListCell>,

<DataListCell key="remove-param-button" width={3}>
<Button
isInline
type="button"
variant="link"
data-test={`remove-param-${i + 1}`}
icon={<MinusCircleIcon />}
onClick={() => {
removeParam(i);
paramArrayHelpers.remove(i);
}}
>
Remove parameter
</Button>
</DataListCell>,
]}
/>
</DataListItemRow>
<DataListContent
hasNoPadding
aria-label="list-item-content"
isHidden={!expanded[i]}
>
<DataListItemRow className="pf-v5-u-pl-0">
<DataListItemCells
dataListCells={[
<DataListCell
key="param-name"
width={3}
className="pf-v5-u-pl-xl pf-v5-u-pt-0"
>
<FormGroup label="Name">
<InputField
name={`${fieldName}[${i}].name`}
data-test={`param-${i}-name`}
/>
</FormGroup>
</DataListCell>,
<DataListCell
key="param-value"
width={4}
className="pf-v5-u-pl-xl pf-v5-u-pt-0"
>
<FormGroup label="Value">
<FieldArray
name={`${fieldName}[${i}].values`}
render={(valueArrayHelpers) => (
<>
{p.values &&
p.values.length > 0 &&
p.values.map((val, j) => (
<InputGroup
key={`value${i}${j}`}
className="pf-v5-u-mb-md"
>
<InputField
key={`value${i}${j}`}
name={`${fieldName}[${i}].values[${j}]`}
data-test={`param-${i}-value=${j}`}
/>
<Button
className="pf-v5-u-ml-md"
isInline
type="button"
variant="link"
isDisabled={p.values.length === 1}
data-test={`remove-value-${i + 1}-${j + 1}`}
icon={<MinusCircleIcon />}
onClick={() => valueArrayHelpers.remove(j)}
/>
</InputGroup>
))}
<Button
isInline
type="button"
variant="link"
data-test={`add-value-${i + 1}`}
icon={<PlusCircleIcon />}
onClick={() => valueArrayHelpers.push('')}
>
Add value
</Button>
</>
)}
/>
</FormGroup>
</DataListCell>,
]}
/>
</DataListItemRow>
</DataListContent>
</DataListItem>
);
})}
<DataListItem>
<Flex>
<FlexItem className="pf-v5-u-mt-md pf-v5-u-mb-md">
<Button
isInline
type="button"
variant="link"
data-test="add-param-button"
className="pf-v5-u-ml-md"
icon={<PlusCircleIcon />}
onClick={() => {
addParam();
paramArrayHelpers.push({
name: `param${parameters.length + 1}`,
values: [''],
});
}}
>
Add parameter
</Button>
</FlexItem>
<Flex flex={{ default: 'flex_3' }}>
<FlexItem />
</Flex>
</Flex>
</DataListItem>
</DataList>
</>
)}
/>
</CardBody>
</CardExpandableContent>
</Card>
</div>
);
};

export default IntegrationTestParams;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { EnvironmentType, getEnvironmentType } from '../../Environment/environme
import { AccessHelpText } from '../../ImportForm/SourceSection/SourceSection';
import { useAccessCheck } from '../../ImportForm/utils/auth-utils';
import { gitUrlRegex, RESOURCE_NAME_REGEX_MSG } from '../../ImportForm/utils/validation-utils';
import FormikParamsField from '../FormikParamsField';
import { ENVIRONMENTS, IntegrationTestFormValues } from './types';
import './IntegrationTestSection.scss';

Expand All @@ -31,9 +32,6 @@ const IntegrationTestSection: React.FC<Props> = ({ isInPage, edit }) => {
type: 'dropdown',
});
const [environments, environmentsLoaded] = useAllEnvironments();
// const [currentEnvironment, setCurrentEnvironment] = React.useState<string>(
// ITSEnvName ?? ENVIRONMENTS.DEFAULT,
// );

const dropdownItems = React.useMemo(() => {
const items = [{ key: 'none', value: 'No environment' }];
Expand Down Expand Up @@ -195,6 +193,8 @@ const IntegrationTestSection: React.FC<Props> = ({ isInPage, edit }) => {
isDisabled={edit}
className="integration-test-section__dropdown"
/>
<FormikParamsField fieldName="integrationTest.params" />

<CheckboxField
name="integrationTest.optional"
aria-label="Mark as optional for release"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ const IntegrationTestView: React.FunctionComponent<IntegrationTestViewProps> = (
(param) => param.name === ResolverRefParams.PATH,
);

const getFormParamValues = (params) => {
if (!params || !Array.isArray(params) || params?.length === 0) {
return [];
}
const formParams = [];
params.forEach((param) => {
if (param.value) {
formParams.push({ name: param.name, values: [param.value] });
} else {
formParams.push(param);
}
});
return formParams;
};

const initialValues = {
integrationTest: {
name: integrationTest?.metadata.name ?? '',
Expand All @@ -46,6 +61,7 @@ const IntegrationTestView: React.FunctionComponent<IntegrationTestViewProps> = (
path: path?.value ?? '',
environmentName: integrationTest?.spec?.environment?.name ?? '',
environmentType: integrationTest?.spec?.environment?.type ?? '',
params: getFormParamValues(integrationTest?.spec?.params),
optional:
integrationTest?.metadata.labels?.[IntegrationTestLabels.OPTIONAL] === 'true' ?? false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('IntegrationTestSection', () => {
expect(wrapper.getByTestId('integration-test-section-header')).toBeTruthy();
});
});

it('should hide the page header when isInPage is set', async () => {
const wrapper = formikRenderer(<IntegrationTestSection isInPage />, {
source: 'test-source',
Expand All @@ -66,6 +67,15 @@ describe('IntegrationTestSection', () => {
await waitFor(() => expect(found).toEqual(false));
});

it('should render parameter section', async () => {
formikRenderer(<IntegrationTestSection isInPage />, {
source: 'test-source',
secret: null,
});

screen.queryByTestId('its-param-field');
});

it('should return show error message if the repo is not accessible', async () => {
useK8sWatchResourceMock.mockReturnValue([[], true]);
useAccessCheckMock.mockReturnValue([
Expand Down
2 changes: 2 additions & 0 deletions src/components/IntegrationTest/IntegrationTestForm/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { K8sResourceCommon } from '@openshift/dynamic-plugin-sdk-utils';
import { ImportFormValues } from '../../../components/ImportForm/utils/types';
import { Param } from '../../../types/coreBuildService';

export type IntegrationTestFormValues = {
name: string;
Expand All @@ -10,6 +11,7 @@ export type IntegrationTestFormValues = {
secret?: string;
environmentName?: string;
environmentType?: string;
params?: Param[];
};

export enum IntegrationTestAnnotations {
Expand Down
Loading

0 comments on commit 256ea5d

Please sign in to comment.