Skip to content

Commit 32f3e65

Browse files
author
Kent C. Dodds
committed
fix(change): no longer using Simulate.change
In real applications, your `onChange` handler wont be called if the value did not change, so the tests shouldn't either. This is another step toward us not using Simulate at all. When using `fireEvent.change` make sure that you're setting the value to change to via: `{target: {value: 'the value'}}` as the second arg. BREAKING CHANGE: If you used the old form of value changes you'll need to update your code to the new form: Before: ```js formField.value = newValue; fireEvent.change(formField); ``` After: ```js fireEvent.change(formField, {target: {value: newValue}}); ```
1 parent 633cb54 commit 32f3e65

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/__tests__/events.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const eventTypes = [
3030
},
3131
{
3232
type: 'Focus',
33-
events: ['change', 'input', 'invalid'],
33+
events: ['input', 'invalid'],
3434
elementType: 'input',
3535
},
3636
{
@@ -154,3 +154,12 @@ eventTypes.forEach(({type, events, elementType, init}) => {
154154
})
155155
})
156156
})
157+
158+
test('onChange works', () => {
159+
const handleChange = jest.fn()
160+
const {
161+
container: {firstChild: input},
162+
} = render(<input onChange={handleChange} />)
163+
fireEvent.change(input, {target: {value: 'a'}})
164+
expect(handleChange).toHaveBeenCalledTimes(1)
165+
})

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function cleanupAtContainer(container) {
4848
}
4949

5050
// fallback to synthetic events for React events that the DOM doesn't support
51-
const syntheticEvents = ['change', 'select', 'mouseEnter', 'mouseLeave']
51+
const syntheticEvents = ['select', 'mouseEnter', 'mouseLeave']
5252
syntheticEvents.forEach(eventName => {
5353
document.addEventListener(eventName.toLowerCase(), e => {
5454
Simulate[eventName](e.target, e)

0 commit comments

Comments
 (0)