@@ -4,7 +4,7 @@ import React, { Component } from 'react'
44import createClass from 'create-react-class'
55import PropTypes from 'prop-types'
66import ReactDOM from 'react-dom'
7- import { createStore } from 'redux'
7+ import { createStore , applyMiddleware } from 'redux'
88import { Provider as ProviderMock , connect } from '../../src/index.js'
99import * as rtl from 'react-testing-library'
1010import 'jest-dom/extend-expect'
@@ -2954,5 +2954,106 @@ describe('React', () => {
29542954 connect ( ) ( createComp ( 'div' ) )
29552955 } ) . not . toThrow ( )
29562956 } )
2957+
2958+ it ( 'should invoke mapState always with latest props' , ( ) => {
2959+ const store = createStore ( ( state = 0 ) => state + 1 )
2960+
2961+ let propsPassedIn
2962+
2963+ @connect ( reduxCount => {
2964+ return { reduxCount }
2965+ } )
2966+ class InnerComponent extends Component {
2967+ render ( ) {
2968+ propsPassedIn = this . props
2969+ return < Passthrough { ...this . props } />
2970+ }
2971+ }
2972+
2973+ class OuterComponent extends Component {
2974+ constructor ( ) {
2975+ super ( )
2976+ this . state = { count : 0 }
2977+ }
2978+
2979+ render ( ) {
2980+ return < InnerComponent { ...this . state } />
2981+ }
2982+ }
2983+
2984+ let outerComponent
2985+ rtl . render (
2986+ < ProviderMock store = { store } >
2987+ < OuterComponent ref = { c => ( outerComponent = c ) } />
2988+ </ ProviderMock >
2989+ )
2990+ outerComponent . setState ( ( { count } ) => ( { count : count + 1 } ) )
2991+ store . dispatch ( { type : '' } )
2992+
2993+ expect ( propsPassedIn . count ) . toEqual ( 1 )
2994+ expect ( propsPassedIn . reduxCount ) . toEqual ( 2 )
2995+ } )
2996+
2997+ it ( 'should use the latest props when updated between actions' , ( ) => {
2998+ const store = applyMiddleware ( store => {
2999+ let callback
3000+ return next => action => {
3001+ if ( action . type === 'SET_COMPONENT_CALLBACK' ) {
3002+ callback = action . payload
3003+ }
3004+ if ( callback && action . type === 'INC1' ) {
3005+ next ( action )
3006+ callback ( )
3007+ store . dispatch ( { type : 'INC2' } )
3008+ return
3009+ }
3010+ next ( action )
3011+ }
3012+ } ) ( createStore ) ( ( state = 0 , action ) => {
3013+ if ( action . type === 'INC1' ) {
3014+ return state + 1
3015+ } else if ( action . type === 'INC2' ) {
3016+ return state + 2
3017+ }
3018+ return state
3019+ } )
3020+ const Child = connect ( count => ( { count } ) ) ( function ( props ) {
3021+ return (
3022+ < div
3023+ data-testid = "child"
3024+ data-prop = { props . prop }
3025+ data-count = { props . count }
3026+ />
3027+ )
3028+ } )
3029+ class Parent extends Component {
3030+ constructor ( ) {
3031+ super ( )
3032+ this . state = {
3033+ prop : 'a'
3034+ }
3035+ this . inc1 = ( ) => store . dispatch ( { type : 'INC1' } )
3036+ store . dispatch ( {
3037+ type : 'SET_COMPONENT_CALLBACK' ,
3038+ payload : ( ) => this . setState ( { prop : 'b' } )
3039+ } )
3040+ }
3041+
3042+ render ( ) {
3043+ return (
3044+ < ProviderMock store = { store } >
3045+ < Child prop = { this . state . prop } />
3046+ </ ProviderMock >
3047+ )
3048+ }
3049+ }
3050+ let parent
3051+ const rendered = rtl . render ( < Parent ref = { ref => ( parent = ref ) } /> )
3052+ expect ( rendered . getByTestId ( 'child' ) . dataset . count ) . toEqual ( '0' )
3053+ expect ( rendered . getByTestId ( 'child' ) . dataset . prop ) . toEqual ( 'a' )
3054+ parent . inc1 ( )
3055+ expect ( rendered . getByTestId ( 'child' ) . dataset . count ) . toEqual ( '3' )
3056+ expect ( rendered . getByTestId ( 'child' ) . dataset . prop ) . toEqual ( 'b' )
3057+ } )
29573058 } )
29583059} )
0 commit comments