Skip to content

Commit

Permalink
warn when a propName looks like a mistyped event
Browse files Browse the repository at this point in the history
closes #3548
  • Loading branch information
bloodyowl committed Jul 1, 2015
1 parent 9f0042c commit 71afd5a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/renderers/dom/shared/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ function checkAndWarnForMutatedStyle(style1, style2, component) {
*/
var BackendIDOperations = null;

var eventTypes = {};

if (__DEV__) {
Object.keys(EventConstants.topLevelTypes)
.forEach(function(key) {
var eventType = key.replace(/^top/, 'on');
eventTypes[eventType.toLowerCase()] = eventType;
});
}
/**
* @param {object} component
* @param {?object} props
Expand Down Expand Up @@ -267,6 +276,18 @@ function assertValidProps(component, props) {
'not a string. For example, style={{marginRight: spacing + \'em\'}} when ' +
'using JSX.'
);
if (__DEV__) {
Object.keys(props).forEach(function(propName) {
var lowerCasePropName = propName.toLowerCase();
warning(
!eventTypes.hasOwnProperty(lowerCasePropName) ||
eventTypes[lowerCasePropName] === propName,
'`%s` seems to be an invalid event name, did you mean `%s`?',
propName,
eventTypes[lowerCasePropName]
);
});
}
}

function enqueuePutListener(id, registrationName, listener, transaction) {
Expand Down
34 changes: 34 additions & 0 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,40 @@ describe('ReactDOMComponent', function() {
});
});

describe('wrong event name case warning', function() {
var React;
var ReactTestUtils;

beforeEach(() => {
React = require('React');
ReactTestUtils = require('ReactTestUtils');
});

it('should warn when an event is lowercased', () => {
spyOn(console, 'error');
ReactTestUtils.renderIntoDocument(<div onclick={function() {}} />);
expect(console.error.calls.length).toBe(1);
expect(console.error.mostRecentCall.args[0]).toBe(
'Warning: `onclick` seems to be an invalid event name, did you mean `onClick`?'
);
});

it('should warn when an event has the wrong case', () => {
spyOn(console, 'error');
ReactTestUtils.renderIntoDocument(<div onMouseup={function() {}} />);
expect(console.error.calls.length).toBe(1);
expect(console.error.mostRecentCall.args[0]).toBe(
'Warning: `onMouseup` seems to be an invalid event name, did you mean `onMouseUp`?'
);
});

it('should not warn when a prop is not a supported, mistyped event type', () => {
spyOn(console, 'error');
ReactTestUtils.renderIntoDocument(<div one={function() {}} />);
expect(console.error.calls.length).toBe(0);
});
});

describe('tag sanitization', function() {
it('should throw when an invalid tag name is used', () => {
var React = require('React');
Expand Down

0 comments on commit 71afd5a

Please sign in to comment.