Skip to content

Commit

Permalink
Add test for async event dispatching (#15300)
Browse files Browse the repository at this point in the history
Verified that a variant of this test fails as follows when the
`context.withAsyncDispatching` function is excluded (i.e., reproduces the
issue).

    Expected value to equal:
      ["press", "longpress", "longpresschange"]
    Received:
      ["press", "longpress", "longpress", "longpresschange"]
  • Loading branch information
necolas authored and trueadm committed Apr 3, 2019
1 parent 38fa840 commit fc6a9f1
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,67 @@ describe('DOMEventResponderSystem', () => {

expect(eventLog).toEqual(['magic event fired', 'magicclick']);
});

it('async event dispatching works', () => {
let eventLog = [];
const buttonRef = React.createRef();

const LongPressEventComponent = createReactEventComponent(
['click'],
(context, props) => {
const pressEvent = {
listener: props.onPress,
target: context.eventTarget,
type: 'press',
};
context.dispatchEvent(pressEvent, {discrete: true});

setTimeout(
() =>
context.withAsyncDispatching(() => {
if (props.onLongPress) {
const longPressEvent = {
listener: props.onLongPress,
target: context.eventTarget,
type: 'longpress',
};
context.dispatchEvent(longPressEvent, {discrete: true});
}

if (props.onLongPressChange) {
const longPressChangeEvent = {
listener: props.onLongPressChange,
target: context.eventTarget,
type: 'longpresschange',
};
context.dispatchEvent(longPressChangeEvent, {discrete: true});
}
}),
500,
);
},
);

function log(msg) {
eventLog.push(msg);
}

const Test = () => (
<LongPressEventComponent
onPress={() => log('press')}
onLongPress={() => log('longpress')}
onLongPressChange={() => log('longpresschange')}>
<button ref={buttonRef}>Click me!</button>
</LongPressEventComponent>
);

ReactDOM.render(<Test />, container);

// Clicking the button should trigger the event responder handleEvent()
let buttonElement = buttonRef.current;
dispatchClickEvent(buttonElement);
jest.runAllTimers();

expect(eventLog).toEqual(['press', 'longpress', 'longpresschange']);
});
});

0 comments on commit fc6a9f1

Please sign in to comment.