Testing complex redux dispatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ActionCard = ({id, dataTestId, icon, label, onClick, dispatch}) => { | |
const onActionCardClick = () => { | |
dispatch((dispatch, getState) => onClick(dispatch, getState())); | |
}; | |
return ( | |
<div id={id} data-test-id={dataTestId} className='action-card' onClick={onActionCardClick}> | |
<Icon type={icon}/> | |
<Truncate wrapper={(<Highlighter/>)}>{i18next.t(label)}</Truncate> | |
</div> | |
); | |
}; | |
ActionCard.propTypes = propTypes; | |
ActionCard.defaultProps = defaultProps; | |
export default connect()(ActionCard); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import {shallow} from 'enzyme'; | |
import {expect} from 'chai'; | |
import sinon from 'sinon'; | |
import Icon from 'components/general/Icon'; | |
import Truncate from 'components/general/Truncate'; | |
import Highlighter from 'components/general/Highlighter'; | |
import ActionCard from './ActionCard'; | |
describe('<ActionCard/>', () => { | |
const props = { | |
type: 'success', | |
icon: 'check-circle', | |
label: 'actioncardupdate_success', | |
onClick: sinon.spy(), | |
dispatch: sinon.spy(), | |
}; | |
const wrapper = shallow(<ActionCard.WrappedComponent {...props}/>); | |
describe.only('Events', () => { | |
it('should call the onClick function prop when clicked', () => { | |
const getStateSpy = sinon.stub().returns('getState'); | |
wrapper.find('.action-card').simulate('click'); | |
// retrieve the function passed to `dispatch` | |
const [dispatchFn] = props.dispatch.firstCall.args; | |
// call the function passed to `dispatch` with the Sinon spies | |
dispatchFn(props.dispatch, getStateSpy); | |
expect(props.onClick.calledOnce).to.be.true; | |
expect(props.onClick.firstCall.args[0]).to.equal(props.dispatch); | |
expect(props.onClick.firstCall.args[1]).to.equal('getState'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment