-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fireEvent): add fireEvent from dom-testing-library (#48)
* add fireEvent from dom-testing-library * add contributor * added docs * use document for synthetic events * added renderIntoDocument and clearDocument * added docs * update tests and README.md * fix typos * fix typo * update readme * fix typo * use beforeEach * added cleanup, removed clearDocument * use afterEach for cleanup * Update README.md
- Loading branch information
Showing
5 changed files
with
308 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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