Skip to content

Commit

Permalink
convert to formik firldArray
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinandan13jan committed Sep 21, 2023
1 parent 9af60a5 commit a63cbd8
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 402 deletions.
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,7 +61,7 @@ const IntegrationTestView: React.FunctionComponent<IntegrationTestViewProps> = (
path: path?.value ?? '',
environmentName: integrationTest?.spec?.environment?.name ?? '',
environmentType: integrationTest?.spec?.environment?.type ?? '',
params: integrationTest?.spec?.params ?? null,
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 @@ -7,6 +7,7 @@ import {
createIntegrationTest,
getLabelForParam,
getURLForParam,
formatParams,
} from '../create-utils';

jest.mock('@openshift/dynamic-plugin-sdk-utils', () => ({
Expand All @@ -24,6 +25,7 @@ const integrationTestData = {
},
spec: {
application: 'Test Application',
params: null,
environment: {
name: 'test1',
type: 'POC',
Expand Down Expand Up @@ -189,3 +191,29 @@ describe('Create Utils', () => {
);
});
});

describe('Create Utils formatParams', () => {
it('Should render null if no params or empty array []', () => {
const formattedParams = formatParams([]);
expect(formattedParams).toBeNull();
});

it('Should render 3 params ', () => {
const formattedParams = formatParams([
{ name: 'apple', values: ['val1', 'val2'] },
{ name: 'apple', values: ['val1', 'val2'] },
{ name: 'apple', values: ['val1', 'val2'] },
]);
expect(formattedParams.length).toBe(3);
});

it('Should convert Values to value if only 1 entry', () => {
const formattedParams = formatParams([
{ name: 'apple', values: ['val1'] },
{ name: 'apple', values: ['val1', 'val2'] },
{ name: 'apple', values: ['val1', 'val2'] },
]);
expect(formattedParams.length).toBe(3);
expect(formattedParams[0].value).toBe('val1');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../../../../models';
import {
IntegrationTestScenarioKind,
Param,
ResolverParam,
ResolverType,
} from '../../../../types/coreBuildService';
Expand All @@ -22,6 +23,20 @@ export enum ResolverRefParams {
REVISION = 'revision',
}

export const formatParams = (params): Param[] => {
if (!params || !Array.isArray(params) || params.length === 0) return null;
const newParams = [];
params.forEach((param) => {
if (param.values?.length === 1) {
newParams.push({ name: param.name, value: param.values[0] });
} else {
newParams.push(param);
}
});

return newParams.length > 0 ? newParams : null;
};

export const editIntegrationTest = (
integrationTest: IntegrationTestScenarioKind,
integrationTestValues: IntegrationTestFormValues,
Expand Down Expand Up @@ -55,7 +70,7 @@ export const editIntegrationTest = (
{ name: ResolverRefParams.PATH, value: path },
],
},
params,
params: formatParams(params),
contexts: [
{
description: 'Application testing',
Expand Down Expand Up @@ -122,7 +137,7 @@ export const createIntegrationTest = (
{ name: ResolverRefParams.PATH, value: path },
],
},
params,
params: formatParams(params),
contexts: [
{
description: 'Application testing',
Expand Down
Loading

0 comments on commit a63cbd8

Please sign in to comment.