-
-
Notifications
You must be signed in to change notification settings - Fork 2k
[Fix] shallow: .setState(): stub out setState on non-root code paths as well.
#1763
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
Changes from all commits
e590784
b412ec8
af8884b
8b97f1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2142,7 +2142,7 @@ describe('shallow', () => { | |
| expect(wrapper.find('.bar')).to.have.lengthOf(1); | ||
| }); | ||
|
|
||
| it.skip('allows setState inside of componentDidMount', () => { | ||
| it('allows setState inside of componentDidMount', () => { | ||
| class MySharona extends React.Component { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
@@ -2233,6 +2233,31 @@ describe('shallow', () => { | |
| expect(wrapper.state()).to.eql({ id: 'foo' }); | ||
| expect(() => wrapper.setState({ id: 'bar' }, 1)).to.throw(Error); | ||
| }); | ||
|
|
||
| it('should preserve the receiver', () => { | ||
| class Comp extends React.Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
|
|
||
| this.state = { | ||
| key: '', | ||
| }; | ||
|
|
||
| this.instanceFunction = () => this.setState(() => ({ key: 'value' })); | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| this.instanceFunction(); | ||
| } | ||
|
|
||
| render() { | ||
| const { key } = this.state; | ||
| return key ? null : null; | ||
| } | ||
| } | ||
|
|
||
| expect(shallow(<Comp />).debug()).to.equal(''); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should; cDU is invoked whenever the component updates. no?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to be called cDU, we should stub
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll write the tests and the patch. |
||
| }); | ||
| }); | ||
|
|
||
| describe('.is(selector)', () => { | ||
|
|
@@ -4942,6 +4967,33 @@ describe('shallow', () => { | |
| expect(wrapper.state('foo')).to.equal('onChange update'); | ||
| expect(spy).to.have.property('callCount', 1); | ||
| }); | ||
|
|
||
| it('should call `componentDidUpdate` when component’s `setState` is called', () => { | ||
| class Foo extends React.Component { | ||
| constructor(props) { | ||
| super(props); | ||
| this.state = { | ||
| foo: 'init', | ||
| }; | ||
| this.update = () => this.setState({ foo: 'update' }); | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| this.update(); | ||
| } | ||
|
|
||
| componentDidUpdate() {} | ||
|
|
||
| render() { | ||
| return <div>{this.state.foo}</div>; | ||
| } | ||
| } | ||
| const spy = sinon.spy(Foo.prototype, 'componentDidUpdate'); | ||
|
|
||
| const wrapper = shallow(<Foo />); | ||
| expect(spy).to.have.property('callCount', 1); | ||
| expect(wrapper.state('foo')).to.equal('update'); | ||
| }); | ||
| }); | ||
|
|
||
| describeIf(is('>= 16'), 'support getSnapshotBeforeUpdate', () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should not allow setState inside of componentDidMount
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would it not?