Skip to content

Commit

Permalink
Change shallowWrapper.text() to handle spaces same behavior as ReactW…
Browse files Browse the repository at this point in the history
…rapper.text()

- remove empty nodes
- remove double spaces
- remove spaces from beginning or end
- not sure all those are all the rule, need to check
- couldn't run tests
  • Loading branch information
Idan Levin committed Nov 14, 2017
1 parent 3f2eab6 commit e58f8b0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 23 additions & 1 deletion packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import sinon from 'sinon';
import { shallow, render, ShallowWrapper } from 'enzyme';
import { shallow, render, ShallowWrapper, mount } from 'enzyme';
import { ITERATOR_SYMBOL, withSetStateAllowed, sym } from 'enzyme/build/Utils';

import './_helpers/setupAdapters';
Expand Down Expand Up @@ -1633,6 +1633,28 @@ describe('shallow', () => {
matchesRender(<div>&gt;</div>);
});

it('should handle spaces with same behavior as ReactWarpper.text()', () => {
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 = shallow(Space);
const mounted = mount(Space);
expect(wrapper.text()).to.equal(mounted.text());
})
});

describeIf(!REACT013, 'stateless function components', () => {
it('should handle nodes with mapped children', () => {
const Foo = props => (
Expand Down
4 changes: 3 additions & 1 deletion packages/enzyme/src/RSTTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,7 @@ export function getTextFromNode(node) {

return childrenOfNode(node).map(getTextFromNode)
.join('')
.replace(/\s+/, ' ');
.replace(/^[ \t\n\r]+$/g, '') // remove empty nodes
.replace(/[ \t\n\r]+/, ' ') // remove double spaces
.replace(/(^[ \t\n\r]*|[ \t\n\r]*$)/, ''); // remove spaces from beginning or end
}

0 comments on commit e58f8b0

Please sign in to comment.