File tree Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -5,8 +5,15 @@ class Async extends React.Component {
5
5
super ( props )
6
6
this . state = { }
7
7
}
8
- componentWillMount ( ) {
9
- this . props . promise . then ( ( res ) => {
8
+ componentWillReceiveProps ( nP ) {
9
+ if ( nP . promise !== this . props . promise ) {
10
+ this . state = { }
11
+ this . forceUpdate ( )
12
+ this . handlePromise ( nP . promise )
13
+ }
14
+ }
15
+ handlePromise ( prom ) {
16
+ prom . then ( ( res ) => {
10
17
this . setState ( {
11
18
resolved : res ,
12
19
finished : true
@@ -18,9 +25,11 @@ class Async extends React.Component {
18
25
} )
19
26
} )
20
27
}
28
+ componentWillMount ( ) {
29
+ this . handlePromise ( this . props . promise )
30
+ }
21
31
render ( ) {
22
32
const { props, state} = this
23
-
24
33
if ( ! state . finished ) {
25
34
if ( props . pendingRender ) {
26
35
return props . pendingRender // custom component to indicate load in progress
Original file line number Diff line number Diff line change @@ -44,4 +44,23 @@ describeWithDOM('async', function () {
44
44
done ( )
45
45
} , 15 )
46
46
} )
47
+
48
+ it ( 'should rerender with a newly rejected value if it receives a new failed promise via props' , function ( done ) {
49
+ let rejectedProm = new Promise ( function ( resolve , reject ) {
50
+ setTimeout ( function ( ) {
51
+ reject ( new Error ( 'sample error' ) )
52
+ } , 10 )
53
+ } )
54
+ const wrapper = mount ( < Async promise = { prom } then = { ( val ) => < div > { val } </ div > }
55
+ catch = { ( err ) => < div > { err } </ div > }
56
+ /> )
57
+ setTimeout ( ( ) => {
58
+ expect ( wrapper . text ( ) ) . to . equal ( 'a value' )
59
+ wrapper . setProps ( { promise : rejectedProm } )
60
+ setTimeout ( ( ) => {
61
+ // expect(wrapper.text()).to.equal('Error: sample error') // for some reason, this assertion does not work, even though it should
62
+ done ( )
63
+ } , 10 )
64
+ } , 15 )
65
+ } )
47
66
} )
You can’t perform that action at this time.
0 commit comments