Skip to content

Commit

Permalink
Add context introspection API to Wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
blainekasten committed Feb 1, 2016
1 parent 46f4d36 commit a295181
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* [last()](/docs/api/ShallowWrapper/last.md)
* [state([key])](/docs/api/ShallowWrapper/state.md)
* [props()](/docs/api/ShallowWrapper/props.md)
* [context([key])](/docs/api/ShallowWrapper/context.md)
* [prop([key])](/docs/api/ShallowWrapper/prop.md)
* [simulate(event[, data])](/docs/api/ShallowWrapper/simulate.md)
* [setState(nextState)](/docs/api/ShallowWrapper/setState.md)
Expand Down Expand Up @@ -67,6 +68,7 @@
* [last()](/docs/api/ReactWrapper/last.md)
* [state([key])](/docs/api/ReactWrapper/state.md)
* [props()](/docs/api/ReactWrapper/props.md)
* [context([key])](/docs/api/ReactWrapper/context.md)
* [prop([key])](/docs/api/ReactWrapper/prop.md)
* [simulate(event[, data])](/docs/api/ReactWrapper/simulate.md)
* [setState(nextState)](/docs/api/ReactWrapper/setState.md)
Expand Down
32 changes: 32 additions & 0 deletions docs/api/ReactWrapper/context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# `.context([key]) => Any`

Returns the context hash for the root node of the wrapper. Optionally pass in a prop name and it
will return just that value.


#### Arguments

1. `key` (`String` [optional]): If provided, the return value will be the `this.context[key]` of the
root component instance.



#### Example


```jsx
const wrapper = mount(
<MyComponent />,
{ options: { context: { foo: 10 } } }
);

expect(wrapper.context().foo).to.equal(10);
expect(wrapper.context('foo')).to.equal(10);
```


#### Related Methods

- [`.state([key]) => Any`](state.md)
- [`.props() => Object`](props.md)
- [`.prop(key) => Any`](prop.md)
1 change: 1 addition & 0 deletions docs/api/ReactWrapper/prop.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ expect(wrapper.prop('foo')).to.equal(10);

- [`.props() => Object`](props.md)
- [`.state([key]) => Any`](state.md)
- [`.context([key]) => Any`](context.md)
1 change: 1 addition & 0 deletions docs/api/ReactWrapper/props.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ expect(wrapper.props().foo).to.equal(10);

- [`.prop() => Object`](prop.md)
- [`.state([key]) => Any`](state.md)
- [`.context([key]) => Any`](context.md)
1 change: 1 addition & 0 deletions docs/api/ReactWrapper/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ expect(wrapper.state('foo')).to.equal(10);

- [`.props() => Object`](props.md)
- [`.prop(key) => Any`](prop.md)
- [`.context([key]) => Any`](context.md)
31 changes: 31 additions & 0 deletions docs/api/ShallowWrapper/context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# `.context([key]) => Any`

Returns the context hash for the root node of the wrapper. Optionally pass in a prop name and it
will return just that value.


#### Arguments

1. `key` (`String` [optional]): If provided, the return value will be the `this.context[key]` of the
root component instance.



#### Example


```jsx
const wrapper = shallow(
<MyComponent />,
{ options: { context: { foo: 10 } } }
);
expect(wrapper.context().foo).to.equal(10);
expect(wrapper.context('foo')).to.equal(10);
```


#### Related Methods

- [`.props() => Object`](props.md)
- [`.prop(key) => Any`](prop.md)
- [`.state([key]) => Any`](state.md)
1 change: 1 addition & 0 deletions docs/api/ShallowWrapper/prop.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ expect(wrapper.prop('foo')).to.equal(10);

- [`.props() => Object`](props.md)
- [`.state([key]) => Any`](state.md)
- [`.context([key]) => Any`](context.md)
1 change: 1 addition & 0 deletions docs/api/ShallowWrapper/props.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ expect(wrapper.props().foo).to.equal(10);

- [`.prop() => Object`](prop.md)
- [`.state([key]) => Any`](state.md)
- [`.context([key]) => Any`](context.md)
1 change: 1 addition & 0 deletions docs/api/ShallowWrapper/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ expect(wrapper.state('foo')).to.equal(10);

