Skip to content

Commit

Permalink
Merge ca8dd98 into 0432f36
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMonstro committed Apr 24, 2019
2 parents 0432f36 + ca8dd98 commit 6cd2fc2
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/components/AuthenticationModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import RegisterForm from './RegisterForm';
import Login from './Login';
import ShowModal from '../store/actions/changeFormAction';
import { showErrors } from '../store/actions/registerActions';
import { IS_LOADING, REDIRECT } from '../store/actions/actionTypes';
import { IS_LOADING } from '../store/actions/actionTypes';
import InitiateResetForm from './InitateResetForm';
import PasswordResetForm from './PasswordResetForm';

Expand All @@ -18,7 +18,6 @@ export class ConnectedAuthenticationModal extends React.Component {
dispatch(ShowModal({ modalShow: false }));
dispatch({ type: IS_LOADING, payload: { isLoading: false } });
dispatch(showErrors({}));
dispatch({ type: REDIRECT, payload: { redirect: false } });
};
let CurrentComponent;
let headerMessage;
Expand Down
13 changes: 5 additions & 8 deletions src/components/PasswordResetForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import ButtonSpinner from './ButtonSpinner';
import { passwordReset } from '../store/actions/passwordResetActions';
import validate from '../utils/registerFormValidator';
import store from '../store/store';
import { IS_LOADING, REDIRECT } from '../store/actions/actionTypes';

const results = window.location.href.split('/');
const token = results[results.length - 1];
store.dispatch({ type: REDIRECT, payload: { redirect: true } });
import { IS_LOADING } from '../store/actions/actionTypes';

