Last active
August 6, 2017 14:40
-
-
Save visualskyrim/c0d5a4c6f83c4bba409674b59209cad8 to your computer and use it in GitHub Desktop.
Use Enzyme to test React/Redux container - Code List 1
This file contains hidden or 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 PropTypes from 'prop-types' | |
import { connect } from 'react-redux' | |
import { submitValue } from '../store/modules/showBox' | |
export class ShowBox extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
searchString: this.props.search || "" | |
} | |
} | |
handleInput = (event) => { | |
this.setState({ | |
searchString: event.target.value | |
}) | |
} | |
render () { | |
return ( | |
<form onSubmit={(e) => this.props.handleShowSubmit(this.state.searchString, e)}> | |
<div> | |
<input | |
type="search" | |
className="form-control" | |
placeholder="Search" | |
value={this.state.searchString} | |
onChange={this.handleInput} | |
/> | |
<div> | |
<i className="icon-search"></i> | |
</div> | |
</div> | |
</form> | |
) | |
} | |
} | |
export default connect( | |
(state) => ({ | |
search: state.showBox.search, | |
}), | |
(dispatch) => { | |
return { | |
handleShowSubmit: (text, e) => { | |
if (e) { | |
// Avoid redirecting | |
e.preventDefault() | |
} | |
dispatch(submitValue(text)) | |
} | |
} | |
} | |
)(ShowBox) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment