-
Notifications
You must be signed in to change notification settings - Fork 275
Feat/fire event api #11
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
9 commits
Select commit
Hold shift + click to select a range
ebab5c2
feat: Initial implementation of fireEvent API
Esemesek df677f4
feat: Add tests to fireEvent API
Esemesek 16d0a2b
Merge remote-tracking branch 'origin' into feat/fire-event-api
Esemesek 404b4dd
Merge remote-tracking branch 'origin' into feat/fire-event-api
Esemesek f4b999a
Feat Review fixes
Esemesek 44f2d2e
feat fireEvent doc update
Esemesek a4bf24f
feat Documentation tweaks
Esemesek fc071de
update example
thymikee 0098fef
use ErrorWithStack
thymikee 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @flow | ||
export const View = (props: *) => props.children; | ||
export const ScrollView = (props: *) => props.children; | ||
export const Text = (props: *) => props.children; | ||
export const TextInput = () => null; | ||
export const TouchableOpacity = (props: *) => props.children; |
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,132 @@ | ||
import React from 'react'; | ||
import { | ||
View, | ||
TouchableOpacity, | ||
Text, | ||
ScrollView, | ||
TextInput, | ||
} from '../__mocks__/reactNativeMock'; | ||
import fireEvent from '../fireEvent'; | ||
import { render } from '..'; | ||
Esemesek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const OnPressComponent = ({ onPress }) => ( | ||
<View> | ||
<TouchableOpacity onPress={onPress} testID="button"> | ||
<Text testID="text-button">Press me</Text> | ||
</TouchableOpacity> | ||
</View> | ||
); | ||
|
||
const WithoutEventComponent = () => ( | ||
<View> | ||
<Text testID="text">Content</Text> | ||
</View> | ||
); | ||
|
||
const CustomEventComponent = ({ onCustomEvent }) => ( | ||
<TouchableOpacity onPress={onCustomEvent}> | ||
<Text>Click me</Text> | ||
</TouchableOpacity> | ||
); | ||
|
||
describe('fireEvent', () => { | ||
test('should invoke specified event', () => { | ||
const onPressMock = jest.fn(); | ||
const { getByTestId } = render(<OnPressComponent onPress={onPressMock} />); | ||
|
||
fireEvent(getByTestId('button'), 'press'); | ||
|
||
expect(onPressMock).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should invoke specified event on parent element', () => { | ||
const onPressMock = jest.fn(); | ||
const { getByTestId } = render(<OnPressComponent onPress={onPressMock} />); | ||
|
||
fireEvent(getByTestId('text-button'), 'press'); | ||
|
||
expect(onPressMock).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should throw an Error when event handler was not found', () => { | ||
const { getByTestId } = render(<WithoutEventComponent />); | ||
|
||
expect(() => fireEvent(getByTestId('text'), 'press')).toThrowError( | ||
'No handler function found for event: press' | ||
); | ||
}); | ||
|
||
test('should invoke event with custom name', () => { | ||
const handlerMock = jest.fn(); | ||
const EVENT_DATA = 'event data'; | ||
|
||
const { getByTestId } = render( | ||
<View> | ||
<CustomEventComponent testID="custom" onCustomEvent={handlerMock} /> | ||
</View> | ||
); | ||
|
||
fireEvent(getByTestId('custom'), 'customEvent', EVENT_DATA); | ||
|
||
expect(handlerMock).toHaveBeenCalledWith(EVENT_DATA); | ||
}); | ||
}); | ||
|
||
test('fireEvent.press', () => { | ||
const onPressMock = jest.fn(); | ||
const { getByTestId } = render(<OnPressComponent onPress={onPressMock} />); | ||
|
||
fireEvent.press(getByTestId('text-button')); | ||
|
||
expect(onPressMock).toHaveBeenCalled(); | ||
}); | ||
|
||
test('fireEvent.scroll', () => { | ||
const onScrollMock = jest.fn(); | ||
const eventData = { | ||
nativeEvent: { | ||
contentOffset: { | ||
y: 200, | ||
}, | ||
}, | ||
}; | ||
|
||
const { getByTestId } = render( | ||
<ScrollView testID="scroll-view" onScroll={onScrollMock}> | ||
<Text>XD</Text> | ||
</ScrollView> | ||
); | ||
|
||
fireEvent.scroll(getByTestId('scroll-view'), eventData); | ||
Esemesek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
expect(onScrollMock).toHaveBeenCalledWith(eventData); | ||
}); | ||
|
||
test('fireEvent.doublePress', () => { | ||
const onDoublePressMock = jest.fn(); | ||
|
||
const { getByTestId } = render( | ||
<TouchableOpacity onDoublePress={onDoublePressMock}> | ||
<Text testID="button-text">Click me</Text> | ||
</TouchableOpacity> | ||
); | ||
|
||
fireEvent.doublePress(getByTestId('button-text')); | ||
|
||
expect(onDoublePressMock).toHaveBeenCalled(); | ||
}); | ||
|
||
test('fireEvent.changeText', () => { | ||
const onChangeTextMock = jest.fn(); | ||
const CHANGE_TEXT = 'content'; | ||
|
||
const { getByTestId } = render( | ||
<View> | ||
<TextInput testID="text-input" onChangeText={onChangeTextMock} /> | ||
</View> | ||
); | ||
|
||
fireEvent.changeText(getByTestId('text-input'), CHANGE_TEXT); | ||
|
||
expect(onChangeTextMock).toHaveBeenCalledWith(CHANGE_TEXT); | ||
}); |
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,68 @@ | ||
// @flow | ||
import ErrorWithStack from './helpers/errorWithStack'; | ||
|
||
const findEventHandler = (element: ReactTestInstance, eventName: string) => { | ||
const eventHandler = toEventHandlerName(eventName); | ||
|
||
if (typeof element.props[eventHandler] === 'function') { | ||
return element.props[eventHandler]; | ||
} | ||
|
||
if (element.parent === null) { | ||
throw new ErrorWithStack( | ||
`No handler function found for event: ${eventName}`, | ||
invokeEvent | ||
); | ||
} | ||
|
||
return findEventHandler(element.parent, eventName); | ||
}; | ||
|
||
const invokeEvent = ( | ||
element: ReactTestInstance, | ||
eventName: string, | ||
data?: * | ||
) => { | ||
const handler = findEventHandler(element, eventName); | ||
|
||
return handler(data); | ||
}; | ||
|
||
const toEventHandlerName = (eventName: string) => | ||
`on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`; | ||
|
||
const pressHandler = (element: ReactTestInstance) => | ||
invokeEvent(element, 'press'); | ||
const doublePressHandler = (element: ReactTestInstance) => | ||
invokeEvent(element, 'doublePress'); | ||
const changeTextHandler = (element: ReactTestInstance, data?: *) => | ||
invokeEvent(element, 'changeText', data); | ||
const scrollHandler = (element: ReactTestInstance, data?: *) => | ||
invokeEvent(element, 'scroll', data); | ||
|
||
const EVENTS = [ | ||
{ | ||
name: 'press', | ||
handler: pressHandler, | ||
}, | ||
{ | ||
name: 'doublePress', | ||
handler: doublePressHandler, | ||
}, | ||
{ | ||
name: 'changeText', | ||
handler: changeTextHandler, | ||
}, | ||
{ | ||
name: 'scroll', | ||
handler: scrollHandler, | ||
}, | ||
]; | ||
|
||
const fireEvent = invokeEvent; | ||
|
||
EVENTS.forEach(event => { | ||
fireEvent[event.name] = event.handler; | ||
}); | ||
|
||
export default fireEvent; |
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.
Uh oh!
There was an error while loading. Please reload this page.