@@ -2,6 +2,7 @@ import React from 'react';
22import { expect } from 'chai' ;
33
44import {
5+ delay ,
56 describeIf ,
67 itIf ,
78} from '../../_helpers' ;
@@ -122,5 +123,50 @@ export default function describeProps({
122123 } ) ;
123124 } ) ;
124125 } ) ;
126+
127+ describe ( 'props in async handler' , ( ) => {
128+ class TestComponent extends React . Component {
129+ constructor ( props ) {
130+ super ( props ) ;
131+ this . state = { counter : 1 } ;
132+ this . handleClick = this . handleClick . bind ( this ) ;
133+ }
134+
135+ handleClick ( ) {
136+ return delay ( 100 ) . then ( ( ) => new Promise ( ( resolve ) => {
137+ this . setState ( { counter : 2 } , ( ) => {
138+ resolve ( ) ;
139+ } ) ;
140+ } ) ) ;
141+ }
142+
143+ render ( ) {
144+ const { counter } = this . state ;
145+ return (
146+ < div id = "parentDiv" onClick = { this . handleClick } >
147+ < TestSubComponent id = "childDiv" counter = { counter } />
148+ </ div >
149+ ) ;
150+ }
151+ }
152+
153+ class TestSubComponent extends React . Component {
154+ render ( ) {
155+ const { counter } = this . props ;
156+ return < div > { counter } </ div > ;
157+ }
158+ }
159+
160+ it ( 'child component props should update after call to setState in async handler' , ( ) => {
161+ const wrapper = Wrap ( < TestComponent /> ) ;
162+ expect ( wrapper . find ( TestSubComponent ) . props ( ) ) . to . eql ( { id : 'childDiv' , counter : 1 } ) ;
163+ const promise = wrapper . find ( '#parentDiv' ) . props ( ) . onClick ( ) ;
164+ expect ( wrapper . find ( TestSubComponent ) . props ( ) ) . to . eql ( { id : 'childDiv' , counter : 1 } ) ;
165+ return promise . then ( ( ) => {
166+ wrapper . update ( ) ;
167+ expect ( wrapper . find ( TestSubComponent ) . props ( ) ) . to . eql ( { id : 'childDiv' , counter : 2 } ) ;
168+ } ) ;
169+ } ) ;
170+ } ) ;
125171 } ) ;
126172}
0 commit comments