Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Update docs around SFC instances in React 16 #1243

Merged
merged 1 commit into from
Dec 23, 2017
Merged
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
56 changes: 49 additions & 7 deletions docs/api/ShallowWrapper/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,62 @@

Gets the instance of the component being rendered as the root node passed into `shallow()`.

NOTE: can only be called on a wrapper instance that is also the root instance.
NOTE: can only be called on a wrapper instance that is also the root instance. With React `16.x`, `instance()` returns `null` for stateless React component/stateless functional components. See example:

#### Returns (React 16.x)

- `ReactComponent`: The stateful React component instance.
- `null`: If stateless React component was wrapped.

#### Returns
#### Returns (React 15.x)

`ReactComponent`: The component instance.
- `ReactComponent`: The component instance.

#### Example

#### Preconditions
<!-- eslint react/prop-types: 0, react/prefer-stateless-function: 0 -->
```jsx
function Stateless() {
return <div>Stateless</div>;
}

class Stateful extends React.Component {
render() {
return <div>Stateful</div>;
}
}
```
#### React 16.x
```jsx
test('shallow wrapper instance should not be null', () => {
const wrapper = shallow(<Stateless />);
const instance = wrapper.instance();

expect(instance).not.to.be.instanceOf(Stateless);
});

#### Example
test('shallow wrapper instance should not be null', () => {
const wrapper = shallow(<Stateful />);
const instance = wrapper.instance();

expect(instance).to.be.instanceOf(Stateful);
});
```
#### React 15.x
```jsx
const wrapper = shallow(<MyComponent />);
const inst = wrapper.instance();
expect(inst).to.be.instanceOf(MyComponent);
test('shallow wrapper instance should not be null', () => {
const wrapper = shallow(<Stateless />);
const instance = wrapper.instance();

expect(instance).to.be.instanceOf(Stateless);
});

test('shallow wrapper instance should not be null', () => {
const wrapper = shallow(<Stateful />);
const instance = wrapper.instance();

expect(instance).to.be.instanceOf(Stateful);
});
```

11 changes: 7 additions & 4 deletions docs/api/ShallowWrapper/props.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ called on a wrapper of a single node.

NOTE: When called on a shallow wrapper, `.props()` will return values for
props on the root node that the component *renders*, not the component itself.
To return the props for the entire React component, use `wrapper.instance().props`.
See [`.instance() => ReactComponent`](instance.md)

To return the props for the entire React component, use `wrapper.instance().props`. This is valid for stateful or stateless components in React `15.*`. But, `wrapper.instance()` will return `null` for stateless React component in React `16.*`, so `wrapper.instance().props` will cause an error in this case. See [`.instance() => ReactComponent`](instance.md)

#### Example

#### Example
```jsx

import PropTypes from 'prop-types';

function MyComponent(props) {
Expand All @@ -34,8 +34,11 @@ expect(wrapper.props().includedProp).to.equal('Success!');
console.log(wrapper.props());
// {children: "Hello", className: "foo bar", includedProp="Success!"}

console.log(wrapper.instance().props);
console.log(wrapper.instance().props); // React 15.x - working as expected
// {children: "Hello", className: "foo bar", includedProp:"Success!", excludedProp: "I'm not included"}

console.log(wrapper.instance().props);
// React 16.* - Uncaught TypeError: Cannot read property 'props' of null
```


Expand Down