Skip to content

Commit

Permalink
fix(Forms): render given elements to warning and info props
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Nov 11, 2024
1 parent ca3bbde commit 47e1dfa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ function FieldBlock(props: Props) {
if (messages.length > 0) {
acc[type] = {
...acc[type],
text: <CombineMessages type={type} messages={messages} />,
children: <CombineMessages type={type} messages={messages} />,
}

fieldStateIdsRef.current[type] = id
Expand Down Expand Up @@ -682,7 +682,7 @@ export function getMessagesFromError(
return [
((content instanceof Error && content.message) ||
(content instanceof FormError && content.message) ||
content?.toString() ||
(React.isValidElement(content) ? content : content?.toString()) ||
content) as StateMessage,
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,33 @@ describe('FieldBlock', () => {
expect(element).toHaveClass('dnb-height-animation--is-visible')
expect(element).toHaveTextContent(blockInfo)
})

it('should render the given element', () => {
render(
<FieldBlock info={<strong>{blockInfo}</strong>}>
content
</FieldBlock>
)

const element = document.querySelector('.dnb-form-status')

expect(element).toBeInTheDocument()
expect(element).toHaveClass('dnb-form-status--info')
expect(element).toHaveClass('dnb-height-animation--is-visible')
expect(element).toHaveTextContent(blockInfo)
})

it('should render the given React component', () => {
const MockComponent = () => <strong>{blockInfo}</strong>
render(<FieldBlock info={<MockComponent />}>content</FieldBlock>)

const element = document.querySelector('.dnb-form-status')

expect(element).toBeInTheDocument()
expect(element).toHaveClass('dnb-form-status--info')
expect(element).toHaveClass('dnb-height-animation--is-visible')
expect(element).toHaveTextContent(blockInfo)
})
})

describe('warning prop', () => {
Expand All @@ -713,6 +740,21 @@ describe('FieldBlock', () => {
expect(element).toHaveClass('dnb-height-animation--is-visible')
expect(element).toHaveTextContent(blockWarning)
})

it('should render the given element', () => {
render(
<FieldBlock warning={<strong>{blockWarning}</strong>}>
content
</FieldBlock>
)

const element = document.querySelector('.dnb-form-status')

expect(element).toBeInTheDocument()
expect(element).toHaveClass('dnb-form-status--warn')
expect(element).toHaveClass('dnb-height-animation--is-visible')
expect(element).toHaveTextContent(blockWarning)
})
})

describe('error prop', () => {
Expand Down

0 comments on commit 47e1dfa

Please sign in to comment.