Skip to content

Commit

Permalink
#24 added tests and prop types for Textarea component
Browse files Browse the repository at this point in the history
  • Loading branch information
paleika committed Jan 9, 2021
1 parent c631560 commit 7f9239b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rendering testing renders without error 1`] = `"<div class=\\"form-group bmd-form-group\\"><label class=\\"control-label bmd-label-static\\" for=\\"the-textarea\\">the textarea</label><textarea name=\\"the-textarea\\" id=\\"the-textarea\\" data-test=\\"textarea-node\\" rows=\\"1\\" class=\\"form-control\\">Some big text</textarea></div>"`;
13 changes: 11 additions & 2 deletions src/components/ui-elements/textarea/textarea.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useRef, useEffect } from 'react'
import React, { useRef } from 'react'
import PropTypes from 'prop-types'

const Textarea = ({ input, label }) => {
const name = input.name

const textareaRef = useRef(null)

useEffect(() => {
React.useEffect(() => {
textareaRef.current.style.height = '0px'
const scrollHeight = textareaRef.current.scrollHeight
textareaRef.current.style.setProperty('height', `${scrollHeight}px`, 'important')
Expand All @@ -21,6 +22,7 @@ const Textarea = ({ input, label }) => {
<textarea
{...input}
id={name}
data-test='textarea-node'
rows="1"
ref={textareaRef}
className="form-control"
Expand All @@ -30,3 +32,10 @@ const Textarea = ({ input, label }) => {
}

export default Textarea

Textarea.propTypes = {
input: PropTypes.shape({
name: PropTypes.string.isRequired
}),
label: PropTypes.string
}
45 changes: 45 additions & 0 deletions src/components/ui-elements/textarea/textarea.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react'
import { mount } from 'enzyme'

import Textarea from './textarea'
import { node } from 'prop-types'

const mockUseEffect = jest.fn()
React.useEffect = mockUseEffect

const setup = (props) => {
mockUseEffect.mockClear()

const defaultProps = { input: { name: 'the-textarea', value: 'Some big text', onChange: () => { } }, label: 'the textarea' }
return mount(<Textarea {...defaultProps} {...props} />)
}

describe('rendering testing', () => {
test('renders without error', () => {
const wrapper = setup()
expect(wrapper.html()).toMatchSnapshot()
})

test('counts height on value change', () => {
const currentProps = { input: { name: 'the-name', value: 'The value' } }
const wrapper = setup(currentProps)
mockUseEffect.mockClear()
wrapper.setProps({ input: { name: 'the-name', value: 'The other value' } })
expect(mockUseEffect).toHaveBeenCalled()
})

test('does not count height on other prop change (except for value)', () => {
const wrapper = setup()
mockUseEffect.mockClear()
wrapper.update()
expect(mockUseEffect).not.toHaveBeenCalled()
})

test('renders the value got from props on mount', () => {
const nodeValue = 'Some saved text'
const wrapper = setup({ input: { value: nodeValue } })
console.log(wrapper.debug())
const textareaNode = wrapper.find('[data-test="textarea-node"]')
expect(textareaNode.prop('value')).toEqual(nodeValue)
})
})

0 comments on commit 7f9239b

Please sign in to comment.