@@ -3351,8 +3351,8 @@ describe('shallow', () => {
3351
3351
}
3352
3352
}
3353
3353
const result = shallow ( < Foo /> , { lifecycleExperimental : true } ) ;
3354
- expect ( result . state ( 'count' ) ) . to . equal ( 2 ) ;
3355
- expect ( spy . callCount ) . to . equal ( 2 ) ;
3354
+ expect ( result . state ( ) ) . to . have . property ( 'count' , 2 ) ;
3355
+ expect ( spy ) . to . have . property ( 'callCount' , 2 ) ;
3356
3356
} ) ;
3357
3357
} ) ;
3358
3358
@@ -3382,19 +3382,17 @@ describe('shallow', () => {
3382
3382
}
3383
3383
render ( ) {
3384
3384
spy ( 'render' ) ;
3385
- return < div > { this . state . foo } </ div > ;
3385
+ const { foo } = this . state ;
3386
+ return < div > { foo } </ div > ;
3386
3387
}
3387
3388
}
3388
3389
Foo . contextTypes = {
3389
3390
foo : PropTypes . string ,
3390
3391
} ;
3391
3392
3392
- const wrapper = shallow (
3393
- < Foo foo = "bar" /> ,
3394
- {
3395
- context : { foo : 'context' } ,
3396
- } ,
3397
- ) ;
3393
+ const wrapper = shallow ( < Foo foo = "bar" /> , {
3394
+ context : { foo : 'context' } ,
3395
+ } ) ;
3398
3396
wrapper . setProps ( { foo : 'baz' } ) ;
3399
3397
wrapper . setProps ( { foo : 'bax' } ) ;
3400
3398
expect ( spy . args ) . to . deep . equal ( [
@@ -3976,6 +3974,40 @@ describe('shallow', () => {
3976
3974
} ) ;
3977
3975
} ) ;
3978
3976
3977
+ context ( 'component instance' , ( ) => {
3978
+ it ( 'should call `componentDidUpdate` when component’s `setState` is called' , ( ) => {
3979
+ const spy = sinon . spy ( ) ;
3980
+ class Foo extends React . Component {
3981
+ constructor ( props ) {
3982
+ super ( props ) ;
3983
+ this . state = {
3984
+ foo : 'init' ,
3985
+ } ;
3986
+ }
3987
+ componentDidUpdate ( ) {
3988
+ spy ( ) ;
3989
+ }
3990
+ onChange ( ) {
3991
+ // enzyme can't handle the update because `this` is a ReactComponent instance,
3992
+ // not a ShallowWrapper instance.
3993
+ this . setState ( { foo : 'onChange update' } ) ;
3994
+ }
3995
+ render ( ) {
3996
+ return < div > { this . state . foo } </ div > ;
3997
+ }
3998
+ }
3999
+ const wrapper = shallow ( < Foo /> ) ;
4000
+
4001
+ wrapper . setState ( { foo : 'wrapper setState update' } ) ;
4002
+ expect ( wrapper . state ( 'foo' ) ) . to . equal ( 'wrapper setState update' ) ;
4003
+ expect ( spy ) . to . have . property ( 'callCount' , 1 ) ;
4004
+
4005
+ wrapper . instance ( ) . onChange ( ) ;
4006
+ expect ( wrapper . state ( 'foo' ) ) . to . equal ( 'onChange update' ) ;
4007
+ expect ( spy ) . to . have . property ( 'callCount' , 2 ) ;
4008
+ } ) ;
4009
+ } ) ;
4010
+
3979
4011
describeIf ( REACT16 , 'support getSnapshotBeforeUpdate' , ( ) => {
3980
4012
it ( 'should call getSnapshotBeforeUpdate and pass snapshot to componentDidUpdate' , ( ) => {
3981
4013
const spy = sinon . spy ( ) ;
0 commit comments