Skip to content

Commit

Permalink
Make ReactWrapper and ShallowWrapper iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Dail authored and Brandon Dail committed Sep 12, 2016
1 parent 69b7a18 commit d36dc6c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/ReactWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
propsOfNode,
typeOfNode,
displayNameOfNode,
ITERATOR_SYMBOL,
} from './Utils';
import {
debugInsts,
Expand Down Expand Up @@ -106,6 +107,26 @@ export default class ReactWrapper {
);
}

/**
* Makes a wrapper iterable, which is useful when using destructive
* assignment with `find()`
* @example
* const wrapper = shallow(<MyComponent />)
* const [fistLink, secondLink] = wrapper.find('a')
* @returns {Object}
*/
[ITERATOR_SYMBOL]() {
let index = 0;
return {
next: () => {
if (index >= this.nodes.length) {
return { done: true };
}
return { done: false, value: this.nodes[index++] };
},
};
}

/**
* If the root component contained a ref, you can access it here
* and get a wrapper around it.
Expand Down
21 changes: 21 additions & 0 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
isReactElementAlike,
displayNameOfNode,
isFunctionalComponent,
ITERATOR_SYMBOL,
} from './Utils';
import {
debugNodes,
Expand Down Expand Up @@ -101,6 +102,26 @@ export default class ShallowWrapper {
this.complexSelector = new ComplexSelector(buildPredicate, findWhereUnwrapped, childrenOfNode);
}

/**
* Makes a wrapper iterable, which is useful when using destructive
* assignment with `find()`
* @example
* const wrapper = shallow(<MyComponent />)
* const [fistLink, secondLink] = wrapper.find('a')
* @returns {Object}
*/
[ITERATOR_SYMBOL]() {
let index = 0;
return {
next: () => {
if (index >= this.nodes.length) {
return { done: true };
}
return { done: false, value: this.nodes[index++] };
},
};
}

/**
* Gets the instance of the component being rendered as the root node passed into `shallow()`.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
REACT15,
} from './version';

export const ITERATOR_SYMBOL = (
typeof Symbol === 'function' && Symbol.iterator
) || '@@iterator';

function internalInstanceKey(node) {
return Object.keys(Object(node)).filter(key => key.match(/^__reactInternalInstance\$/))[0];
}
Expand Down
27 changes: 27 additions & 0 deletions test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3019,4 +3019,31 @@ describeWithDOM('mount', () => {
});
});
});

describe('@@iterator', () => {
it('should be iterable', () => {
class Foo extends React.Component {
render() {
return (
<div>
<a href="#1">Hello</a>
<a href="#2">Hello</a>
<a href="#3">Hello</a>
<a href="#4">Hello</a>
</div>
);
}
}
const wrapper = mount(<Foo />);
const [a, b, c, d] = wrapper.find('a');
const a1 = wrapper.find('a').get(0);
const b1 = wrapper.find('a').get(1);
const c1 = wrapper.find('a').get(2);
const d1 = wrapper.find('a').get(3);
expect(a1).to.equal(a);
expect(b1).to.equal(b);
expect(c1).to.equal(c);
expect(d1).to.equal(d);
});
});
});
27 changes: 27 additions & 0 deletions test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3575,4 +3575,31 @@ describe('shallow', () => {
});
});

describe('@@iterator', () => {
it('should be iterable', () => {
class Foo extends React.Component {
render() {
return (
<div>
<a href="#1">Hello</a>
<a href="#2">Hello</a>
<a href="#3">Hello</a>
<a href="#4">Hello</a>
</div>
);
}
}
const wrapper = shallow(<Foo />);
const [a, b, c, d] = wrapper.find('a');
const a1 = wrapper.find('a').get(0);
const b1 = wrapper.find('a').get(1);
const c1 = wrapper.find('a').get(2);
const d1 = wrapper.find('a').get(3);
expect(a1).to.equal(a);
expect(b1).to.equal(b);
expect(c1).to.equal(c);
expect(d1).to.equal(d);
});
});

});

0 comments on commit d36dc6c

Please sign in to comment.