- [`.props() => Object`](props.md)
- [`.prop(key) => Any`](prop.md)
- [`.context([key]) => Any`](context.md)
3 changes: 3 additions & 0 deletions docs/api/mount.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Returns a wrapper of the last node of the current wrapper.
#### [`.state([key]) => Any`](ReactWrapper/state.md)
Returns the state of the root component.

#### [`.context([key]) => Any`](ReactWrapper/context.md)
Returns the context of the root component.

#### [`.props() => Object`](ReactWrapper/props.md)
Returns the props of the root component.

Expand Down
3 changes: 3 additions & 0 deletions docs/api/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ Returns a wrapper of the last node of the current wrapper.
#### [`.state([key]) => Any`](ShallowWrapper/state.md)
Returns the state of the root component.

#### [`.context([key]) => Any`](ShallowWrapper/context.md)
Returns the context of the root component.

#### [`.props() => Object`](ShallowWrapper/props.md)
Returns the props of the root component.

Expand Down
20 changes: 20 additions & 0 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,26 @@ export default class ReactWrapper {
return _state;
}

/**
* Returns the context hash for the root node of the wrapper.
* Optionally pass in a prop name and it will return just that value.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @param {String} name (optional)
* @returns {*}
*/
context(name) {
if (this.root !== this) {
throw new Error('ReactWrapper::context() can only be called on the root');
}
const _context = this.single(() => this.instance().context);
if (name !== undefined) {
return _context[name];
}
return _context;
}

/**
* Returns a new wrapper with all of the children of the current wrapper.
*
Expand Down
20 changes: 20 additions & 0 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,26 @@ export default class ShallowWrapper {
return _state;
}

/**
* Returns the context hash for the root node of the wrapper.
* Optionally pass in a prop name and it will return just that value.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @param {String} name (optional)
* @returns {*}
*/
context(name) {
if (this.root !== this) {
throw new Error('ShallowWrapper::context() can only be called on the root');
}
const _context = this.single(() => this.instance().context);
if (name) {
return _context[name];
}
return _context;
}

/**
* Returns a new wrapper with all of the children of the current wrapper.
*
Expand Down
20 changes: 18 additions & 2 deletions src/__tests__/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { REACT013 } from '../version';
describeWithDOM('mount', () => {

describe('context', () => {
const context = { name: 'foo' };

it('can pass in context', () => {
const SimpleComponent = React.createClass({
contextTypes: {
Expand All @@ -23,7 +25,6 @@ describeWithDOM('mount', () => {
},
});

const context = { name: 'foo' };
const wrapper = mount(<SimpleComponent />, { context });
expect(wrapper.text()).to.equal('foo');
});
Expand All @@ -35,9 +36,24 @@ describeWithDOM('mount', () => {
},
});

const context = { name: 'foo' };
expect(() => mount(<SimpleComponent />, { context })).to.not.throw(Error);
});

it('is instrospectable through context API', () => {
const SimpleComponent = React.createClass({
contextTypes: {
name: React.PropTypes.string,
},
render() {
return <div>{this.context.name}</div>;
},
});

const wrapper = mount(<SimpleComponent />, { context });

expect(wrapper.context().name).to.equal(context.name);
expect(wrapper.context('name')).to.equal(context.name);
});
});

describeIf(!REACT013, 'stateless components', () => {
Expand Down
20 changes: 18 additions & 2 deletions src/__tests__/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { REACT013 } from '../version';
describe('shallow', () => {

describe('context', () => {
const context = { name: 'foo' };

it('can pass in context', () => {
const SimpleComponent = React.createClass({
contextTypes: {
Expand All @@ -18,7 +20,6 @@ describe('shallow', () => {
},
});

const context = { name: 'foo' };
const wrapper = shallow(<SimpleComponent />, { context });
expect(wrapper.text()).to.equal('foo');
});
Expand All @@ -30,9 +31,24 @@ describe('shallow', () => {
},
});

const context = { name: 'foo' };
expect(() => shallow(<SimpleComponent />, { context })).to.not.throw(Error);
});

it('is instrospectable through context API', () => {
const SimpleComponent = React.createClass({
contextTypes: {
name: React.PropTypes.string,
},
render() {
return <div>{this.context.name}</div>;
},
});

const wrapper = shallow(<SimpleComponent />, { context });

expect(wrapper.context().name).to.equal(context.name);
expect(wrapper.context('name')).to.equal(context.name);
});
});

describeIf(!REACT013, 'stateless components', () => {
Expand Down

0 comments on commit a295181

Please sign in to comment.