Skip to content

Commit

Permalink
Add key method
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 17, 2016
1 parent de4cb59 commit 1b9d317
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* [html()](/docs/api/ShallowWrapper/html.md)
* [instance()](/docs/api/ShallowWrapper/instance.md)
* [is(selector)](/docs/api/ShallowWrapper/is.md)
* [key()](/docs/api/ShallowWrapper/key.md)
* [last()](/docs/api/ShallowWrapper/last.md)
* [map(fn)](/docs/api/ShallowWrapper/map.md)
* [not(selector)](/docs/api/ShallowWrapper/not.md)
Expand Down Expand Up @@ -80,6 +81,7 @@
* [html()](/docs/api/ReactWrapper/html.md)
* [instance()](/docs/api/ReactWrapper/instance.md)
* [is(selector)](/docs/api/ReactWrapper/is.md)
* [key()](/docs/api/ReactWrapper/key.md)
* [last()](/docs/api/ReactWrapper/last.md)
* [map(fn)](/docs/api/ReactWrapper/map.md)
* [mount()](/docs/api/ReactWrapper/mount.md)
Expand Down
18 changes: 18 additions & 0 deletions docs/api/ReactWrapper/key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `.key() => String`

Returns the key value for the node of the current wrapper.

NOTE: can only be called on a wrapper of a single node.

#### Example


```jsx
const wrapper = mount(
<ul>
{['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');
```
18 changes: 18 additions & 0 deletions docs/api/ShallowWrapper/key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `.key() => String`

Returns the key value for the node of the current wrapper.

NOTE: can only be called on a wrapper of a single node.

#### Example


```jsx
const wrapper = shallow(
<ul>
{['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');
```
3 changes: 3 additions & 0 deletions docs/api/mount.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ Returns the props of the root component.
#### [`.prop(key) => Any`](ReactWrapper/prop.md)
Returns the named prop of the root component.

#### [`.key() => String`](ReactWrapper/key.md)
Returns the key of the root component.

#### [`.simulate(event[, data]) => ReactWrapper`](ReactWrapper/simulate.md)
Simulates an event on the current node.

Expand Down
3 changes: 3 additions & 0 deletions docs/api/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ Returns the props of the root component.
#### [`.prop(key) => Any`](ShallowWrapper/prop.md)
Returns the named prop of the root component.

#### [`.key() => String`](ShallowWrapper/key.md)
Returns the key of the root component.

#### [`.simulate(event[, data]) => ShallowWrapper`](ShallowWrapper/simulate.md)
Simulates an event on the current node.

Expand Down
11 changes: 10 additions & 1 deletion src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,16 @@ export default class ReactWrapper {
}

/**
* Returns the type of the root ndoe of this wrapper. If it's a composite component, this will be
* Returns the key assigned to the current node.
*
* @returns {String}
*/
key() {
return this.single((n) => getNode(n).key);
}

/**
* Returns the type of the root node of this wrapper. If it's a composite component, this will be
* the component constructor. If it's native DOM node, it will be a string.
*
* @returns {String|Function}
Expand Down
11 changes: 10 additions & 1 deletion src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,16 @@ export default class ShallowWrapper {
}

/**
* Returns the type of the root ndoe of this wrapper. If it's a composite component, this will be
* Returns the key assigned to the current node.
*
* @returns {String}
*/
key() {
return this.single((n) => n.key);
}

/**
* Returns the type of the root node of this wrapper. If it's a composite component, this will be
* the component constructor. If it's native DOM node, it will be a string.
*
* @returns {String|Function}
Expand Down
12 changes: 12 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1884,4 +1884,16 @@ describeWithDOM('mount', () => {
expect(rendered.length).to.equal(0);
expect(rendered.html()).to.equal(null);
});

describe('.key()', () => {
it('should return the key of the node', () => {
const wrapper = mount(
<ul>
{['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');
});
});
});
12 changes: 12 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2232,4 +2232,16 @@ describe('shallow', () => {
});
});

describe('.key()', () => {
it('should return the key of the node', () => {
const wrapper = shallow(
<ul>
{['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');
});
});

});

0 comments on commit 1b9d317

Please sign in to comment.