-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New]
shallow
/mount
: add invoke(propName)(...args)
- Loading branch information
Showing
9 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# `.invoke(propName)(...args) => Any` | ||
|
||
Invokes a function prop. | ||
|
||
#### Arguments | ||
|
||
1. `propName` (`String`): The function prop that is invoked | ||
2. `...args` (`Any` [optional]): Arguments that is passed to the prop function | ||
|
||
|
||
|
||
#### Returns | ||
|
||
`Any`: Returns the value from the prop function | ||
|
||
#### Example | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
loadData() { | ||
return fetch(); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<button | ||
type="button" | ||
onClick={() => this.loadData()} | ||
> | ||
Load more | ||
</button> | ||
</div> | ||
); | ||
} | ||
} | ||
const wrapper = mount(<Foo />); | ||
wrapper.find('a').invoke('onClick')().then(() => { | ||
// expect() | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# `.invoke(invokePropName)(...args) => Any` | ||
|
||
Invokes a function prop. | ||
|
||
#### Arguments | ||
|
||
1. `propName` (`String`): The function prop that is invoked | ||
2. `...args` (`Any` [optional]): Arguments that is passed to the prop function | ||
|
||
This essentially calls wrapper.prop(propName)(...args). | ||
|
||
#### Returns | ||
|
||
`Any`: Returns the value from the prop function | ||
|
||
#### Example | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
loadData() { | ||
return fetch(); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<button | ||
type="button" | ||
onClick={() => this.loadData()} | ||
> | ||
Load more | ||
</button> | ||
</div> | ||
); | ||
} | ||
} | ||
const wrapper = shallow(<Foo />); | ||
wrapper.find('a').invoke('onClick')().then(() => { | ||
// expect() | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon-sandbox'; | ||
|
||
export default function describeInvoke({ | ||
Wrap, | ||
WrapperName, | ||
}) { | ||
describe('.invoke(propName)(..args)', () => { | ||
class CounterButton extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { count: 0 }; | ||
} | ||
|
||
render() { | ||
const { count } = this.state; | ||
return ( | ||
<div> | ||
<button | ||
type="button" | ||
onClick={() => this.setState(({ count: oldCount }) => ({ count: oldCount + 1 }))} | ||
> | ||
{count} | ||
</button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class ClickableLink extends React.Component { | ||
render() { | ||
const { onClick } = this.props; | ||
return ( | ||
<div> | ||
<a onClick={onClick}>foo</a> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
it('throws when pointing to a non-function prop', () => { | ||
const wrapper = Wrap(<div data-a={{}} />); | ||
|
||
expect(() => wrapper.invoke('data-a')).to.throw( | ||
TypeError, | ||
`${WrapperName}::invoke() requires the name of a prop whose value is a function`, | ||
); | ||
|
||
expect(() => wrapper.invoke('does not exist')).to.throw( | ||
TypeError, | ||
`${WrapperName}::invoke() requires the name of a prop whose value is a function`, | ||
); | ||
}); | ||
|
||
it('can update the state value', () => { | ||
const wrapper = Wrap(<CounterButton />); | ||
expect(wrapper.state('count')).to.equal(0); | ||
wrapper.find('button').invoke('onClick')(); | ||
expect(wrapper.state('count')).to.equal(1); | ||
}); | ||
|
||
it('can return the handlers’ return value', () => { | ||
const sentinel = {}; | ||
const spy = sinon.stub().returns(sentinel); | ||
|
||
const wrapper = Wrap(<ClickableLink onClick={spy} />); | ||
|
||
const value = wrapper.find('a').invoke('onClick')(); | ||
expect(value).to.equal(sentinel); | ||
expect(spy).to.have.property('callCount', 1); | ||
}); | ||
|
||
it('can pass in arguments', () => { | ||
const spy = sinon.spy(); | ||
|
||
const wrapper = Wrap(<ClickableLink onClick={spy} />); | ||
|
||
const a = {}; | ||
const b = {}; | ||
wrapper.find('a').invoke('onClick')(a, b); | ||
expect(spy).to.have.property('callCount', 1); | ||
const [[arg1, arg2]] = spy.args; | ||
expect(arg1).to.equal(a); | ||
expect(arg2).to.equal(b); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters