Skip to content

Commit

Permalink
Made get return raw node and added an at method
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandrichardson committed Nov 16, 2015
1 parent 0134ebf commit 7a3209d
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 72 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* [text()](/docs/api/ShallowWrapper/text.md)
* [html()](/docs/api/ShallowWrapper/html.md)
* [get(index)](/docs/api/ShallowWrapper/get.md)
* [at(index)](/docs/api/ShallowWrapper/at.md)
* [first()](/docs/api/ShallowWrapper/first.md)
* [last()](/docs/api/ShallowWrapper/last.md)
* [state([key])](/docs/api/ShallowWrapper/state.md)
Expand Down
23 changes: 23 additions & 0 deletions docs/api/ShallowWrapper/at.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# `.at(index) => ShallowWrapper`

Returns a wrapper around the node at a given index of the current wrapper.


#### Arguments

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



#### Returns

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



#### Examples

```jsx
const wrapper = shallow(<MyComponent />);
expect(wrapper.find(Foo).at(0).props().foo).to.equal("bar");
```
8 changes: 4 additions & 4 deletions docs/api/ShallowWrapper/get.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `.get(index) => ShallowWrapper`
# `.get(index) => ReactElement`

Returns a wrapper around the node at a given index of the current wrapper.
Returns the node at a given index of the current wrapper.


#### Arguments
Expand All @@ -11,13 +11,13 @@ Returns a wrapper around the node at a given index of the current wrapper.

#### Returns

`ShallowWrapper`: A new wrapper that wraps the retrieved node.
`ReactElement`: The retrieved node.



#### Examples

```jsx
const wrapper = shallow(<MyComponent />);
expect(wrapper.find(Foo).get(0).props().foo).to.equal("bar");
expect(wrapper.find(Foo).get(0).props.foo).to.equal("bar");
```
3 changes: 3 additions & 0 deletions docs/api/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ Returns a string representation of the text nodes in the current render tree.
Returns a static HTML rendering of the current node.

#### [`.get(index) => ShallowWrapper`](ShallowWrapper/get.md)
Returns the node at the provided index of the current wrapper.

#### [`.at(index) => ShallowWrapper`](ShallowWrapper/at.md)
Returns a wrapper of the node at the provided index of the current wrapper.

