All files / modules/75-cd/components/PipelineSteps/PolicyStep PolicyStepWidget.tsx

100% Statements 16/16
100% Branches 0/0
100% Functions 3/3
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91              97x 97x   97x   97x 97x   97x   97x 97x     97x 97x                               4x   4x                                     4x                             7x 7x                           97x  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import * as Yup from 'yup'
import type { FormikProps } from 'formik'
import { defaultTo } from 'lodash-es'
 
import { Formik, MultiTypeInputType } from '@harness/uicore'
import { useStrings } from 'framework/strings'
 
import { getDurationValidationSchema } from '@common/components/MultiTypeDuration/MultiTypeDuration'
 
import { setFormikRef, StepFormikFowardRef, StepViewType } from '@pipeline/components/AbstractSteps/Step'
import { getNameAndIdentifierSchema } from '@pipeline/components/PipelineSteps/Steps/StepsValidateUtils'
 
import type { PolicyStepFormData } from './PolicyStepTypes'
import BasePolicyStep from './BasePolicyStep'
import { getPolicySetValidationSchema } from './utils'
 
interface PolicyStepWidgetProps {
  initialValues: PolicyStepFormData
  onUpdate?: (data: PolicyStepFormData) => void
  onChange?: (data: PolicyStepFormData) => void
  stepViewType?: StepViewType
  isNewStep?: boolean
  readonly?: boolean
  allowableTypes: MultiTypeInputType[]
}
 
function PolicyStepWidget(
  { initialValues, onUpdate, onChange, isNewStep, readonly, stepViewType, allowableTypes }: PolicyStepWidgetProps,
  formikRef: StepFormikFowardRef
): JSX.Element {
  const { getString } = useStrings()
 
  const validationSchema = Yup.object().shape({
    timeout: getDurationValidationSchema({ minimum: '10s' }).required(getString('validation.timeout10SecMinimum')),
    spec: Yup.object().shape({
      type: Yup.string()
        .trim()
        .required(getString('fieldRequired', { field: 'Entity Type' })),
      policySets: getPolicySetValidationSchema({
        minimumErrorMessage: getString('common.policiesSets.policySetRequired'),
        invalidErrorMessage: getString('invalidText') + getString('common.payload')
      }),
      policySpec: Yup.object().shape({
        payload: Yup.string()
          .trim()
          .required(getString('fieldRequired', { field: 'Payload' }))
      })
    }),
    ...getNameAndIdentifierSchema(getString, stepViewType)
  })
 
  return (
    <Formik<PolicyStepFormData>
      onSubmit={
        /* istanbul ignore next */ values => {
          onUpdate?.(values)
        }
      }
      validate={formValues => {
        /* istanbul ignore next */ onChange?.(formValues)
      }}
      formName="policyStepForm"
      initialValues={initialValues}
      validationSchema={validationSchema}
    >
      {(formik: FormikProps<PolicyStepFormData>) => {
        setFormikRef(formikRef, formik)
        return (
          <BasePolicyStep
            isNewStep={defaultTo(isNewStep, true)}
            formik={formik}
            readonly={readonly}
            stepViewType={stepViewType}
            allowableTypes={allowableTypes}
          />
        )
      }}
    </Formik>
  )
}
 
export const PolicyStepWidgetWithRef = React.forwardRef(PolicyStepWidget)