All files / modules/85-cv/pages/health-source/connectors/AppDynamics/Components/BasePath BasePath.utils.ts

85% Statements 17/20
66.67% Branches 12/18
100% Functions 1/1
88.89% Lines 16/18

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              26x 26x     26x   1x 1x 1x 1x 1x   1x     1x 1x 1x           1x   1x 1x           1x          
/*
 * Copyright 2021 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 { cloneDeep } from 'lodash-es'
import { BasePathKeyPrefix } from './BasePath.constants'
import type { BasePathData } from './BasePath.types'
 
export const onBasePathChange = (selectedPath: string, index: number, basePathData: BasePathData): BasePathData => {
  // new logic
  const currentKey = BasePathKeyPrefix + index
  const nextItemKey = BasePathKeyPrefix + parseInt((index + 1).toString())
  const prvKey = BasePathKeyPrefix + index
  const path = basePathData[prvKey]?.path
  const lastItem = Object.keys(basePathData)[Object.keys(basePathData).length - 1]
 
  let basePathDetails = cloneDeep(basePathData)
 
  // delete values after index
  const lastIndex = parseInt(lastItem.split('_')[1])
  const lastItemexist = !!basePathDetails[lastItem]
  Iif (index < lastIndex && lastItemexist) {
    for (let idx = index + 1; idx <= lastIndex; idx++) {
      delete basePathDetails[BasePathKeyPrefix + idx]
    }
  }
 
  Eif (!basePathDetails[nextItemKey] && selectedPath) {
    // adding first time
    const nextPath = path?.length ? `${path}|${selectedPath}` : selectedPath
    basePathDetails = {
      ...basePathDetails,
      [nextItemKey]: { value: '', path: nextPath }
    }
  }
 
  return {
    ...basePathDetails,
    [currentKey]: { value: selectedPath, path }
  }
}