Skip to content

Commit

Permalink
fix(Forms): ensure Tools.Log formats arrays with square brackets (#4384)
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker authored Dec 11, 2024
1 parent 461e258 commit 7905694
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/dnb-eufemia/src/extensions/forms/Tools/Log.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ function replaceUndefinedValues(
): unknown {
if (typeof value === 'undefined') {
return replaceWith
} else if (Array.isArray(value)) {
return value.map((item) => replaceUndefinedValues(item, replaceWith))
} else if (value && typeof value === 'object' && value !== replaceWith) {
return {
...value,
...Object.fromEntries(
Object.entries(value).map(([k, v]) => [
k,
replaceUndefinedValues(v),
replaceUndefinedValues(v, replaceWith),
])
),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react'
import { render } from '@testing-library/react'
import { Form, Tools } from '../../'

describe('Tools.Log', () => {
it('should render data context', () => {
const data = { foo: 'bar' }
render(
<Form.Handler data={data}>
<Tools.Log />
</Form.Handler>
)

const element = document.querySelector('output')
expect(element.textContent).toBe(JSON.stringify(data, null, 2) + ' ')
})

it('should format array with square brackets', () => {
const data = { foo: ['bar', 'baz'] }
render(
<Form.Handler data={data}>
<Tools.Log />
</Form.Handler>
)

const element = document.querySelector('output')
expect(element.textContent).toBe(JSON.stringify(data, null, 2) + ' ')
expect(element.textContent).toContain('[')
expect(element.textContent).toContain('}')
})

it('should format "undefined"', () => {
const data = { foo: { bar: undefined } }
render(
<Form.Handler data={data}>
<Tools.Log />
</Form.Handler>
)

const element = document.querySelector('output')
expect(element.textContent).toContain('"bar": "undefined"')
})
})

0 comments on commit 7905694

Please sign in to comment.