Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 407 type on no value elements #414

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 68 additions & 8 deletions src/__tests__/type-modifiers.js
Expand Up @@ -443,6 +443,36 @@ test('{enter} on a button', () => {
`)
})

test('{enter} with preventDefault on a button', () => {
const {element, getEventSnapshot} = setup('<button />', {
eventHandlers: {
keyDown: e => e.preventDefault(),
},
})

userEvent.type(element, '{enter}')

expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: button

button - pointerover
button - pointerenter
button - mouseover: Left (0)
button - mouseenter: Left (0)
button - pointermove
button - mousemove: Left (0)
button - pointerdown
button - mousedown: Left (0)
button - focus
button - focusin
button - pointerup
button - mouseup: Left (0)
button - click: Left (0)
button - keydown: Enter (13)
button - keyup: Enter (13)
`)
})

test('{space} on a button', () => {
const {element, getEventSnapshot} = setup('<button />')

Expand Down Expand Up @@ -471,6 +501,34 @@ test('{space} on a button', () => {
`)
})

test(`' ' on a button is the same as '{space}'`, () => {
const {element, getEventSnapshot} = setup('<button />')

userEvent.type(element, ' ')

expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: button

button - pointerover
button - pointerenter
button - mouseover: Left (0)
button - mouseenter: Left (0)
button - pointermove
button - mousemove: Left (0)
button - pointerdown
button - mousedown: Left (0)
button - focus
button - focusin
button - pointerup
button - mouseup: Left (0)
button - click: Left (0)
button - keydown: (32)
button - keypress: (32)
visualjerk marked this conversation as resolved.
Show resolved Hide resolved
button - keyup: (32)
button - click: Left (0)
`)
})

test('{space} with preventDefault keydown on button', () => {
const {element, getEventSnapshot} = setup('<button />', {
eventHandlers: {
Expand Down Expand Up @@ -498,14 +556,17 @@ test('{space} with preventDefault keydown on button', () => {
button - click: Left (0)
button - keydown: (32)
button - keyup: (32)
button - click: Left (0)
`)
})

test(`' ' on a button`, () => {
const {element, getEventSnapshot} = setup('<button />')
test('{space} with preventDefault keyup on button', () => {
const {element, getEventSnapshot} = setup('<button />', {
eventHandlers: {
keyUp: e => e.preventDefault(),
},
})

userEvent.type(element, ' ')
userEvent.type(element, '{space}')

expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: button
Expand All @@ -526,7 +587,6 @@ test(`' ' on a button`, () => {
button - keydown: (32)
button - keypress: (32)
button - keyup: (32)
button - click: Left (0)
`)
})

Expand Down Expand Up @@ -559,7 +619,7 @@ test('{space} on an input', () => {
`)
})

test('{enter} on an input type="color"', () => {
test('{enter} on an input type="color" fires same events as a button', () => {
const {element, getEventSnapshot} = setup(
`<input value="#ffffff" type="color" />`,
)
Expand Down Expand Up @@ -589,7 +649,7 @@ test('{enter} on an input type="color"', () => {
`)
})

test('{space} on an input type="color"', () => {
test('{space} on an input type="color" fires same events as a button', () => {
const {element, getEventSnapshot} = setup(
`<input value="#ffffff" type="color" />`,
)
Expand Down Expand Up @@ -619,7 +679,7 @@ test('{space} on an input type="color"', () => {
`)
})

test('" " on an input type="color"', () => {
test(`' ' on input type="color" is the same as '{space}'`, () => {
const {element, getEventSnapshot} = setup(
`<input value="#ffffff" type="color" />`,
)
Expand Down
24 changes: 10 additions & 14 deletions src/__tests__/type.js
Expand Up @@ -707,13 +707,12 @@ test('typing an invalid input value', () => {
expect(element.validity.badInput).toBe(false)
})

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

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

test('typing on input type file should not trigger input event', () => {
const inputHandler = jest.fn()
const {element} = setup('<input type="file" />', {
eventHandlers: {input: inputHandler},
})
userEvent.type(element, 'bar')
expect(inputHandler).toHaveBeenCalledTimes(0)
test('typing on input type file should not result in an error', () => {
const {element} = setup('<input type="file" />')
expect.assertions(0)
return userEvent.type(element, 'bar').catch(e => expect(e).toBeUndefined())
})
6 changes: 1 addition & 5 deletions src/paste.js
Expand Up @@ -38,11 +38,7 @@ function paste(
fireEvent.paste(element, init)

if (!element.readOnly) {
const {newValue, newSelectionStart} = calculateNewValue(
text,
element,
element.value,
)
const {newValue, newSelectionStart} = calculateNewValue(text, element)
fireEvent.input(element, {
inputType: 'insertFromPaste',
target: {value: newValue},
Expand Down