Skip to content

V7 beta failing tests #1208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 102 additions & 1 deletion test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { Component } from 'react'
import createClass from 'create-react-class'
import PropTypes from 'prop-types'
import ReactDOM from 'react-dom'
import { createStore } from 'redux'
import { createStore, applyMiddleware } from 'redux'
import { Provider as ProviderMock, connect } from '../../src/index.js'
import * as rtl from 'react-testing-library'
import 'jest-dom/extend-expect'
Expand Down Expand Up @@ -2954,5 +2954,106 @@ describe('React', () => {
connect()(createComp('div'))
}).not.toThrow()
})

it('should invoke mapState always with latest props', () => {
const store = createStore((state = 0) => state + 1)

let propsPassedIn

@connect(reduxCount => {
return { reduxCount }
})
class InnerComponent extends Component {
render() {
propsPassedIn = this.props
return <Passthrough {...this.props} />
}
}

class OuterComponent extends Component {
constructor() {
super()
this.state = { count: 0 }
}

render() {
return <InnerComponent {...this.state} />
}
}

let outerComponent
rtl.render(
<ProviderMock store={store}>
<OuterComponent ref={c => (outerComponent = c)} />
</ProviderMock>
)
outerComponent.setState(({ count }) => ({ count: count + 1 }))
store.dispatch({ type: '' })

expect(propsPassedIn.count).toEqual(1)
expect(propsPassedIn.reduxCount).toEqual(2)
})

it('should use the latest props when updated between actions', () => {
const store = applyMiddleware(store => {
let callback
return next => action => {
if (action.type === 'SET_COMPONENT_CALLBACK') {
callback = action.payload
}
if (callback && action.type === 'INC1') {
next(action)
callback()
store.dispatch({ type: 'INC2' })
return
}
next(action)
}
})(createStore)((state = 0, action) => {
if (action.type === 'INC1') {
return state + 1
} else if (action.type === 'INC2') {
return state + 2
}
return state
})
const Child = connect(count => ({ count }))(function(props) {
return (
<div
data-testid="child"
data-prop={props.prop}
data-count={props.count}
/>
)
})
class Parent extends Component {
constructor() {
super()
this.state = {
prop: 'a'
}
this.inc1 = () => store.dispatch({ type: 'INC1' })
store.dispatch({
type: 'SET_COMPONENT_CALLBACK',
payload: () => this.setState({ prop: 'b' })
})
}

render() {
return (
<ProviderMock store={store}>
<Child prop={this.state.prop} />
</ProviderMock>
)
}
}
let parent
const rendered = rtl.render(<Parent ref={ref => (parent = ref)} />)
expect(rendered.getByTestId('child').dataset.count).toEqual('0')
expect(rendered.getByTestId('child').dataset.prop).toEqual('a')
parent.inc1()
expect(rendered.getByTestId('child').dataset.count).toEqual('3')
expect(rendered.getByTestId('child').dataset.prop).toEqual('b')
})
})
})