@samitouri / QOS-React-2 / commits / 1ee7f8131c

Let components specify tag for ReactTestUtils.mockComponent to use.

If you're writing a test that involves a mocked component that normally returns a `<li>` tag from its `render` method as the root element, mocking it with a dummy `<div>` probably won't work, so you'll want to set MyListItemComponent.mockTagName = 'li'; The change to the `.mockImplementation` line makes sense because `ConvenienceConstructor` is more or less synonymous (for these purposes) with the wrapper that was previously used: function() { // This `this` used to be `null`, technically, but // ConvenienceConstructor doesn't pay attention to `this` anyway. return ConvenienceConstructor.apply(this, arguments); }

Ben Newman committed Jan 2, 2014 at 18:31 UTC 1ee7f8131c1702e3584bbe51d94b503089b8f3bf
1 file changed +11 -5
src/test/ReactTestUtils.js
+11 -5
@@ -197,18 +197,24 @@ var ReactTestUtils = {
197 * useful methods that allow it to be used as a dummy React component.
198 * Instead of rendering as usual, the component will become a simple
199 * <div> containing any provided children.
200 + *
201 + * @param {object} module the mock function object exported from a
202 + * module that defines the component to be mocked
203 + * @param {?string} mockTagName optional dummy root tag name to return
204 + * from render method (overrides
205 + * module.mockTagName if provided)
206 + * @return {object} the ReactTestUtils object (for chaining)
207 */
201 - mockComponent: function(module) {
208 + mockComponent: function(module, mockTagName) {
209 var ConvenienceConstructor = React.createClass({
210 render: function() {
204 - return ReactDOM.div(null, this.props.children);
211 + var mockTagName = mockTagName || module.mockTagName || "div";
212 + return ReactDOM[mockTagName](null, this.props.children);
213 }
214 });
215
216 copyProperties(module, ConvenienceConstructor);
209 - module.mockImplementation(function() {
210 - return ConvenienceConstructor.apply(null, arguments);
211 - });
217 + module.mockImplementation(ConvenienceConstructor);
218
219 return this;
220 },