Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions frontend/src/resources/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ export function exportObjectString(object: Record<string, string>) {
return keyValueMap.toString()
}

export function returnCSVSafeString(exportValue: string | ReactNode) {
// extract newlines
return `"${typeof exportValue === 'string' ? exportValue.split('\n').join(' ').replace(/"/g, '""') : exportValue}"`
export function returnCSVSafeString(exportValue: string | number | ReactNode) {
if (typeof exportValue === 'number') {
return `"${exportValue}"`
}
if (typeof exportValue !== 'string') {
return '"-"'
}
return `"${exportValue.replaceAll('"', '""')}"`
}

export const getISOStringTimestamp = (timestamp: string) => {
Expand Down
24 changes: 23 additions & 1 deletion frontend/src/routes/Governance/common/util.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* Copyright Contributors to the Open Cluster Management project */
'use strict'

import { hasInformOnlyPolicies, getPolicyRemediation, resolveExternalStatus, parseStringMap } from './util'
import { hasInformOnlyPolicies, getPolicyRemediation, resolveExternalStatus, parseStringMap, preserveWhitespace, returnCSVSafeString } from './util'
import { PolicyTableItem } from '../policies/Policies'
import { Policy, PolicyTemplate, REMEDIATION_ACTION } from '../../../resources'
import { cloneDeep } from 'lodash'
import { render } from '@testing-library/react'

describe('Test resolveExternalStatus', () => {
const mockPolicyWithManagers = (managers: string[]): Policy => {
Expand Down Expand Up @@ -901,3 +902,24 @@ describe('Test parseStringValue', () => {
)
})
})

describe('ACM-32500: UI description text should match CSV export for multiline descriptions', () => {
const descriptionFromApi =
'Policy is placed on hub or managed clusters with label acm-virt-config=acm-dr-virt-config-file-name.\nCreates a velero Schedule for all virtualmachines.kubevirt.io resources with a cluster.open-cluster-management.io/backup-vm label.'

test('preserveWhitespace textContent should equal CSV export value', () => {
const formatted = preserveWhitespace(descriptionFromApi)
const { container } = render(<>{formatted}</>)
const uiText = container.textContent

const csvRaw = returnCSVSafeString(descriptionFromApi)
const csvText = csvRaw.slice(1, -1).replace(/""/g, '"')

expect(uiText).toEqual(csvText)
})

test('preserveWhitespace returns undefined for empty input', () => {
expect(preserveWhitespace(undefined)).toBeUndefined()
expect(preserveWhitespace('')).toBeUndefined()
})
})
4 changes: 2 additions & 2 deletions frontend/src/ui-components/AcmTable/AcmTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1150,10 +1150,10 @@ describe('AcmTable', () => {
})

describe('returnCSVSafeString', () => {
test('returns a csv safe string replacing new line with space', () => {
test('returns a csv safe string preserving newlines', () => {
const content = 'testing for multiline\ndescription'
const exportContent = returnCSVSafeString(content)
expect(exportContent).toEqual('"testing for multiline description"')
expect(exportContent).toEqual('"testing for multiline\ndescription"')
})

test('returns a csv safe string escaping double quotes', () => {
Expand Down