Skip to content

Commit

Permalink
fix(Forms): ensure label supports HTML formatting (#3911)
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker authored Sep 9, 2024
1 parent bc40449 commit 227569c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ function willWordWrap(element: HTMLElement, word: string) {
return
}

const { offsetHeight, textContent } = element
const { offsetHeight, innerHTML } = element

element.textContent += word
element.innerHTML += word
const height = element.offsetHeight
element.textContent = textContent
element.innerHTML = innerHTML

return height > offsetHeight
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,69 @@ describe('Form.SubmitIndicator', () => {
)
})

it('should support HTML content', () => {
Object.defineProperties(HTMLElement.prototype, {
offsetHeight: {
get: () => 10,
},
})

const { rerender } = render(
<Form.SubmitIndicator state="pending">
Text of a <b>bold</b> label 1
</Form.SubmitIndicator>
)

const element = document.querySelector('.dnb-forms-submit-indicator ')
expect(element).not.toHaveClass(
'dnb-forms-submit-indicator--inline-wrap'
)

expect(element.querySelector('span').innerHTML).toBe(
'Text of a <b>bold</b> label 1'
)

let count = 0
Object.defineProperties(HTMLElement.prototype, {
offsetHeight: {
get: () => {
count++
return 10 * count
},
},
})

rerender(
<Form.SubmitIndicator state="pending">
Text of a <b>bold</b> label 2
</Form.SubmitIndicator>
)

expect(element).toHaveClass('dnb-forms-submit-indicator--inline-wrap')
expect(element.querySelector('span').innerHTML).toBe(
'Text of a <b>bold</b> label 2'
)

Object.defineProperties(HTMLElement.prototype, {
offsetHeight: {
get: () => 30,
},
})

rerender(
<Form.SubmitIndicator state="pending">
Text of a <b>bold</b> label 3
</Form.SubmitIndicator>
)

expect(element).not.toHaveClass(
'dnb-forms-submit-indicator--inline-wrap'
)
expect(element.querySelector('span').innerHTML).toBe(
'Text of a <b>bold</b> label 3'
)
})

it('should update children (label) when it changes', async () => {
const MockComponent = () => {
const [count, increment] = React.useReducer((state) => state + 1, 1)
Expand Down

0 comments on commit 227569c

Please sign in to comment.