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

Add options to ShallowWrapper#shallow #275

Merged
merged 1 commit into from
Mar 21, 2016
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
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
126 changes: 125 additions & 1 deletion test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,6 @@ describe('shallow', () => {
});

describe('.shallow()', () => {

it('should return a shallow rendered instance of the current node', () => {
class Bar extends React.Component {
render() {
Expand All @@ -1828,6 +1827,80 @@ describe('shallow', () => {
expect(wrapper.find(Bar).shallow().find('.in-bar')).to.have.length(1);
});

describe('context', () => {
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 context = { name: 'foo' };
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 context = { name: 'foo' };
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 context = { name: 'foo' };
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 @@ -1846,6 +1919,57 @@ describe('shallow', () => {
expect(wrapper.find(Bar)).to.have.length(1);
expect(wrapper.find(Bar).shallow().find('.in-bar')).to.have.length(1);
});

describe('context', () => {
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 context = { name: 'foo' };
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 context = { name: 'foo' };
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 context = { name: 'foo' };
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