Skip to content

Commit

Permalink
Add options to ShallowWrapper#shallow
Browse files Browse the repository at this point in the history
  • Loading branch information
ncuillery committed Mar 21, 2016
1 parent a71d736 commit e62a417
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* [setContext(context)](/docs/api/ShallowWrapper/setContext.md)
* [setProps(nextProps)](/docs/api/ShallowWrapper/setProps.md)
* [setState(nextState)](/docs/api/ShallowWrapper/setState.md)
* [shallow()](/docs/api/ShallowWrapper/shallow.md)
* [shallow([options])](/docs/api/ShallowWrapper/shallow.md)
* [simulate(event[, data])](/docs/api/ShallowWrapper/simulate.md)
* [some(selector)](/docs/api/ShallowWrapper/some.md)
* [someWhere(predicate)](/docs/api/ShallowWrapper/someWhere.md)
Expand Down
7 changes: 6 additions & 1 deletion docs/api/ShallowWrapper/shallow.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# `.shallow() => ShallowWrapper`
# `.shallow([options]) => ShallowWrapper`

Shallow renders the current node and returns a shallow wrapper around it.

NOTE: can only be called on wrapper of a single node.


#### Arguments

1. `options` (`Object` [optional]):
- `options.context`: (`Object` [optional]): Context to be passed into the component



#### Returns
Expand Down
2 changes: 1 addition & 1 deletion docs/api/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Get a wrapper with the direct parent of the current node.
#### [`.closest(selector) => ShallowWrapper`](ShallowWrapper/closest.md)
Get a wrapper with the first ancestor of the current node to match the provided selector.

#### [`.shallow() => ShallowWrapper`](ShallowWrapper/shallow.md)
#### [`.shallow([options]) => ShallowWrapper`](ShallowWrapper/shallow.md)
Shallow renders the current node and returns a shallow wrapper around it.

#### [`.render() => CheerioWrapper`](ShallowWrapper/render.md)
Expand Down
5 changes: 3 additions & 2 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,11 @@ export default class ShallowWrapper {
*
* NOTE: can only be called on wrapper of a single node.
*
* @param options object
* @returns {ShallowWrapper}
*/
shallow() {
return this.single((n) => new ShallowWrapper(n));
shallow(options) {
return this.single((n) => new ShallowWrapper(n, null, options));
}

/**
Expand Down
123 changes: 123 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,79 @@ describe('shallow', () => {
expect(wrapper.find(Bar).shallow().find('.in-bar')).to.have.length(1);
});

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

it('can pass in context', () => {
class Bar extends React.Component {
render() {
return <div>{this.context.name}</div>;
}
}
Bar.contextTypes = {
name: React.PropTypes.string,
};
class Foo extends React.Component {
render() {
return (
<div>
<Bar />
</div>
);
}
}

const wrapper = shallow(<Foo />);
expect(wrapper.find(Bar)).to.have.length(1);
expect(wrapper.find(Bar).shallow({ context }).text()).to.equal('foo');
});

it('should not throw if context is passed in but contextTypes is missing', () => {
class Bar extends React.Component {
render() {
return <div>{this.context.name}</div>;
}
}
class Foo extends React.Component {
render() {
return (
<div>
<Bar />
</div>
);
}
}

const wrapper = shallow(<Foo />);
expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw(Error);
});

it('is instrospectable through context API', () => {
class Bar extends React.Component {
render() {
return <div>{this.context.name}</div>;
}
}
Bar.contextTypes = {
name: React.PropTypes.string,
};
class Foo extends React.Component {
render() {
return (
<div>
<Bar />
</div>
);
}
}

const wrapper = shallow(<Foo />).find(Bar).shallow({ context });

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

describeIf(!REACT013, 'stateless function components', () => {
it('should return a shallow rendered instance of the current node', () => {
const Bar = () => (
Expand All @@ -1844,6 +1917,56 @@ describe('shallow', () => {
expect(wrapper.find(Bar)).to.have.length(1);
expect(wrapper.find(Bar).shallow().find('.in-bar')).to.have.length(1);
});

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

it('can pass in context', () => {
const Bar = (props, context) => (
<div>{context.name}</div>
);
Bar.contextTypes = { name: React.PropTypes.string };
const Foo = () => (
<div>
<Bar />
</div>
);

const wrapper = shallow(<Foo />);
expect(wrapper.find(Bar).shallow({ context }).text()).to.equal('foo');
});

it('should not throw if context is passed in but contextTypes is missing', () => {
const Bar = (props, context) => (
<div>{context.name}</div>
);
const Foo = () => (
<div>
<Bar />
</div>
);

const wrapper = shallow(<Foo />);
expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw(Error);
});

it('is instrospectable through context API', () => {
const Bar = (props, context) => (
<div>{context.name}</div>
);
Bar.contextTypes = { name: React.PropTypes.string };
const Foo = () => (
<div>
<Bar />
</div>
);

const wrapper = shallow(<Foo />).find(Bar).shallow({ context });

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

});
Expand Down

0 comments on commit e62a417

Please sign in to comment.