#### [`.first() => ShallowWrapper`](ShallowWrapper/first.md)
Expand Down
18 changes: 14 additions & 4 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export default class ReactWrapper {
* @returns {ReactWrapper}
*/
parent() {
return this.flatMap(n => [n.parents().nodes[0]]);
return this.flatMap(n => [n.parents().get(0)]);
}

/**
Expand Down Expand Up @@ -482,13 +482,23 @@ export default class ReactWrapper {
return this.flatMap(n => treeFilter(n.node, predicate));
}

/**
* Returns the node at a given index of the current wrapper.
*
* @param {Number} index
* @returns {ReactElement}
*/
get(index) {
return this.nodes[index];
}

/**
* Returns a wrapper around the node at a given index of the current wrapper.
*
* @param {Number} index
* @returns {ReactWrapper}
*/
get(index) {
at(index) {
return this.wrap(this.nodes[index]);
}

Expand All @@ -498,7 +508,7 @@ export default class ReactWrapper {
* @returns {ReactWrapper}
*/
first() {
return this.get(0);
return this.at(0);
}

/**
Expand All @@ -507,7 +517,7 @@ export default class ReactWrapper {
* @returns {ReactWrapper}
*/
last() {
return this.get(this.length - 1);
return this.at(this.length - 1);
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export default class ShallowWrapper {
* @returns {ShallowWrapper}
*/
parent() {
return this.flatMap(n => [n.parents().nodes[0]]);
return this.flatMap(n => [n.parents().get(0)]);
}

/**
Expand Down Expand Up @@ -514,13 +514,23 @@ export default class ShallowWrapper {
return this.flatMap(n => treeFilter(n.node, predicate));
}

/**
* Returns the node at a given index of the current wrapper.
*
* @param index
* @returns {ReactElement}
*/
get(index) {
return this.nodes[index];
}

/**
* Returns a wrapper around the node at a given index of the current wrapper.
*
* @param index
* @returns {ShallowWrapper}
*/
get(index) {
at(index) {
return this.wrap(this.nodes[index]);
}

Expand All @@ -530,7 +540,7 @@ export default class ShallowWrapper {
* @returns {ShallowWrapper}
*/
first() {
return this.get(0);
return this.at(0);
}

/**
Expand All @@ -539,7 +549,7 @@ export default class ShallowWrapper {
* @returns {ShallowWrapper}
*/
last() {
return this.get(this.length - 1);
return this.at(this.length - 1);
}

/**
Expand Down
77 changes: 47 additions & 30 deletions src/__tests__/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ describeWithDom('mount', () => {
</div>
);
expect(wrapper.children().length).to.equal(3);
expect(wrapper.children().get(0).hasClass('foo')).to.be.true;
expect(wrapper.children().get(1).hasClass('bar')).to.be.true;
expect(wrapper.children().get(2).hasClass('baz')).to.be.true;
expect(wrapper.children().at(0).hasClass('foo')).to.be.true;
expect(wrapper.children().at(1).hasClass('bar')).to.be.true;
expect(wrapper.children().at(2).hasClass('baz')).to.be.true;
});

it('should not return any of the children of children', () => {
Expand All @@ -525,8 +525,8 @@ describeWithDom('mount', () => {
</div>
);
expect(wrapper.children().length).to.equal(2);
expect(wrapper.children().get(0).hasClass('foo')).to.be.true;
expect(wrapper.children().get(1).hasClass('baz')).to.be.true;
expect(wrapper.children().at(0).hasClass('foo')).to.be.true;
expect(wrapper.children().at(1).hasClass('baz')).to.be.true;
});

it('should handle mixed children with and without arrays', () => {
Expand All @@ -547,9 +547,9 @@ describeWithDom('mount', () => {
]} />
);
expect(wrapper.children().length).to.equal(3);
expect(wrapper.children().get(0).hasClass('foo')).to.be.true;
expect(wrapper.children().get(1).hasClass('bar')).to.be.true;
expect(wrapper.children().get(2).hasClass('baz')).to.be.true;
expect(wrapper.children().at(0).hasClass('foo')).to.be.true;
expect(wrapper.children().at(1).hasClass('bar')).to.be.true;
expect(wrapper.children().at(2).hasClass('baz')).to.be.true;
});

it('should optionally allow a selector to filter by', () => {
Expand All @@ -562,8 +562,8 @@ describeWithDom('mount', () => {
);
const children = wrapper.children('.bip');
expect(children.length).to.equal(2);
expect(children.get(0).hasClass('bar')).to.be.true;
expect(children.get(1).hasClass('baz')).to.be.true;
expect(children.at(0).hasClass('bar')).to.be.true;
expect(children.at(1).hasClass('baz')).to.be.true;
});
});

Expand All @@ -582,9 +582,9 @@ describeWithDom('mount', () => {
const parents = wrapper.find('.baz').parents();

expect(parents.length).to.equal(3);
expect(parents.get(0).hasClass('bar')).to.be.true;
expect(parents.get(1).hasClass('foo')).to.be.true;
expect(parents.get(2).hasClass('bax')).to.be.true;
expect(parents.at(0).hasClass('bar')).to.be.true;
expect(parents.at(1).hasClass('foo')).to.be.true;
expect(parents.at(2).hasClass('bax')).to.be.true;

});

Expand All @@ -602,8 +602,8 @@ describeWithDom('mount', () => {
const parents = wrapper.find('.bar').parents();

expect(parents.length).to.equal(2);
expect(parents.get(0).hasClass('foo')).to.be.true;
expect(parents.get(1).hasClass('bax')).to.be.true;
expect(parents.at(0).hasClass('foo')).to.be.true;
expect(parents.at(1).hasClass('bax')).to.be.true;
});

it('should optionally allow a selector', () => {
Expand All @@ -620,8 +620,8 @@ describeWithDom('mount', () => {
const parents = wrapper.find('.baz').parents('.foo');

expect(parents.length).to.equal(2);
expect(parents.get(0).hasClass('foo')).to.be.true;
expect(parents.get(1).hasClass('bax')).to.be.true;
expect(parents.at(0).hasClass('foo')).to.be.true;
expect(parents.at(1).hasClass('bax')).to.be.true;
});
});

Expand Down Expand Up @@ -657,9 +657,9 @@ describeWithDom('mount', () => {

const parents = wrapper.find('.baz').parent();
expect(parents).to.have.length(3);
expect(parents.get(0).hasClass('foo')).to.be.true;
expect(parents.get(1).hasClass('bar')).to.be.true;
expect(parents.get(2).hasClass('bax')).to.be.true;
expect(parents.at(0).hasClass('foo')).to.be.true;
expect(parents.at(1).hasClass('bar')).to.be.true;
expect(parents.at(2).hasClass('bax')).to.be.true;
});
});

Expand Down Expand Up @@ -961,12 +961,12 @@ describeWithDom('mount', () => {
const nodes = wrapper.find('.foo').flatMap(w => w.children().nodes);

expect(nodes.length).to.equal(6);
expect(nodes.get(0).hasClass('bar')).to.be.true;
expect(nodes.get(1).hasClass('bar')).to.be.true;
expect(nodes.get(2).hasClass('baz')).to.be.true;
expect(nodes.get(3).hasClass('baz')).to.be.true;
expect(nodes.get(4).hasClass('bax')).to.be.true;
expect(nodes.get(5).hasClass('bax')).to.be.true;
expect(nodes.at(0).hasClass('bar')).to.be.true;
expect(nodes.at(1).hasClass('bar')).to.be.true;
expect(nodes.at(2).hasClass('baz')).to.be.true;
expect(nodes.at(3).hasClass('baz')).to.be.true;
expect(nodes.at(4).hasClass('bax')).to.be.true;
expect(nodes.at(5).hasClass('bax')).to.be.true;
});
});

Expand Down Expand Up @@ -1008,6 +1008,23 @@ describeWithDom('mount', () => {
});
});

describe('.at(index)', () => {
it('gets a wrapper of the node at the specified index', () => {
const wrapper = mount(
<div>
<div className="bar foo" />
<div className="bar bax" />
<div className="bar bux" />
<div className="bar baz" />
</div>
);
expect(wrapper.find('.bar').at(0).hasClass('foo')).to.be.true;
expect(wrapper.find('.bar').at(1).hasClass('bax')).to.be.true;
expect(wrapper.find('.bar').at(2).hasClass('bux')).to.be.true;
expect(wrapper.find('.bar').at(3).hasClass('baz')).to.be.true;
});
});

describe('.get(index)', () => {
it('gets the node at the specified index', () => {
const wrapper = mount(
Expand All @@ -1018,10 +1035,10 @@ describeWithDom('mount', () => {
<div className="bar baz" />
</div>
);
expect(wrapper.find('.bar').get(0).hasClass('foo')).to.be.true;
expect(wrapper.find('.bar').get(1).hasClass('bax')).to.be.true;
expect(wrapper.find('.bar').get(2).hasClass('bux')).to.be.true;
expect(wrapper.find('.bar').get(3).hasClass('baz')).to.be.true;
expect(wrapper.find('.bar').get(0)).to.equal(wrapper.find('.foo').node);
expect(wrapper.find('.bar').get(1)).to.equal(wrapper.find('.bax').node);
expect(wrapper.find('.bar').get(2)).to.equal(wrapper.find('.bux').node);
expect(wrapper.find('.bar').get(3)).to.equal(wrapper.find('.baz').node);
});
});

Expand Down
Loading

0 comments on commit 7a3209d

Please sign in to comment.