Skip to content
Open
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,14 @@ component, and leaves out the `<span>` children of the `<Text>` components. The
the `<App>` wrapper component, and the `<Text>` wrapper component.


## Stateless components
## Function components

Because [stateless components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions) can't be instantiated, `renderIntoDocument` won't return an instance back.
Because [function components](https://reactjs.org/docs/components-and-props.html#function-and-class-components) can't be instantiated, `renderIntoDocument` won't return an instance back.
Using the shallow renderer works as shown in the first example.
For full rendering, use the `when deeply rendered` to render the component

```js
expect(<StatelessComponent name="Daniel" />,
expect(<FunctionComponent name="Daniel" />,
'when deeply rendered',
'to have rendered',
<div>Hello, Daniel!</div>);
Expand Down
4 changes: 2 additions & 2 deletions documentation/assertions/ReactElement/when-deeply-rendered.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

You can render a component with the full DOM renderer directly with the `when deeply rendered` assertion.

This works with both stateful and stateless (functional) components, which can be easier than creating a manual wrapper and manually rendering into the DOM with `TestUtils.renderIntoDocument(...)` or `ReactDOM.render(...)`
This works with both class and function components, which can be easier than creating a manual wrapper and manually rendering into the DOM with `TestUtils.renderIntoDocument(...)` or `ReactDOM.render(...)`

Say you have these components:
```js
Expand All @@ -22,7 +22,7 @@ const Pages = (props) => {

```

You can check the rendering directly using the `when deeply rendered` assertion, saving you from creating stateless wrappers and rendering into the DOM by hand:
You can check the rendering directly using the `when deeply rendered` assertion, saving you from creating function component wrappers and rendering into the DOM by hand:
```js
expect(<Pages count={3} />,
'when deeply rendered',
Expand Down
10 changes: 5 additions & 5 deletions documentation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,25 +423,25 @@ component, and leaves out the `<span>` children of the `<Text>` components. The
the `<App>` wrapper component, and the `<Text>` wrapper component.


## Stateless components
Because [stateless components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions) can't be instantiated, `renderIntoDocument` won't return an instance back.
## Function components
Because [function components](https://reactjs.org/docs/components-and-props.html#function-and-class-components) can't be instantiated, `renderIntoDocument` won't return an instance back.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that when deeply rendered adds a wrapper for function components behind the scenes, is this part still valid?

Using the shallow renderer works as shown in the first example.
For full rendering, they can be tested with a wrapper component as such:

```js
class StatelessWrapper extends React.Component {
class FunctionWrapper extends React.Component {
render() {
return (this.props.children);
}
}

var StatelessComponent = function (props) {
var FunctionComponent = function (props) {
return (
<div>{ props.name }</div>
)
}

var component = TestUtils.renderIntoDocument(<StatelessWrapper><StatelessComponent name="Daniel" /></StatelessWrapper>);
var component = TestUtils.renderIntoDocument(<FunctionWrapper><FunctionComponent name="Daniel" /></FunctionWrapper>);
```

## Cleaning up
Expand Down
8 changes: 4 additions & 4 deletions src/assertions/deepAssertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function installInto(expect) {
checkAttached(expect);
});

class StatelessWrapper extends React.Component {
class FunctionWrapper extends React.Component {
render() {
return this.props.children;
}
Expand All @@ -87,16 +87,16 @@ function installInto(expect) {
if (subject.type && subject.type.prototype && typeof subject.type.prototype.render === 'function') {
component = TestUtils.renderIntoDocument(subject);
} else {
// Stateless component
component = TestUtils.renderIntoDocument(<StatelessWrapper>{subject}</StatelessWrapper>);
// Function component
component = TestUtils.renderIntoDocument(<FunctionWrapper>{subject}</FunctionWrapper>);
component = RenderHook.findComponent(component);
if (component) {
component = component && component.data.children[0] && RenderHook.findInternalComponent(component.data.children[0]);
} else {
expect.errorMode = 'nested';
expect.fail({
message(output) {
return output.error('Cannot find rendered stateless component. Are you sure you passed a real component to `when deeply rendered`');
return output.error('Cannot find rendered function component. Are you sure you passed a real component to `when deeply rendered`');
}
});
}
Expand Down
63 changes: 31 additions & 32 deletions src/tests/general/unexpected-react-deep.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,8 @@ describe('unexpected-react (deep rendering)', () => {

describe('when deeply rendered', function () {

const Stateless = function (props) {
return <div className="stateless-ftw">Yay</div>;
const Function = function (props) {
return <div className="function-ftw">Yay</div>;
};

it('renders a class component', function () {
Expand All @@ -954,39 +954,38 @@ describe('unexpected-react (deep rendering)', () => {
);
});

it('renders a stateless component', function () {
it('renders a function component', function () {

expect(<Stateless />, 'when deeply rendered', 'to have exactly rendered', <Stateless><div className="stateless-ftw">Yay</div></Stateless>);
expect(<Function />, 'when deeply rendered', 'to have exactly rendered', <Function><div className="function-ftw">Yay</div></Function>);
});

it('errors when a stateless component render does not match', function () {
it('errors when a function component render does not match', function () {

expect(() => expect(<Stateless />, 'when deeply rendered', 'to have exactly rendered',
<Stateless>
<div className="stateless-broken">Yay</div>
</Stateless>), 'to throw',
expect(() => expect(<Function />, 'when deeply rendered', 'to have exactly rendered',
<Function>
<div className="function-broken">Yay</div>
</Function>), 'to throw',
[
'expected <Stateless />',
'when deeply rendered to have exactly rendered <Stateless><div className="stateless-broken">Yay</div></Stateless>',
'expected <Function />',
'when deeply rendered to have exactly rendered <Function><div className="function-broken">Yay</div></Function>',
'',
'<Stateless>',
' <div',
' className="stateless-ftw" // expected \'stateless-ftw\' to equal \'stateless-broken\'',
' //',
' // -stateless-ftw',
' // +stateless-broken',
'<Function>',
' <div className="function-ftw" // expected \'function-ftw\' to equal \'function-broken\'',
' //',
' // -function-ftw',
' // +function-broken',
' >',
' Yay',
' </div>',
'</Stateless>'
'</Function>'
].join('\n'));
});
});

describe('to deeply render as', function () {

const Stateless = function (props) {
return <div className="stateless-ftw">Yay</div>;
const Function = function (props) {
return <div className="function-ftw">Yay</div>;
};

it('renders a class component', function () {
Expand All @@ -996,26 +995,26 @@ describe('unexpected-react (deep rendering)', () => {
);
});

it('renders a stateless component', function () {
it('renders a function component', function () {

expect(<Stateless />, 'to deeply render as', <Stateless><div className="stateless-ftw">Yay</div></Stateless>);
expect(<Function />, 'to deeply render as', <Function><div className="function-ftw">Yay</div></Function>);
});

it('errors when a stateless component render does not match', function () {
expect(() => expect(<Stateless />, 'to deeply render as',
<Stateless>
<div className="stateless-broken">Yay</div>
</Stateless>), 'to throw',
it('errors when a function component render does not match', function () {
expect(() => expect(<Function />, 'to deeply render as',
<Function>
<div className="function-broken">Yay</div>
</Function>), 'to throw',
[
'expected <Stateless />',
'to deeply render as <Stateless><div className="stateless-broken">Yay</div></Stateless>',
'expected <Function />',
'to deeply render as <Function><div className="function-broken">Yay</div></Function>',
'',
'<Stateless>',
' <div className="stateless-ftw" // missing class \'stateless-broken\'',
'<Function>',
' <div className="function-ftw" // missing class \'function-broken\'',
' >',
' Yay',
' </div>',
'</Stateless>'
'</Function>'
].join('\n'));
});

Expand Down
2 changes: 1 addition & 1 deletion src/tests/general/unexpected-react-shallow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ describe('unexpected-react-shallow', () => {
});
});

it('renders a stateless component', function () {
it('renders a function component', function () {
expect(<FunctionComp className="test"/>, 'when rendered', 'to have rendered', <div className="test" />) ;
});

Expand Down
2 changes: 1 addition & 1 deletion src/types/dom-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function installInto(expect) {
return (typeof value === 'object' &&
value !== null &&
(value._reactInternalInstance || value._reactInternalComponent) &&
(typeof value.setState === 'function' || typeof value.updater === 'object' /* stateless components */));
(typeof value.setState === 'function' || typeof value.updater === 'object' /* function components */));
},

inspect(value, depth, output, inspect) {
Expand Down