-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add fireEvent from dom-testing-library #48
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 all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8a10c39
add fireEvent from dom-testing-library
2fda80f
add contributor
108e295
added docs
7f7daa6
use document for synthetic events
2381249
added renderIntoDocument and clearDocument
6cbbce9
added docs
cc2310b
update tests and README.md
eb482e7
fix typos
32f7560
fix typo
8304913
update readme
9006f0e
fix typo
04fec2e
use beforeEach
a85fad9
added cleanup, removed clearDocument
e210c0c
use afterEach for cleanup
404e14b
Update README.md
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
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,156 @@ | ||
import React from 'react' | ||
import {renderIntoDocument, cleanup, fireEvent} from '../' | ||
|
||
const eventTypes = [ | ||
{ | ||
type: 'Clipboard', | ||
events: ['copy', 'paste'], | ||
elementType: 'input', | ||
}, | ||
{ | ||
type: 'Composition', | ||
events: ['compositionEnd', 'compositionStart', 'compositionUpdate'], | ||
elementType: 'input', | ||
}, | ||
{ | ||
type: 'Keyboard', | ||
events: ['keyDown', 'keyPress', 'keyUp'], | ||
elementType: 'input', | ||
init: {keyCode: 13}, | ||
}, | ||
{ | ||
type: 'Focus', | ||
events: ['focus', 'blur'], | ||
elementType: 'input', | ||
}, | ||
{ | ||
type: 'Form', | ||
events: ['focus', 'blur'], | ||
elementType: 'input', | ||
}, | ||
{ | ||
type: 'Focus', | ||
events: ['change', 'input', 'invalid'], | ||
elementType: 'input', | ||
}, | ||
{ | ||
type: 'Focus', | ||
events: ['submit'], | ||
elementType: 'form', | ||
}, | ||
{ | ||
type: 'Mouse', | ||
events: [ | ||
'click', | ||
'contextMenu', | ||
'doubleClick', | ||
'drag', | ||
'dragEnd', | ||
'dragEnter', | ||
'dragExit', | ||
'dragLeave', | ||
'dragOver', | ||
'dragStart', | ||
'drop', | ||
'mouseDown', | ||
'mouseEnter', | ||
'mouseLeave', | ||
'mouseMove', | ||
'mouseOut', | ||
'mouseOver', | ||
'mouseUp', | ||
], | ||
elementType: 'button', | ||
}, | ||
{ | ||
type: 'Selection', | ||
events: ['select'], | ||
elementType: 'input', | ||
}, | ||
{ | ||
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', | ||
}, | ||
] | ||
|
||
afterEach(cleanup) | ||
|
||
eventTypes.forEach(({type, events, elementType, init}) => { | ||
describe(`${type} Events`, () => { | ||
events.forEach(eventName => { | ||
const propName = `on${eventName.charAt(0).toUpperCase()}${eventName.slice( | ||
1, | ||
)}` | ||
|
||
it(`triggers ${propName}`, () => { | ||
const ref = React.createRef() | ||
const spy = jest.fn() | ||
|
||
renderIntoDocument( | ||
React.createElement(elementType, { | ||
[propName]: spy, | ||
ref, | ||
}), | ||
) | ||
|
||
fireEvent[eventName](ref.current, init) | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) | ||
}) |
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,29 @@ | ||
import React from 'react' | ||
import {renderIntoDocument, cleanup} from '../' | ||
|
||
afterEach(cleanup) | ||
|
||
it('renders button into document', () => { | ||
const ref = React.createRef() | ||
const {container} = renderIntoDocument(<div ref={ref} />) | ||
expect(container.firstChild).toBe(ref.current) | ||
}) | ||
|
||
it('cleansup document', () => { | ||
const spy = jest.fn() | ||
|
||
class Test extends React.Component { | ||
componentWillUnmount() { | ||
spy() | ||
} | ||
|
||
render() { | ||
return <div /> | ||
} | ||
} | ||
|
||
renderIntoDocument(<Test />) | ||
cleanup() | ||
expect(document.body.innerHTML).toBe('') | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
}) |
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
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.
Let's update the docs here to document
renderIntoDocument
andclearDocument
(in that order). Then update thefireEvent
docs to explain why it's important to use those helpers.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.
oh i just added this, but added them below
render
and beforeSimulate