Skip to content

Commit

Permalink
Conditionally define iterator method
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Dail authored and Brandon Dail committed Oct 11, 2016
1 parent 2e08d9e commit 1e78d6a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 45 deletions.
31 changes: 10 additions & 21 deletions src/ReactWrapper.jsx
Expand Up @@ -63,7 +63,7 @@ function filterWhereUnwrapped(wrapper, predicate) {
/**
* @class ReactWrapper
*/
export default class ReactWrapper {
class ReactWrapper {

constructor(nodes, root, options = {}) {
if (!global.window && !global.document) {
Expand Down Expand Up @@ -107,26 +107,6 @@ 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 Expand Up @@ -929,3 +909,12 @@ export default class ReactWrapper {
unmountComponentAtNode(this.options.attachTo);
}
}


if (ITERATOR_SYMBOL) {
ReactWrapper.prototype[ITERATOR_SYMBOL] = function iterator() {
return this.nodes[ITERATOR_SYMBOL]();
};
}

export default ReactWrapper;
30 changes: 9 additions & 21 deletions src/ShallowWrapper.js
Expand Up @@ -64,7 +64,7 @@ function filterWhereUnwrapped(wrapper, predicate) {
/**
* @class ShallowWrapper
*/
export default class ShallowWrapper {
class ShallowWrapper {

constructor(nodes, root, options = {}) {
if (!root) {
Expand Down Expand Up @@ -104,26 +104,6 @@ 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 Expand Up @@ -1003,3 +983,11 @@ export default class ShallowWrapper {
});
}
}

if (ITERATOR_SYMBOL) {
ShallowWrapper.prototype[ITERATOR_SYMBOL] = function iterator() {
return this.nodes[ITERATOR_SYMBOL]();
};
}

export default ShallowWrapper;
4 changes: 1 addition & 3 deletions src/Utils.js
Expand Up @@ -12,9 +12,7 @@ import {
REACT15,
} from './version';

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

function internalInstanceKey(node) {
return Object.keys(Object(node)).filter(key => key.match(/^__reactInternalInstance\$/))[0];
Expand Down

0 comments on commit 1e78d6a

Please sign in to comment.