-
Notifications
You must be signed in to change notification settings - Fork 111
Use and expose a local copy of fireEvent #75
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ export default { | |
props: { | ||
text: { | ||
type: String, | ||
required: true | ||
default: 'Button Text' | ||
} | ||
}, | ||
methods: { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import { render, fireEvent } from '@testing-library/vue' | ||
import Button from './components/Button' | ||
|
||
const eventTypes = [ | ||
{ | ||
type: 'Clipboard', | ||
events: ['copy', 'paste'] | ||
}, | ||
{ | ||
type: 'Composition', | ||
events: ['compositionEnd', 'compositionStart', 'compositionUpdate'] | ||
}, | ||
{ | ||
type: 'Keyboard', | ||
events: ['keyDown', 'keyPress', 'keyUp'], | ||
init: { keyCode: 13 } | ||
}, | ||
{ | ||
type: 'Focus', | ||
events: ['focus', 'blur'] | ||
}, | ||
{ | ||
type: 'Form', | ||
events: ['focus', 'blur'] | ||
}, | ||
{ | ||
type: 'Focus', | ||
events: ['input', 'invalid'] | ||
}, | ||
{ | ||
type: 'Focus', | ||
events: ['submit'], | ||
elementType: 'form' | ||
}, | ||
{ | ||
type: 'Mouse', | ||
events: [ | ||
'click', | ||
'contextMenu', | ||
'drag', | ||
'dragEnd', | ||
'dragEnter', | ||
'dragExit', | ||
'dragLeave', | ||
'dragOver', | ||
'dragStart', | ||
'drop', | ||
'mouseDown', | ||
'mouseEnter', | ||
'mouseLeave', | ||
'mouseMove', | ||
'mouseOut', | ||
'mouseOver', | ||
'mouseUp' | ||
], | ||
elementType: 'button' | ||
}, | ||
{ | ||
type: 'Selection', | ||
events: ['select'] | ||
}, | ||
{ | ||
type: 'Touch', | ||
events: ['touchCancel', 'touchEnd', 'touchMove', 'touchStart'], | ||
elementType: 'button' | ||
}, | ||
{ | ||
type: 'UI', | ||
events: ['scroll'], | ||
elementType: 'div' | ||
}, | ||
{ | ||
type: 'Wheel', | ||
events: ['wheel'], | ||
elementType: 'div' | ||
}, | ||
{ | ||
type: 'Media', | ||
events: [ | ||
'abort', | ||
'canPlay', | ||
'canPlayThrough', | ||
'durationChange', | ||
'emptied', | ||
'encrypted', | ||
'ended', | ||
'error', | ||
'loadedData', | ||
'loadedMetadata', | ||
'loadStart', | ||
'pause', | ||
'play', | ||
'playing', | ||
'progress', | ||
'rateChange', | ||
'seeked', | ||
'seeking', | ||
'stalled', | ||
'suspend', | ||
'timeUpdate', | ||
'volumeChange', | ||
'waiting' | ||
], | ||
elementType: 'video' | ||
}, | ||
{ | ||
type: 'Image', | ||
events: ['load', 'error'], | ||
elementType: 'img' | ||
}, | ||
{ | ||
type: 'Animation', | ||
events: ['animationStart', 'animationEnd', 'animationIteration'], | ||
elementType: 'div' | ||
}, | ||
{ | ||
type: 'Transition', | ||
events: ['transitionEnd'], | ||
elementType: 'div' | ||
} | ||
] | ||
|
||
// For each event type, we assert that the right events are being triggered | ||
// when the associated fireEvent method is called. | ||
eventTypes.forEach(({ type, events, elementType = 'input', init }) => { | ||
describe(`${type} Events`, () => { | ||
events.forEach(eventName => { | ||
it(`triggers ${eventName}`, async () => { | ||
const testId = `${type}-${eventName}` | ||
const spy = jest.fn() | ||
|
||
// Render an element with a listener of the event under testing and a | ||
// test-id attribute, so that we can get the DOM node afterwards. | ||
const { getByTestId } = render({ | ||
render(h) { | ||
return h(elementType, { | ||
on: { | ||
[eventName.toLowerCase()]: spy | ||
}, | ||
attrs: { | ||
'data-testid': testId | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
const elem = getByTestId(testId) | ||
|
||
await fireEvent[eventName](elem, init) | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
// The event is called `dblclick`, while fireEvent exposes a "doubleClick" | ||
test('triggers dblclick on doubleClick', async () => { | ||
const spy = jest.fn() | ||
|
||
const { getByRole } = render({ | ||
render(h) { | ||
return h('input', { | ||
on: { dblclick: spy } | ||
}) | ||
} | ||
}) | ||
|
||
const elem = getByRole('textbox') | ||
|
||
await fireEvent.doubleClick(elem) | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
// fireEvent(node, event) is also a valid API | ||
test('calling `fireEvent` directly works too', async () => { | ||
const { getByRole, emitted } = render(Button) | ||
|
||
const button = getByRole('button') | ||
|
||
await fireEvent(button, new Event('click')) | ||
|
||
expect(emitted()).toHaveProperty('click') | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"so that..."
couldn't come up with a good wording to explain why waiting for a tick is needed. Any ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to know, too, whether if making fireEvent return a promise is totally necessary due to Vue internals. There might be some alternative that keeps VTL public API as similar as possible to RTL's and DTL's.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about "to allow vue to asynchronously handle the event"?
Maybe it would make sense to link to the docs here: https://vuejs.org/v2/guide/reactivity.html#Async-Update-Queue
Good question on if it is necessary for fireevent to be async. Both React and Preact expose ways of forcing them to flush their update queues synchronously. Maybe it is worth looking into
vm.$forceUpdate
to see if it does what we wantThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! Added.
I'm gonna give
vm.$forceUpdate
(or evennextTick
? who knows) a go. I'd really love to stick to synchronous events.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might also be worth looking into vuejs/vue-test-utils#1137