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

Add event name mapping for animation events #1569

Merged
merged 2 commits into from
Jul 5, 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 @@ -148,7 +148,7 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event);
const mappedEvent = mapNativeEventNames(event, { animation: true });
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event);
const mappedEvent = mapNativeEventNames(event, { animation: true });
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event);
const mappedEvent = mapNativeEventNames(event, { animation: true });
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
Expand Down
9 changes: 8 additions & 1 deletion packages/enzyme-adapter-utils/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import createRenderWrapper from './createRenderWrapper';

export { createMountWrapper, createRenderWrapper };

export function mapNativeEventNames(event) {
export function mapNativeEventNames(event, {
animation = false, // should be true for React 15+
} = {}) {
const nativeToReactEventMap = {
compositionend: 'compositionEnd',
compositionstart: 'compositionStart',
Expand Down Expand Up @@ -42,6 +44,11 @@ export function mapNativeEventNames(event) {
mouseenter: 'mouseEnter',
mouseleave: 'mouseLeave',
transitionend: 'transitionEnd',
...(animation && {
animationstart: 'animationStart',
animationiteration: 'animationIteration',
animationend: 'animationEnd',
}),
};

return nativeToReactEventMap[event] || event;
Expand Down
12 changes: 12 additions & 0 deletions packages/enzyme-test-suite/test/Utils-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,18 @@ describe('Utils', () => {
expect(result).to.equal('dragEnter');
});
});

describe('conditionally supported events', () => {
it('ignores unsupported events', () => {
const result = mapNativeEventNames('animationiteration');
expect(result).to.equal('animationiteration');
});

it('transforms events when supported', () => {
const result = mapNativeEventNames('animationiteration', { animation: true });
expect(result).to.equal('animationIteration');
});
});
});

describe('displayNameOfNode', () => {
Expand Down