export class ConnectedPasswordResetForm extends Component {
constructor() {
Expand All @@ -28,7 +24,7 @@ export class ConnectedPasswordResetForm extends Component {

handleSubmit = (event) => {
const { password, confirmPassword } = this.state;
const { PasswordReset } = this.props;
const { PasswordReset, token } = this.props;
event.preventDefault();
const errors = validate('', '', password, confirmPassword);
if (!errors.password && !errors.confirmPassword) {
Expand Down Expand Up @@ -59,7 +55,7 @@ export class ConnectedPasswordResetForm extends Component {
<Form.Text className="error-text">{errors.password}</Form.Text>
<Form.Control
className={errors.password && 'error'}
type="text"
type="password"
name="password"
id="password"
onChange={this.handleChange}
Expand All @@ -70,7 +66,7 @@ export class ConnectedPasswordResetForm extends Component {
<Form.Text className="error-text">{errors.confirmPassword}</Form.Text>
<Form.Control
className={errors.confirmPassword && 'error'}
type="text"
type="password"
name="confirmPassword"
onChange={this.handleChange}
/>
Expand All @@ -90,6 +86,7 @@ export class ConnectedPasswordResetForm extends Component {
ConnectedPasswordResetForm.propTypes = {
PasswordReset: PropTypes.func.isRequired,
isLoading: PropTypes.bool.isRequired,
token: PropTypes.string.isRequired,
};

export const mapStateToProps = state => state.resetPasswordState;
Expand Down
18 changes: 8 additions & 10 deletions src/components/ResetPassword.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';
import store from '../store/store';
import { PASSWORD_RESET } from '../store/actions/actionTypes';
import { PASSWORD_RESET, SET_TOKEN } from '../store/actions/actionTypes';

export class ConnectedResetPassword extends React.Component {
class ResetPassword extends React.Component {
componentDidMount() {
store.dispatch({ type: PASSWORD_RESET });
const { match: { params } } = this.props;
const { resetToken } = params;
store.dispatch({ type: SET_TOKEN, payload: { token: resetToken } });
}

render() {
const { redirect } = this.props;
return (<>{redirect && <Redirect to="/" />}</>);
return (<Redirect to="/" />);
}
}

ConnectedResetPassword.propTypes = {
redirect: PropTypes.bool.isRequired,
ResetPassword.propTypes = {
match: PropTypes.shape({}).isRequired,
};

export const mapStateToProps = state => state.resetPasswordState;

const ResetPassword = connect(mapStateToProps)(ConnectedResetPassword);
export default ResetPassword;
2 changes: 2 additions & 0 deletions src/store/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export const PASSWORD_RESET = 'PASSWORD_RESET';
export const RESET_PASSWORD = 'RESET_PASSWORD';
export const REDIRECT = 'REDIRECT';
export const LOGOUT = 'LOGOUT';
export const SET_TOKEN = 'SET_TOKEN';
export const USER_VERIFIED = 'USER_VERIFIED';
2 changes: 1 addition & 1 deletion src/store/actions/registerActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function registerUser(payload) {
});
dispatch({
type: SHOW_ALERT,
payload: { message: response.data.user.message, showAlert: true },
payload: { message: response.data.user.message, showAlert: true, colorClass: 'alert-success' },
});
dispatch({ type: IS_LOADING, payload: { isLoading: false } });
dispatch(showErrors({}));
Expand Down
6 changes: 3 additions & 3 deletions src/store/reducers/passwordResetReducer.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { IS_LOADING, REDIRECT } from '../actions/actionTypes';
import { IS_LOADING, SET_TOKEN } from '../actions/actionTypes';

const initialState = {
isLoading: false,
redirect: false,
token: '',
};

function passwordResetReducer(state = initialState, action) {
if (action.type === IS_LOADING) {
return Object.assign({}, state, action.payload);
}
if (action.type === REDIRECT) {
if (action.type === SET_TOKEN) {
return Object.assign({}, state, action.payload);
}

Expand Down
25 changes: 6 additions & 19 deletions src/tests/components/ResetPassword.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import { ConnectedResetPassword } from '../../components/ResetPassword';
import { shallow, mount } from 'enzyme';
import ResetPassword from '../../components/ResetPassword';
import { shallow } from 'enzyme';
import React from 'react';
import { mapStateToProps } from '../../components/ResetPassword';

describe('ResetPassword', () => {
it('renders one Password reset component', () => {
const props = {redirect: true}
const wrapper = shallow(<ConnectedResetPassword {...props}/>);
expect(wrapper).toHaveLength(1);
});
it('redirects when redirect is true', () => {
const props = {redirect: true}
const wrapper = shallow(<ConnectedResetPassword {...props}/>);
const props = {
match: { params: { resetToken: 'fedageds' } }
}
const wrapper = shallow(<ResetPassword {...props} />);
expect(wrapper).toHaveLength(1);
});
})

describe('MapToProps', () => {
it('should return new state', () => {
const newState = mapStateToProps({ resetPasswordState: { redirect: true } });
const expected = { redirect: true };
expect(newState).toEqual(expected)
})
});

15 changes: 14 additions & 1 deletion src/tests/components/passwordResetForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Reset password Reset form', () => {
PasswordReset: jest.fn(() => {
Promise.resolve();
}),
...{ isLoading: false },
...{ isLoading: false, token: 'blahblah'},
};

let state = {
Expand Down Expand Up @@ -46,6 +46,19 @@ describe('Reset password Reset form', () => {
expect(errorMsg).toBe("Password should be at least 8 characters long")
});

it('should show errors when passwords are not equal', () => {
const wrapper = mount(<ConnectedPasswordResetForm {...props} />);
const instance = wrapper.instance()
const event = {
preventDefault: jest.fn()
};

instance.setState({password:'fsbdgbhndss', ConfirmPassword: 'vtbgrw'});
instance.handleSubmit(event);
const errorMsg = instance.state.errors.confirmPassword
expect(errorMsg).toBe("Passwords do not match")
});

it("should call handleChange when input value is changed", () => {
const wrapper = mount(<ConnectedPasswordResetForm {...props} />);
const instance = wrapper.instance()
Expand Down

0 comments on commit 6cd2fc2

Please sign in to comment.