Skip to content

Commit

Permalink
[Deps] [breaking] update cheerio to v1
Browse files Browse the repository at this point in the history
The effective breaking change is that now `render` returns a wrapper around the *rendered thing*, like shallow and mount - instead of a “root” that requires you dive into `.children()`

See cheeriojs/cheerio#1047 (comment)
  • Loading branch information
ljharb committed Aug 28, 2017
1 parent b2f4e9e commit 8339ced
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
22 changes: 15 additions & 7 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2817,7 +2817,7 @@ describe('shallow', () => {
});

describe('.render()', () => {
it('should return a cheerio wrapper around the current node', () => {
it('returns a cheerio wrapper around the current node', () => {
class Foo extends React.Component {
render() {
return (<div className="in-foo" />);
Expand All @@ -2832,15 +2832,21 @@ describe('shallow', () => {
);
}
}

const wrapper = shallow(<Bar />);
expect(wrapper.render().find('.in-bar')).to.have.length(1);

const rendered = wrapper.render();
expect(rendered.is('.in-bar')).to.equal(true);
expect(rendered).to.have.lengthOf(1);

const renderedFoo = wrapper.find(Foo).render();
expect(renderedFoo.find('.in-foo')).to.have.length(1);
expect(renderedFoo.is('.in-foo')).to.equal(true);
expect(renderedFoo.is('.in-bar')).to.equal(false);
expect(renderedFoo.find('.in-bar')).to.have.length(0);
});

describeIf(!REACT013, 'stateless function components', () => {
it('should return a cheerio wrapper around the current node', () => {
describeIf(!REACT013, 'stateless functional components', () => {
it('returns a cheerio wrapper around the current node', () => {
const Foo = () => (
<div className="in-foo" />
);
Expand All @@ -2851,9 +2857,11 @@ describe('shallow', () => {
);

const wrapper = shallow(<Bar />);
expect(wrapper.render().find('.in-bar')).to.have.length(1);
expect(wrapper.render().is('.in-bar')).to.equal(true);

const renderedFoo = wrapper.find(Foo).render();
expect(renderedFoo.find('.in-foo')).to.have.length(1);
expect(renderedFoo.is('.in-foo')).to.equal(true);
expect(renderedFoo.is('.in-bar')).to.equal(false);
expect(renderedFoo.find('.in-bar')).to.have.length(0);
});
});
Expand Down
17 changes: 5 additions & 12 deletions packages/enzyme-test-suite/test/staticRender-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ describeWithDOM('render', () => {

const context = { name: 'foo' };
const wrapper = render(<SimpleComponent />, { context });
expect(wrapper).to.have.lengthOf(1);

const rootEls = wrapper.children();
expect(rootEls).to.have.lengthOf(1);

expect(rootEls.is('div')).to.equal(true);
expect(rootEls.text()).to.equal('foo');
expect(wrapper.is('div')).to.equal(true);
expect(wrapper.text()).to.equal('foo');

expect(String(wrapper)).to.equal('<div>foo</div>');
expect(String(rootEls)).to.equal('<div>foo</div>');
});

it('can pass context to the child of mounted component', () => {
Expand All @@ -53,20 +49,17 @@ describeWithDOM('render', () => {
};
const context = { name: 'foo' };
const wrapper = render(<ComplexComponent />, { context, childContextTypes });
expect(wrapper).to.have.length(1);

const rootEls = wrapper.children();
expect(rootEls).to.have.length(1);

expect(rootEls.is('div')).to.equal(true);
expect(wrapper.is('div')).to.equal(true);

const children = rootEls.children();
const children = wrapper.children();
expect(children).to.have.length(1);
expect(children.is('span')).to.equal(true);

expect(children.first().text()).to.equal('foo');

expect(String(wrapper)).to.equal('<div><span>foo</span></div>');
expect(String(rootEls)).to.equal('<div><span>foo</span></div>');
expect(String(children)).to.equal('<span>foo</span>');
});

Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"author": "Leland Richardson <leland.richardson@airbnb.com>",
"license": "MIT",
"dependencies": {
"cheerio": "^0.22.0",
"cheerio": "^1.0.0-rc.2",
"function.prototype.name": "^1.0.3",
"is-subset": "^0.1.1",
"lodash": "^4.17.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ class ReactWrapper {
*/
render() {
const html = this.html();
return html === null ? cheerio() : cheerio.load(html).root();
return html === null ? cheerio() : cheerio.load('')(html);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ class ShallowWrapper {
* @returns {CheerioWrapper}
*/
render() {
return this.type() === null ? cheerio() : cheerio.load(this.html()).root();
return this.type() === null ? cheerio() : cheerio.load('')(this.html());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export default function render(node, options = {}) {
const adapter = getAdapter(options);
const renderer = adapter.createRenderer({ mode: 'string', ...options });
const html = renderer.render(node, options.context);
return cheerio.load(html).root();
return cheerio.load('')(html);
}

0 comments on commit 8339ced

Please sign in to comment.