Skip to content

Commit e1423ed

Browse files
authored
fix(type): type on no value elements (testing-library#414)
Closes: testing-library#407
1 parent 52c333c commit e1423ed

File tree

4 files changed

+113
-92
lines changed

4 files changed

+113
-92
lines changed

src/__tests__/type-modifiers.js

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,36 @@ test('{enter} on a button', () => {
443443
`)
444444
})
445445

446+
test('{enter} with preventDefault on a button', () => {
447+
const {element, getEventSnapshot} = setup('<button />', {
448+
eventHandlers: {
449+
keyDown: e => e.preventDefault(),
450+
},
451+
})
452+
453+
userEvent.type(element, '{enter}')
454+
455+
expect(getEventSnapshot()).toMatchInlineSnapshot(`
456+
Events fired on: button
457+
458+
button - pointerover
459+
button - pointerenter
460+
button - mouseover: Left (0)
461+
button - mouseenter: Left (0)
462+
button - pointermove
463+
button - mousemove: Left (0)
464+
button - pointerdown
465+
button - mousedown: Left (0)
466+
button - focus
467+
button - focusin
468+
button - pointerup
469+
button - mouseup: Left (0)
470+
button - click: Left (0)
471+
button - keydown: Enter (13)
472+
button - keyup: Enter (13)
473+
`)
474+
})
475+
446476
test('{space} on a button', () => {
447477
const {element, getEventSnapshot} = setup('<button />')
448478

@@ -471,6 +501,34 @@ test('{space} on a button', () => {
471501
`)
472502
})
473503

504+
test(`' ' on a button is the same as '{space}'`, () => {
505+
const {element, getEventSnapshot} = setup('<button />')
506+
507+
userEvent.type(element, ' ')
508+
509+
expect(getEventSnapshot()).toMatchInlineSnapshot(`
510+
Events fired on: button
511+
512+
button - pointerover
513+
button - pointerenter
514+
button - mouseover: Left (0)
515+
button - mouseenter: Left (0)
516+
button - pointermove
517+
button - mousemove: Left (0)
518+
button - pointerdown
519+
button - mousedown: Left (0)
520+
button - focus
521+
button - focusin
522+
button - pointerup
523+
button - mouseup: Left (0)
524+
button - click: Left (0)
525+
button - keydown: (32)
526+
button - keypress: (32)
527+
button - keyup: (32)
528+
button - click: Left (0)
529+
`)
530+
})
531+
474532
test('{space} with preventDefault keydown on button', () => {
475533
const {element, getEventSnapshot} = setup('<button />', {
476534
eventHandlers: {
@@ -498,14 +556,17 @@ test('{space} with preventDefault keydown on button', () => {
498556
button - click: Left (0)
499557
button - keydown: (32)
500558
button - keyup: (32)
501-
button - click: Left (0)
502559
`)
503560
})
504561

505-
test(`' ' on a button`, () => {
506-
const {element, getEventSnapshot} = setup('<button />')
562+
test('{space} with preventDefault keyup on button', () => {
563+
const {element, getEventSnapshot} = setup('<button />', {
564+
eventHandlers: {
565+
keyUp: e => e.preventDefault(),
566+
},
567+
})
507568

508-
userEvent.type(element, ' ')
569+
userEvent.type(element, '{space}')
509570

510571
expect(getEventSnapshot()).toMatchInlineSnapshot(`
511572
Events fired on: button
@@ -526,7 +587,6 @@ test(`' ' on a button`, () => {
526587
button - keydown: (32)
527588
button - keypress: (32)
528589
button - keyup: (32)
529-
button - click: Left (0)
530590
`)
531591
})
532592

@@ -559,7 +619,7 @@ test('{space} on an input', () => {
559619
`)
560620
})
561621

562-
test('{enter} on an input type="color"', () => {
622+
test('{enter} on an input type="color" fires same events as a button', () => {
563623
const {element, getEventSnapshot} = setup(
564624
`<input value="#ffffff" type="color" />`,
565625
)
@@ -589,7 +649,7 @@ test('{enter} on an input type="color"', () => {
589649
`)
590650
})
591651

592-
test('{space} on an input type="color"', () => {
652+
test('{space} on an input type="color" fires same events as a button', () => {
593653
const {element, getEventSnapshot} = setup(
594654
`<input value="#ffffff" type="color" />`,
595655
)
@@ -619,7 +679,7 @@ test('{space} on an input type="color"', () => {
619679
`)
620680
})
621681

622-
test('" " on an input type="color"', () => {
682+
test(`' ' on input type="color" is the same as '{space}'`, () => {
623683
const {element, getEventSnapshot} = setup(
624684
`<input value="#ffffff" type="color" />`,
625685
)

src/__tests__/type.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -707,13 +707,12 @@ test('typing an invalid input value', () => {
707707
expect(element.validity.badInput).toBe(false)
708708
})
709709

710-
test('should give error if we are trying to call type on an invalid element', async () => {
711-
const {element} = setup('<div />')
712-
await expect(() =>
713-
userEvent.type(element, "I'm only a div :("),
714-
).rejects.toThrowErrorMatchingInlineSnapshot(
715-
`"the current element is of type BODY and doesn't have a valid value"`,
716-
)
710+
test('should not throw error if we are trying to call type on an element without a value', () => {
711+
const {element} = setup('<div />')
712+
expect.assertions(0)
713+
return userEvent
714+
.type(element, "I'm only a div :(")
715+
.catch(e => expect(e).toBeUndefined())
717716
})
718717

719718
test('typing on button should not alter its value', () => {
@@ -752,11 +751,8 @@ test('typing on input type submit should not alter its value', () => {
752751
expect(element).toHaveValue('foo')
753752
})
754753

755-
test('typing on input type file should not trigger input event', () => {
756-
const inputHandler = jest.fn()
757-
const {element} = setup('<input type="file" />', {
758-
eventHandlers: {input: inputHandler},
759-
})
760-
userEvent.type(element, 'bar')
761-
expect(inputHandler).toHaveBeenCalledTimes(0)
754+
test('typing on input type file should not result in an error', () => {
755+
const {element} = setup('<input type="file" />')
756+
expect.assertions(0)
757+
return userEvent.type(element, 'bar').catch(e => expect(e).toBeUndefined())
762758
})

src/paste.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ function paste(
3838
fireEvent.paste(element, init)
3939

4040
if (!element.readOnly) {
41-
const {newValue, newSelectionStart} = calculateNewValue(
42-
text,
43-
element,
44-
element.value,
45-
)
41+
const {newValue, newSelectionStart} = calculateNewValue(text, element)
4642
fireEvent.input(element, {
4743
inputType: 'insertFromPaste',
4844
target: {value: newValue},

0 commit comments

Comments
 (0)