Skip to content
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

[New] add pointer events support #1753

Merged
merged 3 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ function instanceToTree(inst) {
};
}

const eventOptions = { animation: true };

class ReactFifteenFourAdapter extends EnzymeAdapter {
constructor() {
super();
Expand Down Expand Up @@ -153,7 +155,7 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, { animation: true });
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
Expand Down Expand Up @@ -201,7 +203,7 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
};
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event)];
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
withSetStateAllowed(() => {
// TODO(lmr): create/use synthetic events
Expand Down
6 changes: 4 additions & 2 deletions packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ function instanceToTree(inst) {
};
}

const eventOptions = { animation: true };

class ReactFifteenAdapter extends EnzymeAdapter {
constructor() {
super();
Expand Down Expand Up @@ -153,7 +155,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, { animation: true });
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
Expand Down Expand Up @@ -201,7 +203,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
};
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event)];
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
withSetStateAllowed(() => {
// TODO(lmr): create/use synthetic events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ function nodeToHostNode(_node) {
return ReactDOM.findDOMNode(node.instance);
}

const eventOptions = { animation: true };

class ReactSixteenOneAdapter extends EnzymeAdapter {
constructor() {
super();
Expand Down Expand Up @@ -218,7 +220,7 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, { animation: true });
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
Expand Down Expand Up @@ -283,7 +285,7 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
};
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event)];
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
withSetStateAllowed(() => {
// TODO(lmr): create/use synthetic events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ function nodeToHostNode(_node) {
return ReactDOM.findDOMNode(node.instance);
}

const eventOptions = { animation: true };

class ReactSixteenTwoAdapter extends EnzymeAdapter {
constructor() {
super();
Expand Down Expand Up @@ -220,7 +222,7 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, { animation: true });
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
Expand Down Expand Up @@ -285,7 +287,7 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
};
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event)];
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
withSetStateAllowed(() => {
// TODO(lmr): create/use synthetic events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ function nodeToHostNode(_node) {
return ReactDOM.findDOMNode(node.instance);
}

const eventOptions = { animation: true };

class ReactSixteenThreeAdapter extends EnzymeAdapter {
constructor() {
super();
Expand Down Expand Up @@ -238,7 +240,7 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, { animation: true });
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
Expand Down Expand Up @@ -303,7 +305,7 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
};
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event)];
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
withSetStateAllowed(() => {
// TODO(lmr): create/use synthetic events
Expand Down
14 changes: 9 additions & 5 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ function nodeToHostNode(_node) {
return ReactDOM.findDOMNode(node.instance);
}

const eventOptions = {
animation: true,
pointerEvents: true, // 16.4+
};

class ReactSixteenAdapter extends EnzymeAdapter {
constructor() {
super();
Expand Down Expand Up @@ -239,12 +244,11 @@ class ReactSixteenAdapter extends EnzymeAdapter {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, { animation: true });
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
}
// eslint-disable-next-line react/no-find-dom-node
eventFn(nodeToHostNode(node), mock);
},
batchedUpdates(fn) {
Expand Down Expand Up @@ -304,7 +308,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
};
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event)];
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
withSetStateAllowed(() => {
// TODO(lmr): create/use synthetic events
Expand Down Expand Up @@ -340,7 +344,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {

// Provided a bag of options, return an `EnzymeRenderer`. Some options can be implementation
// specific, like `attach` etc. for React, but not part of this interface explicitly.
// eslint-disable-next-line class-methods-use-this, no-unused-vars
// eslint-disable-next-line class-methods-use-this
createRenderer(options) {
switch (options.mode) {
case EnzymeAdapter.MODES.MOUNT: return this.createMountRenderer(options);
Expand All @@ -354,7 +358,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
// eslint-disable-next-line class-methods-use-this, no-unused-vars
// eslint-disable-next-line class-methods-use-this
nodeToElement(node) {
if (!node || typeof node !== 'object') return null;
return React.createElement(node.type, propsWithKeysAndRef(node));
Expand Down
17 changes: 15 additions & 2 deletions packages/enzyme-adapter-utils/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { createMountWrapper, createRenderWrapper };

export function mapNativeEventNames(event, {
animation = false, // should be true for React 15+
pointerEvents = false, // should be true for React 16.4+
} = {}) {
const nativeToReactEventMap = {
compositionend: 'compositionEnd',
Expand Down Expand Up @@ -50,15 +51,27 @@ export function mapNativeEventNames(event, {
animationiteration: 'animationIteration',
animationend: 'animationEnd',
}),
...(pointerEvents && {
pointerdown: 'pointerDown',
pointermove: 'pointerMove',
pointerup: 'pointerUp',
pointercancel: 'pointerCancel',
gotpointercapture: 'gotPointerCapture',
lostpointercapture: 'lostPointerCapture',
pointerenter: 'pointerEnter',
pointerleave: 'pointerLeave',
pointerover: 'pointerOver',
pointerout: 'pointerOut',
}),
};

return nativeToReactEventMap[event] || event;
}

// 'click' => 'onClick'
// 'mouseEnter' => 'onMouseEnter'
export function propFromEvent(event) {
const nativeEvent = mapNativeEventNames(event);
export function propFromEvent(event, eventOptions = {}) {
const nativeEvent = mapNativeEventNames(event, eventOptions);
return `on${nativeEvent[0].toUpperCase()}${nativeEvent.slice(1)}`;
}

Expand Down
60 changes: 60 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,66 @@ describeWithDOM('mount', () => {
expect(spy).to.have.property('callCount', 1);
});
});

describeIf(is('>= 15'), 'animation events', () => {
it('should convert lowercase events to React camelcase', () => {
const spy = sinon.spy();
class Foo extends React.Component {
render() {
return (
<a onAnimationIteration={spy}>foo</a>
);
}
}

const wrapper = mount(<Foo />);

wrapper.simulate('animationiteration');
expect(spy).to.have.property('callCount', 1);
});

it('should convert lowercase events to React camelcase in stateless components', () => {
const spy = sinon.spy();
const Foo = () => (
<a onAnimationIteration={spy}>foo</a>
);

const wrapper = mount(<Foo />);

wrapper.simulate('animationiteration');
expect(spy).to.have.property('callCount', 1);
});
});

describeIf(is('>= 16.4'), 'pointer events', () => {
it('should convert lowercase events to React camelcase', () => {
const spy = sinon.spy();
class Foo extends React.Component {
render() {
return (
<a onGotPointerCapture={spy}>foo</a>
);
}
}

const wrapper = mount(<Foo />);

wrapper.simulate('gotpointercapture');
expect(spy).to.have.property('callCount', 1);
});

it('should convert lowercase events to React camelcase in stateless components', () => {
const spy = sinon.spy();
const Foo = () => (
<a onGotPointerCapture={spy}>foo</a>
);

const wrapper = mount(<Foo />);

wrapper.simulate('gotpointercapture');
expect(spy).to.have.property('callCount', 1);
});
});
});

it('should be batched updates', () => {
Expand Down
60 changes: 60 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,66 @@ describe('shallow', () => {
expect(spy).to.have.property('callCount', 1);
});
});

describeIf(is('>= 15'), 'animation events', () => {
it('should convert lowercase events to React camelcase', () => {
const spy = sinon.spy();
class Foo extends React.Component {
render() {
return (
<a onAnimationIteration={spy}>foo</a>
);
}
}

const wrapper = shallow(<Foo />);

wrapper.simulate('animationiteration');
expect(spy).to.have.property('callCount', 1);
});

it('should convert lowercase events to React camelcase in stateless components', () => {
const spy = sinon.spy();
const Foo = () => (
<a onAnimationIteration={spy}>foo</a>
);

const wrapper = shallow(<Foo />);

wrapper.simulate('animationiteration');
expect(spy).to.have.property('callCount', 1);
});
});

describeIf(is('>= 16.4'), 'pointer events', () => {
it('should convert lowercase events to React camelcase', () => {
const spy = sinon.spy();
class Foo extends React.Component {
render() {
return (
<a onGotPointerCapture={spy}>foo</a>
);
}
}

const wrapper = shallow(<Foo />);

wrapper.simulate('gotpointercapture');
expect(spy).to.have.property('callCount', 1);
});

it('should convert lowercase events to React camelcase in stateless components', () => {
const spy = sinon.spy();
const Foo = () => (
<a onGotPointerCapture={spy}>foo</a>
);

const wrapper = shallow(<Foo />);

wrapper.simulate('gotpointercapture');
expect(spy).to.have.property('callCount', 1);
});
});
});

itIf(BATCHING, 'should be batched updates', () => {
Expand Down
Loading