Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty strings as key props #1524

Merged
merged 1 commit into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/enzyme-adapter-react-13/src/ReactThirteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
createRenderWrapper,
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
} from 'enzyme-adapter-utils';
import mapNativeEventNames from './ReactThirteenMapNativeEventNames';
import elementToTree from './ReactThirteenElementToTree';
Expand Down Expand Up @@ -75,7 +76,7 @@ function instanceToTree(inst) {
nodeType: 'host',
type: el.type,
props: el._store.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance.getDOMNode(),
rendered: values(children).map(instanceToTree),
Expand All @@ -86,7 +87,7 @@ function instanceToTree(inst) {
nodeType: 'class',
type: el.type,
props: el._store.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: instanceToTree(inst._renderedComponent),
Expand Down Expand Up @@ -174,7 +175,7 @@ class ReactThirteenAdapter extends EnzymeAdapter {
nodeType: 'class',
type: cachedNode.type,
props: cachedNode.props,
key: cachedNode.key || undefined,
key: ensureKeyOrUndefined(cachedNode.key),
ref: cachedNode.ref,
instance: renderer._instance._instance,
rendered: elementToTree(output),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flatten, isArrayLike } from 'enzyme-adapter-utils';
import { flatten, isArrayLike, ensureKeyOrUndefined } from 'enzyme-adapter-utils';

export function nodeTypeFromType(type) {
if (typeof type === 'string') {
Expand Down Expand Up @@ -35,7 +35,7 @@ export default function elementToTree(el) {
nodeType: nodeTypeFromType(type),
type,
props,
key: key || undefined,
key: ensureKeyOrUndefined(key),
ref,
instance: null,
rendered,
Expand Down
7 changes: 4 additions & 3 deletions packages/enzyme-adapter-react-14/src/ReactFourteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
createRenderWrapper,
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
} from 'enzyme-adapter-utils';

function typeToNodeType(type) {
Expand Down Expand Up @@ -47,7 +48,7 @@ function instanceToTree(inst) {
nodeType: 'host',
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: ReactDOM.findDOMNode(inst.getPublicInstance()) || null,
rendered: values(children).map(instanceToTree),
Expand All @@ -58,7 +59,7 @@ function instanceToTree(inst) {
nodeType: typeToNodeType(el.type),
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || null,
rendered: instanceToTree(inst._renderedComponent),
Expand Down Expand Up @@ -145,7 +146,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
nodeType: typeToNodeType(cachedNode.type),
type: cachedNode.type,
props: cachedNode.props,
key: cachedNode.key || undefined,
key: ensureKeyOrUndefined(cachedNode.key),
ref: cachedNode.ref,
instance: renderer._instance._instance,
rendered: elementToTree(output),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
createRenderWrapper,
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
} from 'enzyme-adapter-utils';
import ifReact from 'enzyme-adapter-react-helper/build/ifReact';

Expand Down Expand Up @@ -59,7 +60,7 @@ function instanceToTree(inst) {
nodeType: nodeType(inst),
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: childrenFromInst(inst, el).map(instanceToTree),
Expand All @@ -70,7 +71,7 @@ function instanceToTree(inst) {
nodeType: nodeType(inst),
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: childrenFromInst(inst, el).map(instanceToTree),
Expand All @@ -81,7 +82,7 @@ function instanceToTree(inst) {
nodeType: nodeType(inst),
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: instanceToTree(inst._renderedComponent),
Expand All @@ -91,7 +92,7 @@ function instanceToTree(inst) {
nodeType: nodeType(inst),
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || null,
rendered: childrenFromInst(inst, el).map(instanceToTree),
Expand Down Expand Up @@ -176,7 +177,7 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
nodeType: compositeTypeToNodeType(renderer._instance._compositeType),
type: cachedNode.type,
props: cachedNode.props,
key: cachedNode.key || undefined,
key: ensureKeyOrUndefined(cachedNode.key),
ref: cachedNode.ref,
instance: renderer._instance._instance,
rendered: elementToTree(output),
Expand Down
11 changes: 6 additions & 5 deletions packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
createRenderWrapper,
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
} from 'enzyme-adapter-utils';

function compositeTypeToNodeType(type) {
Expand Down Expand Up @@ -59,7 +60,7 @@ function instanceToTree(inst) {
nodeType: nodeType(inst),
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: values(inst._renderedChildren).map(instanceToTree),
Expand All @@ -70,7 +71,7 @@ function instanceToTree(inst) {
nodeType: 'host',
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: childrenFromInst(inst, el).map(instanceToTree),
Expand All @@ -81,7 +82,7 @@ function instanceToTree(inst) {
nodeType: nodeType(inst),
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: instanceToTree(inst._renderedComponent),
Expand All @@ -91,7 +92,7 @@ function instanceToTree(inst) {
nodeType: nodeType(inst),
type: el.type,
props: el.props,
key: el.key || undefined,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: inst._instance || null,
rendered: childrenFromInst(inst, el).map(instanceToTree),
Expand Down Expand Up @@ -176,7 +177,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
nodeType: compositeTypeToNodeType(renderer._instance._compositeType),
type: cachedNode.type,
props: cachedNode.props,
key: cachedNode.key || undefined,
key: ensureKeyOrUndefined(cachedNode.key),
ref: cachedNode.ref,
instance: renderer._instance._instance,
rendered: elementToTree(output),
Expand Down
9 changes: 5 additions & 4 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
createRenderWrapper,
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -76,7 +77,7 @@ function toTree(vnode) {
nodeType: 'class',
type: node.type,
props: { ...node.memoizedProps },
key: node.key || undefined,
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: node.stateNode,
rendered: childrenToTree(node.child),
Expand All @@ -88,7 +89,7 @@ function toTree(vnode) {
nodeType: 'function',
type: node.type,
props: { ...node.memoizedProps },
key: node.key || undefined,
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: null,
rendered: childrenToTree(node.child),
Expand All @@ -102,7 +103,7 @@ function toTree(vnode) {
nodeType: 'host',
type: node.type,
props: { ...node.memoizedProps },
key: node.key || undefined,
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: node.stateNode,
rendered: renderedNodes,
Expand Down Expand Up @@ -228,7 +229,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
nodeType: nodeTypeFromType(cachedNode.type),
type: cachedNode.type,
props: cachedNode.props,
key: cachedNode.key || undefined,
key: ensureKeyOrUndefined(cachedNode.key),
ref: cachedNode.ref,
instance: renderer._instance,
rendered: Array.isArray(output)
Expand Down
6 changes: 5 additions & 1 deletion packages/enzyme-adapter-utils/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ export function flatten(arrs) {
return flatArrs;
}

export function ensureKeyOrUndefined(key) {
return key || (key === '' ? '' : undefined);
Copy link
Author

@tryggvigy tryggvigy Feb 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check could be more restrictive.

 export function ensureStringOrUndefined(key) {
  return typeof key === 'string' ? key : undefined;
}

Are there situations where a key can be anything other than a string or a falsey value?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

react stringifies all valid keys before storing them, and a key is valid if it is not undefined. So i think this workes either way. I think it's probably best to leave it as you have now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe typeof key === 'string' ? key : undefined?

}

export function elementToTree(el) {
if (el === null || typeof el !== 'object' || !('type' in el)) {
return el;
Expand All @@ -174,7 +178,7 @@ export function elementToTree(el) {
nodeType: nodeTypeFromType(type),
type,
props,
key: key || undefined,
key: ensureKeyOrUndefined(key),
ref,
instance: null,
rendered,
Expand Down
3 changes: 2 additions & 1 deletion packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3146,11 +3146,12 @@ describeWithDOM('mount', () => {
it('should return the key of the node', () => {
const wrapper = mount((
<ul>
{['foo', 'bar'].map(s => <li key={s}>{s}</li>)}
{['foo', 'bar', ''].map(s => <li key={s}>{s}</li>)}
</ul>
)).find('li');
expect(wrapper.at(0).key()).to.equal('foo');
expect(wrapper.at(1).key()).to.equal('bar');
expect(wrapper.at(2).key()).to.equal('');
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4007,11 +4007,12 @@ describe('shallow', () => {
it('should return the key of the node', () => {
const wrapper = shallow((
<ul>
{['foo', 'bar'].map(s => <li key={s}>{s}</li>)}
{['foo', 'bar', ''].map(s => <li key={s}>{s}</li>)}
</ul>
)).find('li');
expect(wrapper.at(0).key()).to.equal('foo');
expect(wrapper.at(1).key()).to.equal('bar');
expect(wrapper.at(2).key()).to.equal('');
});
});

Expand Down