Skip to content

Instantly share code, notes, and snippets.

@zrod
Created January 15, 2017 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zrod/d9ef49c8f1c2c35363fb068fedaea9dd to your computer and use it in GitHub Desktop.
Save zrod/d9ef49c8f1c2c35363fb068fedaea9dd to your computer and use it in GitHub Desktop.
import React, {PropTypes} from 'react';
import {Form} from 'semantic-ui-react';
import I18n from '../../helpers/I18n';
const AdForm = ({ad, onChange}) => {
return (
<Form>
<h1>{i18n.ad.new}</h1>
<Form.Input
name="title"
label={i18n.ad.form.title}
value={ad.title}
/>
<Form.TextArea
name="txt"
label={i18n.ad.form.txt}
onChange={onChange}
value={ad.txt}
/>
<Form.Button type="submit">
{i18n.common.form.save}
</Form.Button>
</Form>
);
};
AdForm.propTypes = {
ad: PropTypes.object.isRequired,
onChange: PropTypes.func.isRequired
};
export default AdForm;
import React, {PropTypes} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as adActions from '../../actions/adActions';
import AdForm from './AdForm';
export class ManageAdPage extends React.Component
{
constructor(props, context) {
super(props, context);
this.state = {
ad: Object.assign({}, props.ad),
errors: {}
};
this.updateAdState = this.updateAdState.bind(this);
}
updateAdState(event) {
const field = event.target.name;
let ad = this.state.ad;
ad[field] = event.target.value;
return this.setState({ad: ad});
}
render() {
return (
<AdForm
ad={this.state.ad}
onChange={this.updateAdState}
/>
);
}
}
ManageAdPage.propTypes = {
ad: PropTypes.object.isRequired,
actions: PropTypes.object.isRequired
};
function mapStateToProps(state, ownProps) {
const slug = ownProps.params.slug;
let ad = {
_id: '', slug: '', txt: ''
};
if (slug && state.ads.length > 0) {
//ad = getAdBySlug(state.ads, slug);
}
return {
ad: ad
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(adActions, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(ManageAdPage);
import React from 'react';
import {mount} from 'enzyme';
import expect from 'expect';
import {ManageAdPage} from '../../../src/components/ad/ManageAdPage';
function setup() {
const props = {
actions: {},
ad: {
_id: '', txt: ''
}
};
return mount(<ManageAdPage {...props}/>);
}
describe('Form Behavior', () => {
let wrapper;
beforeEach(() => {
wrapper = setup();
});
it('shows that text is entered into text textarea', () => {
const txtTextArea = wrapper.find('FormTextArea');
txtTextArea.simulate('change', 'text goes here');
//console.log(txtTextArea.props());
expect(txtTextArea.props().value).toEqual('text goes here');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment