Skip to content

Commit

Permalink
Merge pull request #187 from mauriciosoares/mauriciosoares-patch-2
Browse files Browse the repository at this point in the history
Add a .childAt(index) method
  • Loading branch information
lelandrichardson committed Feb 16, 2016
2 parents 995f142 + da803c4 commit 68f1bf6
Show file tree
Hide file tree
Showing 11 changed files with 114 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 @@ -24,6 +24,7 @@
* [is(selector)](/docs/api/ShallowWrapper/is.md)
* [not(selector)](/docs/api/ShallowWrapper/not.md)
* [children()](/docs/api/ShallowWrapper/children.md)
* [childAt()](/docs/api/ShallowWrapper/childAt.md)
* [parents()](/docs/api/ShallowWrapper/parents.md)
* [parent()](/docs/api/ShallowWrapper/parent.md)
* [closest(selector)](/docs/api/ShallowWrapper/closest.md)
Expand Down Expand Up @@ -65,6 +66,7 @@
* [is(selector)](/docs/api/ReactWrapper/is.md)
* [not(selector)](/docs/api/ReactWrapper/not.md)
* [children()](/docs/api/ReactWrapper/children.md)
* [childAt()](/docs/api/ReactWrapper/childAt.md)
* [parents()](/docs/api/ReactWrapper/parents.md)
* [parent()](/docs/api/ReactWrapper/parent.md)
* [closest(selector)](/docs/api/ReactWrapper/closest.md)
Expand Down
28 changes: 28 additions & 0 deletions docs/api/ReactWrapper/childAt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# `.childAt(index) => ReactWrapper`

Returns a new wrapper with child at the specified index.

#### Arguments

1. `index` (`number`): A zero-based integer indicating which node to retrieve.


#### Returns

`ReactWrapper`: A new wrapper that wraps the resulting node.



#### Examples

```jsx
const wrapper = mount(<ToDoList items={items} />);
expect(wrapper.find('ul').childAt(0).type()).to.equal('li');
```

#### Related Methods

- [`.parents() => ReactWrapper`](parents.md)
- [`.parent() => ReactWrapper`](parent.md)
- [`.closest(selector) => ReactWrapper`](closest.md)
- [`.children() => ReactWrapper`](children.md)
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/children.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `.children([selector]) => ReactWrapper`

Returns a new wrapper with all of the children of the node(s) in the current wrapper. Optionally, a
Returns a new wrapper with all of the children of the node(s) in the current wrapper. Optionally, a
selector can be provided and it will filter the children by this selector


Expand Down
28 changes: 28 additions & 0 deletions docs/api/ShallowWrapper/childAt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# `.childAt(index) => ShallowWrapper`

Returns a new wrapper with child at the specified index.

#### Arguments

1. `index` (`number`): A zero-based integer indicating which node to retrieve.


#### Returns

`ShallowWrapper`: A new wrapper that wraps the resulting node.



#### Examples

```jsx
const wrapper = shallow(<ToDoList items={items} />);
expect(wrapper.find('ul').childAt(0).type()).to.equal('li');
```

#### Related Methods

- [`.parents() => ShallowWrapper`](parents.md)
- [`.parent() => ShallowWrapper`](parent.md)
- [`.closest(selector) => ShallowWrapper`](closest.md)
- [`.children() => ReactWrapper`](children.md)
2 changes: 1 addition & 1 deletion docs/api/ShallowWrapper/children.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `.children([selector]) => ShallowWrapper`

Returns a new wrapper with all of the children of the node(s) in the current wrapper. Optionally, a
Returns a new wrapper with all of the children of the node(s) in the current wrapper. Optionally, a
selector can be provided and it will filter the children by this selector


Expand Down
3 changes: 3 additions & 0 deletions docs/api/mount.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ Remove nodes in the current wrapper that match the provided selector. (inverse o
#### [`.children() => ReactWrapper`](ReactWrapper/children.md)
Get a wrapper with all of the children nodes of the current wrapper.

#### [`.childAt() => ReactWrapper`](ReactWrapper/childAt.md)
Returns a new wrapper with child at the specified index.

#### [`.parents() => ReactWrapper`](ReactWrapper/parents.md)
Get a wrapper with all of the parents (ancestors) of 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 @@ -85,6 +85,9 @@ Remove nodes in the current wrapper that match the provided selector. (inverse o
#### [`.children() => ShallowWrapper`](ShallowWrapper/children.md)
Get a wrapper with all of the children nodes of the current wrapper.

#### [`.childAt() => ShallowWrapper`](ShallowWrapper/childAt.md)
Returns a new wrapper with child at the specified index.

#### [`.parents() => ShallowWrapper`](ShallowWrapper/parents.md)
Get a wrapper with all of the parents (ancestors) of the current node.

Expand Down
10 changes: 10 additions & 0 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ export default class ReactWrapper {
return selector ? allChildren.filter(selector) : allChildren;
}

/**
* Returns a new wrapper with a specific child
*
* @param {Number} [index]
* @returns {ReactWrapper}
*/
childAt(index) {
return this.single(() => this.children().at(index));
}

/**
* Returns a wrapper around all of the parents/ancestors of the wrapper. Does not include the node
* in the current wrapper.
Expand Down
10 changes: 10 additions & 0 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,16 @@ export default class ShallowWrapper {
return selector ? allChildren.filter(selector) : allChildren;
}

/**
* Returns a new wrapper with a specific child
*
* @param {Number} [index]
* @returns {ShallowWrapper}
*/
childAt(index) {
return this.single(() => this.children().at(index));
}

/**
* Returns a wrapper around all of the parents/ancestors of the wrapper. Does not include the node
* in the current wrapper.
Expand Down
14 changes: 14 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,20 @@ describeWithDOM('mount', () => {
});
});

describe('.childAt(index)', () => {
it('should get a wrapped node at the specified index', () => {
const wrapper = mount(
<div>
<div className="bar" />
<div className="baz" />
</div>
);

expect(wrapper.childAt(0).hasClass('bar')).to.equal(true);
expect(wrapper.childAt(1).hasClass('baz')).to.equal(true);
});
});

describe('.parents([selector])', () => {
it('should return an array of current nodes ancestors', () => {
const wrapper = mount(
Expand Down
14 changes: 14 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,20 @@ describe('shallow', () => {
});
});

describe('.childAt(index)', () => {
it('should get a wrapped node at the specified index', () => {
const wrapper = shallow(
<div>
<div className="bar" />
<div className="baz" />
</div>
);

expect(wrapper.childAt(0).hasClass('bar')).to.equal(true);
expect(wrapper.childAt(1).hasClass('baz')).to.equal(true);
});
});

describe('.parents([selector])', () => {
it('should return an array of current nodes ancestors', () => {
const wrapper = shallow(
Expand Down

0 comments on commit 68f1bf6

Please sign in to comment.