Skip to content

Commit

Permalink
#24 tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
paleika committed Jan 9, 2021
1 parent 7f9239b commit f1c105d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rendering testing renders without error 1`] = `"<div class=\\"form-group bmd-form-group has-danger\\"><label class=\\"control-label bmd-label-static\\" for=\\"the-input\\">the input</label><input type=\\"text\\" name=\\"the-input\\" id=\\"the-input\\" class=\\"form-control\\"/><span class=\\"form-control-feedback\\"><i class=\\"material-icons \\">clear</i></span><label class=\\"error\\" for=\\"the-input\\" data-test=\\"error-message\\">Bad input</label></div>"`;
exports[`rendering testing renders without error 1`] = `"<div class=\\"form-group bmd-form-group has-danger\\"><label class=\\"control-label bmd-label-static\\" for=\\"the-input\\">the input</label><input type=\\"text\\" name=\\"the-input\\" id=\\"the-input\\" data-test=\\"input-node\\" class=\\"form-control\\"/><span class=\\"form-control-feedback\\"><i class=\\"material-icons \\">clear</i></span><label class=\\"error\\" for=\\"the-input\\" data-test=\\"error-message\\">Bad input</label></div>"`;
1 change: 1 addition & 0 deletions src/components/ui-elements/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Input = ({ input, type, label, disabled, meta: { touched, pristine, valid,
<input
{...input}
id={name}
data-test="input-node"
type={type}
className="form-control"
disabled={disabled}
Expand Down
11 changes: 9 additions & 2 deletions src/components/ui-elements/input/input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ const validMetaProps = { touched: true, pristine: false, valid: true, invalid: f
const invalidMetaProps = { touched: true, pristine: false, valid: false, invalid: true, error: 'Bad input' }

const setup = (props) => {
const defaultProps = { input: { name: 'the-input' }, type: 'text', label: 'the input', disabled: false }
const defaultProps = { input: { name: 'the-input' }, type: 'text', label: 'the input', disabled: false, meta: invalidMetaProps }
return shallow(<Input {...defaultProps} {...props} />)
}

describe('rendering testing', () => {
let wrapper

test('renders without error', () => {
wrapper = setup({ meta: invalidMetaProps })
wrapper = setup()
expect(wrapper.html()).toMatchSnapshot()
})

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

describe('valid input data', () => {
beforeEach(() => {
wrapper = setup({ meta: validMetaProps })
Expand Down
6 changes: 6 additions & 0 deletions src/components/ui-elements/textarea/textarea.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import React, { useRef } from 'react'
import PropTypes from 'prop-types'

/**
* @component A textarea form field to use within Redux-form
* @param {object} input Prop that connects a component to the Redux
* @param {string} label The label of the checkbox
* @returns React Component
*/
const Textarea = ({ input, label }) => {
const name = input.name

Expand Down
8 changes: 3 additions & 5 deletions src/components/ui-elements/textarea/textarea.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react'
import { mount } from 'enzyme'

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

const mockUseEffect = jest.fn()
React.useEffect = mockUseEffect
Expand All @@ -20,15 +19,15 @@ describe('rendering testing', () => {
expect(wrapper.html()).toMatchSnapshot()
})

test('counts height on value change', () => {
test('counts height on value prop 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)', () => {
test('does not count height on update with the same value prop', () => {
const wrapper = setup()
mockUseEffect.mockClear()
wrapper.update()
Expand All @@ -37,8 +36,7 @@ describe('rendering testing', () => {

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 wrapper = setup({ input: { name: 'the-name', value: nodeValue } })
const textareaNode = wrapper.find('[data-test="textarea-node"]')
expect(textareaNode.prop('value')).toEqual(nodeValue)
})
Expand Down

0 comments on commit f1c105d

Please sign in to comment.