10000 Separate createElement and JSX tests · gitcommituser/react@cea2c38 · GitHub
[go: up one dir, main page]

Skip to content

Commit cea2c38

Browse files
committed
Separate createElement and JSX tests
This essentially copies all classic element creation tests to the modern JSX tests. The classic tests doesn't use JSX and modern tests do. The idea is that the JSX tests can start dropping dynamic checks once we have Flow support for those features. JSX won't be necessary for dropping dynamic checks. Plain object will also work. Flow will also not be necessary for JSX. However, the tests should test for the suggested combination (JSX + Flow). This also moves some misplaced tests to ReactDOM and Validator. Note that the modern tests uses ES6 classes. I will add separate tests for those. However, these are not guaranteed to have .displayName so all our error messages should check .name if available instead. This should be better abstracted but I just adhoc fix this for now.
1 parent 60b2241 commit cea2c38

File tree

7 files changed

+750
-215
lines changed

7 files changed

+750
-215
lines changed

src/browser/__tests__/ReactDOM-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ describe('ReactDOM', function() {
4747
});
4848
*/
4949

50+
it("allows a DOM element to be used with a string", function() {
51+
var element = React.createElement('div', { className: 'foo' });
52+
var instance = ReactTestUtils.renderIntoDocument(element);
53+
expect(instance.getDOMNode().tagName).toBe('DIV');
54+
});
55+
5056
it("should allow children to be passed as an argument", function() {
5157
var argDiv = ReactTestUtils.renderIntoDocument(
5258
div(null, 'child')

src/classic/element/ReactElementValidator.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ var loggedTypeFailures = {};
4141

4242
var NUMERIC_PROPERTY_REGEX = /^\d+$/;
4343

44+
/**
45+
* Gets the instance's name for use in warnings.
46+
*
47+
* @internal
48+
* @return {?string} Display name or undefined
49+
*/
50+
function getName(instance) {
51+
var publicInstance = instance && instance.getPublicInstance();
52+
if (!publicInstance) {
53+
return undefined;
54+
}
55+
var constructor = publicInstance.constructor;
56+
if (!constructor) {
57+
return undefined;
58+
}
59+
return constructor.displayName || constructor.name || undefined;
60+
}
61+
4462
/**
4563
* Gets the current owner's displayName for use in warnings.
4664
*
@@ -50,7 +68,7 @@ var NUMERIC_PROPERTY_REGEX = /^\d+$/;
5068
function getCurrentOwnerDisplayName() {
5169
var current = ReactCurrentOwner.current;
5270
return (
53-
current && current.getPublicInstance().constructor.displayName || undefined
71+
current && getName(current) || undefined
5472
);
5573
}
5674

@@ -110,7 +128,7 @@ function validatePropertyKey(name, element, parentType) {
110128
*/
111129
function warnAndMonitorForKeyUse(warningID, message, element, parentType) {
112130
var ownerName = getCurrentOwnerDisplayName();
113-
var parentName = parentType.displayName;
131+
var parentName = parentType.displayName || parentType.name;
114132

115133
var useName = ownerName || parentName;
116134
var memoizer = ownerHasKeyUseWarning[warningID];
@@ -131,8 +149,7 @@ function warnAndMonitorForKeyUse(warningID, message, element, parentType) {
131149
element._owner &&
132150
element._owner !== ReactCurrentOwner.current) {
133151
// Name of the component that originally created this child.
134-
childOwnerName =
135-
element._owner.getPublicInstance().constructor.displayName;
152+
childOwnerName = getName(element._owner);
136153

137154
message += ` It was passed a child from ${childOwnerName}.`;
138155
}
@@ -262,7 +279,7 @@ var ReactElementValidator = {
262279
}
263280

264281
if (type) {
265-
var name = type.displayName;
282+
var name = type.displayName || type.name;
266283
if (type.propTypes) {
267284
checkPropTypes(
268285
name,

0 commit comments

Comments
 (0)
0