Skip to content

Generate event object for fireEvent #1171

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

Closed
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 116 additions & 45 deletions src/__tests__/fireEvent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ const CustomEventComponentWithCustomName = ({
);

describe('fireEvent', () => {
test('QUESTION: is there a kind of event that takes multiple arguments?', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK events by convention take a single argument of Event type or subtype. We also have onChangeText which gets a single string value, but that seems to be simplified version of onChange. I would assume that we can focus only on handling single argument. That should simplify the typing, while we can always refactor if there would be any such event.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this info! BTW I noticed that I didn't clearly link to our additional questions from here. Here they are: #714 (comment)

const handlerMock = jest.fn();

const { getByText } = render(
<View>
<CustomEventComponent onCustomEvent={handlerMock} />
</View>
);

fireEvent(
getByText('Custom event component'),
'customEvent',
'some_argument_1',
'some_argument_2'
);

expect(handlerMock).toHaveBeenCalledWith(
'some_argument_1',
'some_argument_2'
);
});

test('should invoke specified event', () => {
const onPressMock = jest.fn();
const { getByText } = render(
Expand Down Expand Up @@ -104,64 +126,113 @@ describe('fireEvent', () => {
});
});

test('fireEvent.press', () => {
const onPressMock = jest.fn();
const text = 'Fireevent press';
const eventData = {
nativeEvent: {
pageX: 20,
pageY: 30,
},
};
const { getByText } = render(
<OnPressComponent onPress={onPressMock} text={text} />
);
describe('fireEvent.press', () => {
test('should pass along provided event data', () => {
const onPressMock = jest.fn();
const text = 'Fireevent press';
const eventData = {
nativeEvent: {
pageX: 20,
pageY: 30,
},
};
const { getByText } = render(
<OnPressComponent onPress={onPressMock} text={text} />
);

fireEvent.press(getByText(text), eventData);
fireEvent.press(getByText(text), eventData);

expect(onPressMock).toHaveBeenCalledWith(eventData);
expect(onPressMock).toHaveBeenCalledWith(eventData);
});

test('should pass a generated event object by default', () => {
const onPressMock = jest.fn();
const text = 'Fireevent press';
const { getByText } = render(
<OnPressComponent onPress={onPressMock} text={text} />
);

fireEvent.press(getByText(text));

expect(onPressMock).toHaveBeenCalledWith({ someKey: 'value' });
});
});

test('fireEvent.scroll', () => {
const onScrollMock = jest.fn();
const eventData = {
nativeEvent: {
contentOffset: {
y: 200,
describe('fireEvent.scroll', () => {
test('should pass along provided event data', () => {
const onScrollMock = jest.fn();
const eventData = {
nativeEvent: {
contentOffset: {
y: 200,
},
},
},
};
};

const { getByText } = render(
<ScrollView onScroll={onScrollMock}>
<Text>XD</Text>
</ScrollView>
);
const { getByText } = render(
<ScrollView onScroll={onScrollMock}>
<Text>XD</Text>
</ScrollView>
);

fireEvent.scroll(getByText('XD'), eventData);

expect(onScrollMock).toHaveBeenCalledWith(eventData);
});
test('should pass a generated event object by default', () => {
const onScrollMock = jest.fn();
const eventData = { someKey: 'value' };

const { getByText } = render(
<ScrollView onScroll={onScrollMock}>
<Text>XD</Text>
</ScrollView>
);

fireEvent.scroll(getByText('XD'), eventData);
fireEvent.scroll(getByText('XD'));

expect(onScrollMock).toHaveBeenCalledWith(eventData);
expect(onScrollMock).toHaveBeenCalledWith(eventData);
});
});

test('fireEvent.changeText', () => {
const onChangeTextMock = jest.fn();
const CHANGE_TEXT = 'content';
describe('fireEvent.changeText', () => {
test('should pass the provided text', () => {
const onChangeTextMock = jest.fn();
const CHANGE_TEXT = 'content';

const { getByPlaceholderText } = render(
<View>
<TextInput
placeholder="Customer placeholder"
onChangeText={onChangeTextMock}
/>
</View>
);
const { getByPlaceholderText } = render(
<View>
<TextInput
placeholder="Customer placeholder"
onChangeText={onChangeTextMock}
/>
</View>
);

fireEvent.changeText(
getByPlaceholderText('Customer placeholder'),
CHANGE_TEXT
);
fireEvent.changeText(
getByPlaceholderText('Customer placeholder'),
CHANGE_TEXT
);

expect(onChangeTextMock).toHaveBeenCalledWith(CHANGE_TEXT);
});

expect(onChangeTextMock).toHaveBeenCalledWith(CHANGE_TEXT);
test('what if no text is provided', () => {
const onChangeTextMock = jest.fn();

const { getByPlaceholderText } = render(
<View>
<TextInput
placeholder="Customer placeholder"
onChangeText={onChangeTextMock}
/>
</View>
);

fireEvent.changeText(getByPlaceholderText('Customer placeholder'));

expect(onChangeTextMock).toHaveBeenCalledWith();
});
});

test('custom component with custom event name', () => {
Expand Down
9 changes: 8 additions & 1 deletion src/fireEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,17 @@ const invokeEvent = (
return;
}

// this is just a placeholder and needs to be a more realistic object
const generatedEventObject = { someKey: 'value' };

let defaultCallbackValues =
eventName === 'changeText' ? [] : [generatedEventObject];
const handlerCallbackValues = data.length > 0 ? data : defaultCallbackValues;

let returnValue;

act(() => {
returnValue = handler(...data);
returnValue = handler(...handlerCallbackValues);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally we would like to follow DTL's fireEvent implementation, with adjustments necessary to account for differences between RN and React DOM.

  1. They have event map record which holds mapping from event name to event object constructor name and some default init props: https://github.com/testing-library/dom-testing-library/blob/main/src/event-map.js
  2. They only apply default event iff someone uses fireEvent.xxx. They do not seem to apply in when using fireEvent('xxx'):
  1. Important parts seems to be dispatching that event in context of given node. See: https://github.com/testing-library/dom-testing-library/blob/edffb7c5ec2e4afd7f6bedf842c669ddbfb73297/src/events.js#L17

In DTL they have element.displatchEvent, but in ourcase it seems that we have to manually assign target property of event, since our ReactTestInstance does not implement EventTarget which achieves that effect automatically for RTL.


return returnValue;
Expand Down