Skip to content

Commit

Permalink
[Tests] mount/shallow: .text(): make tests consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 29, 2018
1 parent 4553bb7 commit 8badab7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 26 deletions.
83 changes: 63 additions & 20 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2926,10 +2926,55 @@ describeWithDOM('mount', () => {
matchesRender(<div>&gt;</div>);
});

it('handles spaces the same between shallow and mount', () => {
const Space = (
<div>
<div> test </div>
<div>Hello


World
</div>
<div>Hello World</div>
<div>Hello
World
</div>
<div>Hello World</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;</div>
</div>
);

const wrapper = mount(Space);

expect(wrapper.text()).to.equal(' test Hello WorldHello WorldHello WorldHello World   ');
});

it('handles non-breaking spaces correctly', () => {
class Foo extends React.Component {
render() {
return (
<div>
&nbsp; &nbsp;
</div>
);
}
}
const wrapper = mount(<Foo />);
const charCodes = wrapper.text().split('').map(x => x.charCodeAt(0));
expect(charCodes).to.eql([
0x00a0, // non-breaking space
0x20, // normal space
0x00a0, // non-breaking space
]);
});

describeIf(is('> 0.13'), 'stateless function components', () => {
it('handles nodes with mapped children', () => {
const Foo = props => (
<div>{props.items.map(x => x)}</div>
<div>
{props.items.map(x => x)}
</div>
);
matchesRender(<Foo items={['abc', 'def', 'hij']} />);
matchesRender((
Expand All @@ -2947,6 +2992,7 @@ describeWithDOM('mount', () => {
const Foo = () => (
<div>foo</div>
);

const wrapper = mount((
<div>
<Foo />
Expand All @@ -2957,31 +3003,28 @@ describeWithDOM('mount', () => {
});
});

it('handles non-breaking spaces correctly', () => {
class Foo extends React.Component {
render() {
return (
<div>
&nbsp; &nbsp;
</div>
);
}
}
const wrapper = mount(<Foo />);
const charCodes = wrapper.text().split('').map(x => x.charCodeAt(0));
expect(charCodes).to.eql([
0x00a0, // non-breaking space
0x20, // normal space
0x00a0, // non-breaking space
]);
});

it('renders falsy numbers', () => {
[0, -0, '0', NaN].forEach((x) => {
const wrapper = mount(<div>{x}</div>);
expect(wrapper.text()).to.equal(String(x));
});
});

describe('text content with curly braces', () => {
it('handles literal strings', () => {
const wrapper = mount(<div><div>{'{}'}</div></div>);
expect(wrapper.text()).to.equal('{}');
});

it('handles innerHTML', () => {
const wrapper = mount((
<div>
<div dangerouslySetInnerHTML={{ __html: '{ some text }' }} />
</div>
));
expect(wrapper.text()).to.equal('{ some text }');
});
});
});

describe('.props()', () => {
Expand Down
15 changes: 9 additions & 6 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2870,7 +2870,7 @@ describe('shallow', () => {
matchesRender(<div>&gt;</div>);
});

it('handles spaces with same behavior as ReactWrapper.text()', () => {
it('handles spaces the same between shallow and mount', () => {
const Space = (
<div>
<div> test </div>
Expand All @@ -2890,9 +2890,8 @@ describe('shallow', () => {
);

const wrapper = shallow(Space);
const mounted = mount(Space);

expect(wrapper.text()).to.equal(mounted.text());
expect(wrapper.text()).to.equal(' test Hello WorldHello WorldHello WorldHello World   ');
});

it('handles non-breaking spaces correctly', () => {
Expand Down Expand Up @@ -2935,7 +2934,7 @@ describe('shallow', () => {

it('renders composite components dumbly', () => {
const Foo = () => (
<div />
<div>foo</div>
);

const wrapper = shallow((
Expand All @@ -2962,8 +2961,12 @@ describe('shallow', () => {
});

it.skip('handles innerHTML', () => {
const wrapper = shallow(<div><div dangerouslySetInnerHTML={{ __html: '{}' }} /></div>);
expect(wrapper.text()).to.equal('{}');
const wrapper = shallow((
<div>
<div dangerouslySetInnerHTML={{ __html: '{ some text }' }} />
</div>
));
expect(wrapper.text()).to.equal('{ some text }');
});
});
});
Expand Down

0 comments on commit 8badab7

Please sign in to comment.