Skip to content

Commit

Permalink
add test for Textarea
Browse files Browse the repository at this point in the history
  • Loading branch information
joakbjerk committed Nov 30, 2023
1 parent ccbb5a2 commit f4a3951
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/dnb-eufemia/src/components/textarea/Textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default class Textarea extends React.PureComponent {
constructor(props) {
super(props)

this._ref = props.inner_ref || React.createRef()
this._ref = React.createRef()
this._id = props.id || makeUniqueId() // cause we need an id anyway

// make sure we don't trigger getDerivedStateFromProps on startup
Expand All @@ -206,6 +206,12 @@ export default class Textarea extends React.PureComponent {
this.state._value = props.value
}
componentDidMount() {
if (this.props.inner_ref) {
typeof this.props.inner_ref === 'function'
? this.props.inner_ref(this._ref.current)
: (this.props.inner_ref.current = this._ref.current)
}

if (isTrue(this.props.autoresize) && typeof window !== 'undefined') {
this.setAutosize()
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,37 @@ describe('Textarea component', () => {

expect(await axeComponent(Comp)).toHaveNoViolations()
})

it('gets valid ref element', () => {
let ref: React.RefObject<HTMLTextAreaElement>

function MockComponent() {
ref = React.useRef()
return <Textarea {...props} inner_ref={ref} />
}

render(<MockComponent />)

expect(ref.current.classList).toContain('dnb-textarea__textarea')
expect(ref.current.tagName).toBe('TEXTAREA')
expect(ref.current).toBeInstanceOf(HTMLTextAreaElement)
})

it('gets valid element when ref is function', () => {
const ref: React.MutableRefObject<HTMLTextAreaElement> =
React.createRef()

const refFn = (elem: HTMLTextAreaElement) => {
ref.current = elem
}

render(<Textarea id="unique" inner_ref={refFn} />)

expect(ref.current.getAttribute('id')).toBe('unique')
expect(ref.current.classList).toContain('dnb-textarea__textarea')
expect(ref.current.tagName).toBe('TEXTAREA')
expect(ref.current).toBeInstanceOf(HTMLTextAreaElement)
})
})

describe('Textarea scss', () => {
Expand Down

0 comments on commit f4a3951

Please sign in to comment.