Skip to content

Commit 2335842

Browse files
authored
fix: submit form on ENTER pressed (testing-library#397)
1 parent e1423ed commit 2335842

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/__tests__/type.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,3 +756,20 @@ test('typing on input type file should not result in an error', () => {
756756
expect.assertions(0)
757757
return userEvent.type(element, 'bar').catch(e => expect(e).toBeUndefined())
758758
})
759+
760+
test('should submit a form when ENTER is pressed on input', () => {
761+
const handleSubmit = jest.fn()
762+
const {element} = setup(
763+
`
764+
<form>
765+
<input type='text'/>
766+
</form>
767+
`,
768+
{
769+
eventHandlers: {submit: handleSubmit},
770+
},
771+
)
772+
userEvent.type(element.querySelector('input'), '{enter}')
773+
774+
expect(handleSubmit).toHaveBeenCalledTimes(1)
775+
})

src/type.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,10 @@ function handleEnter({currentElement, eventOverrides}) {
490490
})
491491
}
492492

493+
if (currentElement().tagName === 'INPUT' && currentElement().form) {
494+
fireEvent.submit(currentElement().form)
495+
}
496+
493497
fireEvent.keyUp(currentElement(), {
494498
key,
495499
keyCode,

0 commit comments

Comments
 (0)