@samitouri / QOS-React-2 / commits / c9fb5b258e

Add toBeTextComponentWithValue to reactComponentExpect

This was previously possible by looking at renderedChildren and props but this feels better.

Paul O’Shannessy committed Dec 2, 2014 at 17:52 UTC c9fb5b258ee40320aa14aa010b1737021226b7c7
3 files changed +52 -2
src/core/__tests__/ReactMultiChildText-test.js
+1 -1
@@ -70,7 +70,7 @@ var expectChildren = function(d, children) {
70 if (typeof child === 'string') {
71 reactComponentExpect(d)
72 .expectRenderedChildAt(i)
73 - .toBeTextComponent();
73 + .toBeTextComponentWithValue(child);
74
75 textNode = d.getDOMNode().childNodes[i].firstChild;
76
src/test/__tests__/reactComponentExpect-test.js new
+49
@@ -0,0 +1,49 @@
1 +/**
2 + * Copyright 2013-2014, Facebook, Inc.
3 + * All rights reserved.
4 + *
5 + * This source code is licensed under the BSD-style license found in the
6 + * LICENSE file in the root directory of this source tree. An additional grant
7 + * of patent rights can be found in the PATENTS file in the same directory.
8 + *
9 + * @emails react-core
10 + */
11 +
12 +"use strict";
13 +
14 +var React;
15 +var ReactTestUtils;
16 +var reactComponentExpect;
17 +
18 +var mocks;
19 +var warn;
20 +
21 +describe('reactComponentExpect', function() {
22 +
23 + beforeEach(function() {
24 + React = require('React');
25 + ReactTestUtils = require('ReactTestUtils');
26 + reactComponentExpect = require('reactComponentExpect');
27 + });
28 +
29 + it('should detect text components', function() {
30 + var SomeComponent = React.createClass({
31 + render: function() {
32 + return (
33 + <div>
34 + <div>This is a div</div>
35 + {'This is text'}
36 + </div>
37 + );
38 + }
39 + });
40 +
41 + var component = ReactTestUtils.renderIntoDocument(<SomeComponent />);
42 +
43 + reactComponentExpect(component)
44 + .expectRenderedChild()
45 + .expectRenderedChildAt(1)
46 + .toBeTextComponentWithValue('This is text');
47 + });
48 +});
49 +
src/test/reactComponentExpect.js
+2 -1
@@ -139,9 +139,10 @@ assign(reactComponentExpectInternal.prototype, {
139 return this;
140 },
141
142 - toBeTextComponent: function() {
142 + toBeTextComponentWithValue: function(val) {
143 var elementType = typeof this._instance._currentElement;
144 expect(elementType === 'string' || elementType === 'number').toBe(true);
145 + expect(this._instance._stringText).toBe(val);
146 return this;
147 },
148