Skip to content

Commit b72e0b3

Browse files
author
Jeff Jia
committed
More redux experimentation. Playing with the structure of binding dispatchers
1 parent 40113ba commit b72e0b3

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

app/__tests__/main-test.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@ describe('Wrapped Main View', ()=> {
2121
store.getState.restore();
2222
store.dispatch.restore();
2323
});
24+
2425
it('It translates correct store state to properties', ()=> {
2526
expect(main.prop('text')).to.eql(stateText);
2627
});
2728

28-
it('It provides onClick property which dispatches change text action', ()=> {
29-
main.prop('onClick')();
29+
it('It provides changeText property which dispatches change text action', ()=> {
30+
var changeTextFunc = main.prop('changeText');
31+
32+
changeTextFunc('Greetings');
33+
var dispatch = store.dispatch.lastCall.args[0];
34+
expect(dispatch.type).to.equal(ActionTypes.CHANGE_TEXT);
35+
expect(dispatch.text).to.equal('Greetings');
36+
37+
changeTextFunc('Hello Text');
3038
var dispatch = store.dispatch.lastCall.args[0];
3139
expect(dispatch.type).to.equal(ActionTypes.CHANGE_TEXT);
3240
expect(dispatch.text).to.equal('Hello Text');
@@ -41,7 +49,7 @@ describe('Main View', function () {
4149

4250
before(()=> {
4351
textValue = 'here is the text';
44-
main = mount(<Main text={textValue}/>);
52+
main = mount(<Main text={textValue} changeText={()=>{}}/>);
4553
});
4654

4755
it('Has a view with some text', ()=> {
@@ -59,13 +67,26 @@ describe('Main View', function () {
5967
});
6068

6169
describe('Interaction', () => {
62-
it('Fires on click property on button press', ()=> {
63-
var onClick = sinon.spy();
64-
var main = mount(<Main onClick={onClick}/>);
70+
it('Fires on click property on button press sends Hello React to changeText method', ()=> {
71+
var changeTextSpy = sinon.spy();
72+
var text = '';
73+
var main = mount(<Main text={text} changeText={changeTextSpy}/>);
74+
var button = main.find(TouchableHighlight);
75+
76+
button.prop('onPress')();
77+
expect(changeTextSpy.called).to.equal(true);
78+
expect(changeTextSpy.lastCall.args).to.eql(['Hello React']);
79+
});
80+
81+
it('Fires on click property on button press sends Goodbye React to changeText method if text prop is Hello React', ()=> {
82+
var changeTextSpy = sinon.spy();
83+
var text = 'Hello React';
84+
var main = mount(<Main text={text} changeText={changeTextSpy}/>);
6585
var button = main.find(TouchableHighlight);
6686

6787
button.prop('onPress')();
68-
expect(onClick.called).to.equal(true);
88+
expect(changeTextSpy.called).to.equal(true);
89+
expect(changeTextSpy.lastCall.args).to.eql(['Goodbye React']);
6990
});
7091
});
7192
});

app/main.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,77 @@
33
* https://github.com/facebook/react-native
44
*/
55
'use strict';
6-
import { connect } from 'react-redux'
7-
import {changeText} from './actions/actions'
6+
import {bindActionCreators} from 'redux';
7+
import { connect} from 'react-redux';
8+
import {changeText} from './actions/actions';
89
import React,
910
{
1011
Component,
1112
View,
1213
Text,
1314
TouchableHighlight,
14-
StyleSheet
15-
} from 'react-native'
15+
StyleSheet,
16+
PropTypes
17+
} from 'react-native';
1618

1719
export class Main extends Component {
20+
constructor(props) {
21+
super(props);
22+
this._updateText = this.__updateText.bind(this);
23+
}
24+
25+
__updateText() {
26+
if (this.props.text === 'Hello React') {
27+
this.props.changeText('Goodbye React');
28+
}
29+
else {
30+
this.props.changeText('Hello React');
31+
}
32+
}
33+
1834

1935
render() {
2036
return (
2137
<View style={styles.container}>
2238
<Text>{this.props.text}</Text>
23-
<TouchableHighlight onPress={this.props.onClick}><Text>Push Me</Text></TouchableHighlight>
39+
<TouchableHighlight onPress={this._updateText}><Text>Push Me</Text></TouchableHighlight>
2440
</View>
2541
);
2642
}
2743
}
44+
Main.propTypes = {
45+
text: PropTypes.string.isRequired,
46+
changeText: PropTypes.func.isRequired
47+
};
2848

2949
const styles = StyleSheet.create({
3050
container: {
3151
flex: 1,
3252
justifyContent: 'center',
3353
alignItems: 'center',
34-
backgroundColor: '#F5FCFF',
54+
backgroundColor: '#F5FCFF'
3555
},
3656
welcome: {
3757
fontSize: 20,
3858
textAlign: 'center',
39-
margin: 10,
59+
margin: 10
4060
},
4161
instructions: {
4262
textAlign: 'center',
4363
color: '#333333',
44-
marginBottom: 5,
45-
},
64+
marginBottom: 5
65+
}
4666
});
4767

4868

49-
5069
const mapStateToProps = (state) => {
5170
return {
5271
text: state.text
5372
};
5473
};
5574

5675
const mapDispatchToProps = (dispatch) => {
57-
return {
58-
onClick: () => {
59-
dispatch(changeText('Hello Text'));
60-
}
61-
}
76+
return bindActionCreators({changeText}, dispatch);
6277
};
6378

64-
export default connect(mapStateToProps,mapDispatchToProps)(Main);
79+
export default connect(mapStateToProps, mapDispatchToProps)(Main);

0 commit comments

Comments
 (0)