1
1
import expect from 'expect' ;
2
2
import jsdom from 'mocha-jsdom' ;
3
3
import React , { createClass , Children , PropTypes , Component } from 'react' ;
4
+ import ReactDOM from 'react-dom' ;
4
5
import TestUtils from 'react-addons-test-utils' ;
5
6
import { createStore } from 'redux' ;
6
7
import { connect } from '../../src/index' ;
@@ -643,7 +644,7 @@ describe('React', () => {
643
644
} ;
644
645
645
646
@connect (
646
- state => ( { string : state } ) ,
647
+ state => ( { string : state } ) ,
647
648
dispatch => ( { dispatch } )
648
649
)
649
650
class Container extends Component {
@@ -652,18 +653,52 @@ describe('React', () => {
652
653
}
653
654
}
654
655
655
- const tree = TestUtils . renderIntoDocument (
656
+ const div = document . createElement ( 'div' ) ;
657
+ ReactDOM . render (
656
658
< ProviderMock store = { store } >
657
659
< Container />
658
- </ ProviderMock >
660
+ </ ProviderMock > ,
661
+ div
659
662
) ;
660
663
661
- const connector = TestUtils . findRenderedComponentWithType ( tree , Container ) ;
662
664
expect ( spy . calls . length ) . toBe ( 0 ) ;
663
- connector . componentWillUnmount ( ) ;
665
+ ReactDOM . unmountComponentAtNode ( div ) ;
664
666
expect ( spy . calls . length ) . toBe ( 1 ) ;
665
667
} ) ;
666
668
669
+ it ( 'should not attempt to set state after unmounting' , ( ) => {
670
+ const store = createStore ( stringBuilder ) ;
671
+ let mapStateToPropsCalls = 0 ;
672
+
673
+ @connect (
674
+ ( ) => ( { calls : ++ mapStateToPropsCalls } ) ,
675
+ dispatch => ( { dispatch } )
676
+ )
677
+ class Container extends Component {
678
+ render ( ) {
679
+ return < Passthrough { ...this . props } /> ;
680
+ }
681
+ }
682
+
683
+ const div = document . createElement ( 'div' ) ;
684
+ store . subscribe ( ( ) =>
685
+ ReactDOM . unmountComponentAtNode ( div )
686
+ ) ;
687
+ ReactDOM . render (
688
+ < ProviderMock store = { store } >
689
+ < Container />
690
+ </ ProviderMock > ,
691
+ div
692
+ ) ;
693
+
694
+ expect ( mapStateToPropsCalls ) . toBe ( 2 ) ;
695
+ const spy = expect . spyOn ( console , 'error' ) ;
696
+ store . dispatch ( { type : 'APPEND' , body : 'a' } ) ;
697
+ spy . destroy ( ) ;
698
+ expect ( spy . calls . length ) . toBe ( 0 ) ;
699
+ expect ( mapStateToPropsCalls ) . toBe ( 2 ) ;
700
+ } ) ;
701
+
667
702
it ( 'should shallowly compare the selected state to prevent unnecessary updates' , ( ) => {
668
703
const store = createStore ( stringBuilder ) ;
669
704
const spy = expect . createSpy ( ( ) => ( { } ) ) ;
0 commit comments