Skip to content

Commit

Permalink
fix(LoginPage): Add submit error callback handling (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron-Lavi authored and jeff-phillips-18 committed Nov 26, 2018
1 parent 2cc0ab9 commit 44aab83
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ const createProps = () => {
},
disableSubmit: false,
submitText: card.form.submitText,
onSubmit: e => storyAction(e, 'Form was submitted')
onSubmit: (e, onError) => {
onError(card.form.error);
storyAction(e, 'Form was submitted');
}
},
social: {
links: createLogoList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DropdownButton } from 'react-bootstrap';
import englishMessages from './mocks/messages.en';
import frenchMessages from './mocks/messages.fr';
import { LoginPage, LoginCardWithValidation } from './index';
import { KEY_CODES } from '../../common/helpers';
import { KEY_CODES, noop } from '../../common/helpers';

const { Input, ForgotPassword, SignUp } = LoginPage.Card;
const { FooterLinks } = LoginPage;
Expand Down Expand Up @@ -257,6 +257,22 @@ test('Submit while password is too short raises the correct error', () => {
).toEqual(englishMessages.card.passwordField.errors.short);
});

test('valid form should be submitted and raise a "server error"', () => {
const serverError = 'server error';
const props = { ...createProps() };
props.card.form.onSubmit = (e, onError) => onError(serverError);
const component = mount(<LoginPage {...props} />);
const passwordElement = component.find('input[type="password"]').at(0);
const usernameElement = component.find('input[type="email"]').at(0);
passwordElement.simulate('change', { target: { value: 'validPassword' } });
usernameElement.simulate('change', { target: { value: 'validUser@gmail.com' } });

const validator = component.find(LoginCardWithValidation).instance();
validator.onSubmit({ preventDefault: noop, target: { submit: noop } });
component.update();
expect(validator.state.form.submitError).toBe(serverError);
});

test('Submit while username is invalid cause a specific error to be shown and onChange is disappears', () => {
const component = mount(<LoginPage {...createProps()} />);
const usernameElement = component.find('input[type="email"]').at(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,36 @@ class LoginCardWithValidation extends React.Component {
onSubmit = e => {
e.preventDefault();
if (this.isFormValid()) {
const { onSubmit, submitError } = this.props;
onSubmit && onSubmit(e);
submitError &&
this.setState({
form: {
...this.state.form,
showError: true,
error: submitError,
disableSubmit: true,
isSubmitting: true
}
});
this.onSubmitStart();
this.props.onSubmit(e, this.onSubmitError);
} else {
this.handleOnInputErrors();
}
};

onSubmitStart = () => {
this.setState({
form: {
...this.state.form,
disableSubmit: true,
isSubmitting: true,
showError: false
}
});
};

onSubmitError = submitError => {
this.setState({
form: {
...this.state.form,
showError: true,
submitError,
disableSubmit: false,
isSubmitting: false
}
});
};

getModifiedProps = () => {
const { usernameField, passwordField } = this.props;
const passwordFieldWarningType = this.state.isCapsLock ? 'capsLock' : this.state.passwordField.warningType;
Expand Down Expand Up @@ -121,7 +134,8 @@ class LoginCardWithValidation extends React.Component {
onSubmit: e => this.onSubmit(e),
showError: this.state.form.showError,
disableSubmit: this.state.form.disableSubmit,
isSubmitting: this.state.form.isSubmitting
isSubmitting: this.state.form.isSubmitting,
submitError: this.state.form.submitError
};
};

Expand Down

0 comments on commit 44aab83

Please sign in to comment.