Skip to content

Instantly share code, notes, and snippets.

@HigoRibeiro
Created March 21, 2018 14:35
Show Gist options
  • Save HigoRibeiro/a7e32c56097fe4c57f20320e457f87f3 to your computer and use it in GitHub Desktop.
Save HigoRibeiro/a7e32c56097fe4c57f20320e457f87f3 to your computer and use it in GitHub Desktop.
/* Core */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
/* Redux */
import { connect } from 'react-redux';
import UserActions from 'store/ducks/user';
/* Presentational */
import { View, TextInput } from 'react-native';
import Alert from 'components/Alert';
import Button from 'components/Button';
import Input from 'components/Input';
import styles from './styles';
class Home extends Component {
static propTypes = {
getInformation: PropTypes.func.isRequired,
};
state = {
cellphone: '',
};
validation = () => {
const { cellphone } = this.state;
if (cellphone) {
const { input } = this.input;
this.props.getInformation(input.getRawValue());
} else {
Alert.alert('Você precisa passar um número válido.');
}
}
render() {
return (
<View style={styles.container}>
<Input
onChangeText={(cellphone) => { this.setState({ cellphone }); }}
value={this.state.cellphone}
type="cel-phone"
icon="phone"
placeholder="Seu número de telefone"
ref={(ref) => { this.input = ref; }}
/>
<Button title="Entrar" onPress={this.validation} />
</View>
);
}
}
const mapDispatchToProps = dispatch => ({
getInformation: cellphone => dispatch(UserActions.userGetInformation(cellphone)),
});
export default connect(null, mapDispatchToProps)(Home);
/* Core */
import React from 'react';
/* Test */
import { shallow } from 'enzyme';
import sinon from 'sinon';
/**
* Presentational
*/
import Button from 'components/Button';
import Home from 'pages/Home';
import Alert from 'components/Alert';
/* redux */
import configureStore from 'redux-mock-store';
import ActionCreators from 'store/ducks/user';
const mockStore = configureStore([]);
const initialStore = {
user: {},
};
describe('Testing Home Page', () => {
const store = mockStore(initialStore);
let wrapper;
function createWrapper() {
return shallow(
<Home />,
{ context: { store } },
);
}
beforeEach(() => {
wrapper = createWrapper().dive();
store.clearActions();
});
it('Can get user information when is valid cell phone', () => {
wrapper.setState({ cellphone: '9999999999' });
wrapper.find(Button).simulate('press');
expect(store.getActions()).toContainEqual(ActionCreators.userGetInformation('9999999999'));
});
it('Can\'t get user information when is invalid cell phone', () => {
sinon.spy(Alert, 'alert');
wrapper.find(Button).simulate('press');
expect(store.getActions()).not.toContainEqual(ActionCreators.userGetInformation(''));
expect(Alert.alert.calledOnce).toBe(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment