Skip to content

Commit

Permalink
Explictly add support for Immutable instead of generic Iterables
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcq committed Oct 11, 2017
1 parent 465a0ba commit ad838e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export default function elementToTree(el) {
let rendered = null;
if (Array.isArray(children)) {
rendered = flatten(children, true).map(elementToTree);
} else if (children && typeof children.toArray === 'function') {
rendered = flatten(children.toArray(), true).map(elementToTree);
} else if (typeof children !== 'undefined') {
rendered = elementToTree(children);
}
Expand Down
52 changes: 3 additions & 49 deletions packages/enzyme-adapter-utils/src/elementToTree.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
import flatten from 'lodash/flatten';
import nodeTypeFromType from './nodeTypeFromType';

const ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
const FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec.

/**
* Returns the iterator method function contained on the iterable object.
*
* Be sure to invoke the function with the iterable as context:
*
* const iteratorFn = getIteratorFn(myIterable);
* if (iteratorFn) {
* const iterator = iteratorFn.call(myIterable);
* ...
* }
*
* @param {?object} maybeIterable
* @return {?function}
*/
function getIteratorFn(maybeIterable) {
const iteratorFn = (
maybeIterable && (
(ITERATOR_SYMBOL && maybeIterable[ITERATOR_SYMBOL]) ||
maybeIterable[FAUX_ITERATOR_SYMBOL]
)
);
if (typeof iteratorFn === 'function') {
return iteratorFn;
}
return undefined;
}

export default function elementToTree(el) {
if (el === null || typeof el !== 'object' || !('type' in el)) {
return el;
Expand All @@ -40,26 +10,10 @@ export default function elementToTree(el) {
let rendered = null;
if (Array.isArray(children)) {
rendered = flatten(children, true).map(elementToTree);
} else if (typeof children === 'string') {
} else if (children && typeof children.toArray === 'function') {
rendered = flatten(children.toArray(), true).map(elementToTree);
} else if (typeof children !== 'undefined') {
rendered = elementToTree(children);
} else {
// This allows children to be iterables that aren't Arrays,
// e.g. Immutable-js Lists, Seqs, etc.
// For more info see the relevant React pull request,
// off which this code is based:
// https://github.com/facebook/react/pull/2376
const iteratorFn = getIteratorFn(children);
if (typeof iteratorFn === 'function') {
const iterator = iteratorFn.call(children);
let step = null;
while (step && !step.done) {
const child = step.value;
rendered = elementToTree(child);
step = iterator.next();
}
} else if (typeof children !== 'undefined') {
rendered = elementToTree(children);
}
}
return {
nodeType: nodeTypeFromType(type),
Expand Down
27 changes: 11 additions & 16 deletions packages/enzyme-test-suite/test/RSTTraversal-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,27 @@ describe('RSTTraversal', () => {
</div>,
);
treeForEach(node, spy);
expect(spy.callCount).to.equal(4);
expect(spy.callCount).to.equal(3);
});

it('should handle non-array iterable children', () => {
it('should handle Immutable.js children', () => {
const spy = sinon.spy();
const twoDivIterable = {
'@@iterator'() {
let i = 0;
return {
next() {
i += 1;
if (i < 2) {
return { value: <div key={i} />, done: false };
}
return { value: undefined, done: true };
},
};
// This object mimics only the toArray functionality of Immutable.js
const twoDivImmutable = {
toArray() {
return [
<div key="a" />,
<div key="b" />,
];
},
};
const node = $(
<div>
{twoDivIterable}
{twoDivImmutable}
</div>,
);
treeForEach(node, spy);
expect(spy.callCount).to.equal(4);
expect(spy.callCount).to.equal(3);
});

it('should not get trapped from empty strings', () => {
Expand Down

0 comments on commit ad838e4

Please sign in to comment.