Skip to content

Commit dbe2714

Browse files
Merge pull request #11 from balazsorban44/dev
Set checked prop on radio buttons
2 parents e3f8a78 + c4eb11a commit dbe2714

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

src/__tests__/useForm-with-input-prop-generator.test.js

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react'
22
import { render, fireEvent } from '../test-utils'
33
import validatorsMock from './utils/validators.mock'
44
import useForm from '../useForm'
5+
import { screen } from '@testing-library/react'
56

67
const initialStates = {
78
form: {
@@ -32,7 +33,7 @@ const providerProps = {
3233

3334
describe('v3 input props', () => {
3435
const App = () => {
35-
const { fields, inputs, loading } = useForm({ name: 'form', validators: validatorsMock })
36+
const { fields, inputs } = useForm({ name: 'form', validators: validatorsMock })
3637

3738
return (
3839
<form>
@@ -61,11 +62,11 @@ describe('v3 input props', () => {
6162
<fieldset>
6263
<legend>Selects:</legend>
6364

64-
<radiogroup>
65-
<input {...inputs.radio('radio', { value: 'option-1' })}/>
66-
<input {...inputs.radio('radio', { value: 'option-2' })}/>
67-
<p>{fields.radio.value}</p>
68-
</radiogroup>
65+
<label htmlFor="option-1">Option 1</label>
66+
<input {...inputs.radio('radio', { value: 'option-1' })}/>
67+
<label htmlFor="option-2">Option 2</label>
68+
<input {...inputs.radio('radio', { value: 'option-2' })}/>
69+
<p>{fields.radio.value}</p>
6970

7071
{/* REVIEW: */}
7172
<label htmlFor="checkbox">checkbox</label>
@@ -109,63 +110,63 @@ describe('v3 input props', () => {
109110

110111
const dateTypes = ['date', 'time', 'week', 'month', 'datetime-local']
111112
it('Dates: date input', () => {
112-
const { getByDisplayValue, debug } = render(<App/>, providerProps)
113+
render(<App/>, providerProps)
113114

114115
const value = new Date().toISOString().slice(0, 10)
115116

116117
fireEvent.change(
117-
getByDisplayValue(initialStates.form.date),
118+
screen.getByDisplayValue(initialStates.form.date),
118119
{ target: { name: 'date', value } }
119120
)
120-
expect(getByDisplayValue(value)).toBeInTheDocument()
121+
expect(screen.getByDisplayValue(value)).toBeInTheDocument()
121122
})
122123

123124
it('Dates: datetime-local input', () => {
124-
const { getByDisplayValue, debug } = render(<App/>, providerProps)
125+
render(<App/>, providerProps)
125126

126127
const value = new Date().toISOString().slice(0, 16)
127128

128129
fireEvent.change(
129-
getByDisplayValue(initialStates.form.datetimeLocal),
130+
screen.getByDisplayValue(initialStates.form.datetimeLocal),
130131
{ target: { name: 'datetimeLocal', value } }
131132
)
132-
expect(getByDisplayValue(value)).toBeInTheDocument()
133+
expect(screen.getByDisplayValue(value)).toBeInTheDocument()
133134
})
134135

135136
it('Dates: time input', () => {
136-
const { getByDisplayValue, debug } = render(<App/>, providerProps)
137+
render(<App/>, providerProps)
137138

138139
const value = '14:00'
139140

140141
fireEvent.change(
141-
getByDisplayValue(initialStates.form.time),
142+
screen.getByDisplayValue(initialStates.form.time),
142143
{ target: { name: 'time', value } }
143144
)
144-
expect(getByDisplayValue(value)).toBeInTheDocument()
145+
expect(screen.getByDisplayValue(value)).toBeInTheDocument()
145146
})
146147

147148
it('Dates: week input', () => {
148-
const { getByDisplayValue, debug } = render(<App/>, providerProps)
149+
render(<App/>, providerProps)
149150

150151
const value = '2019-W33'
151152

152153
fireEvent.change(
153-
getByDisplayValue(initialStates.form.week),
154+
screen.getByDisplayValue(initialStates.form.week),
154155
{ target: { name: 'week', value } }
155156
)
156-
expect(getByDisplayValue(value)).toBeInTheDocument()
157+
expect(screen.getByDisplayValue(value)).toBeInTheDocument()
157158
})
158159

159160
it('Dates: month input', () => {
160-
const { getByDisplayValue, debug } = render(<App/>, providerProps)
161+
render(<App/>, providerProps)
161162

162163
const value = '2019-08'
163164

164165
fireEvent.change(
165-
getByDisplayValue(initialStates.form.month),
166+
screen.getByDisplayValue(initialStates.form.month),
166167
{ target: { name: 'month', value } }
167168
)
168-
expect(getByDisplayValue(value)).toBeInTheDocument()
169+
expect(screen.getByDisplayValue(value)).toBeInTheDocument()
169170
})
170171

171172

@@ -201,13 +202,27 @@ describe('v3 input props', () => {
201202
})
202203

203204
it('Selects: radio input', () => {
204-
const { getByDisplayValue, queryByText } = render(<App/>, providerProps)
205-
expect(queryByText('option-2')).not.toBeInTheDocument()
206-
fireEvent.click(
207-
getByDisplayValue('option-2'),
208-
{ target: { name: 'radio', value: 'option-2' } }
209-
)
210-
expect(queryByText('option-2')).toBeInTheDocument()
205+
render(<App/>, providerProps)
206+
207+
expect(screen.getByText('option-1')).toBeInTheDocument()
208+
expect(screen.queryByText('option-2')).not.toBeInTheDocument()
209+
210+
const option1 = screen.getByLabelText(/option 1/i)
211+
expect(option1).toBeChecked()
212+
213+
const option2 = screen.getByLabelText(/option 2/i)
214+
215+
fireEvent.click(option2)
216+
expect(option1).not.toBeChecked()
217+
expect(option2).toBeChecked()
218+
expect(screen.queryByText('option-1')).not.toBeInTheDocument()
219+
expect(screen.getByText('option-2')).toBeInTheDocument()
220+
221+
fireEvent.click(option1)
222+
expect(option1).toBeChecked()
223+
expect(option2).not.toBeChecked()
224+
expect(screen.getByText('option-1')).toBeInTheDocument()
225+
expect(screen.queryByText('option-2')).not.toBeInTheDocument()
211226
})
212227

213228

src/inputPropGenerators.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const inputPropsGenerator = ({ type, fields, handleChange, handleSubmit }) =>
3434
props.type = 'datetime-local'
3535
break
3636
case 'radio':
37+
props.checked = field.value === value
3738
props.id = value
3839
break
3940
case 'checkbox':

0 commit comments

Comments
 (0)