Skip to content

Commit 9865256

Browse files
committed
feat(intl): error messages
1 parent ebe75dd commit 9865256

11 files changed

Lines changed: 381 additions & 13199 deletions

File tree

src/app/components/RegisterForm.test.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
import {shallow} from "enzyme";
22
import {Promise} from "es6-promise";
33
import * as React from "react";
4-
import {TestHelper} from "../helpers/TestHelper";
54
import {RegisterForm, RegisterReduxForm} from "./RegisterForm";
65
describe("<RegisterForm />", () => {
76
it("without props", () => {
8-
const component = new TestHelper().mount(RegisterReduxForm);
7+
const component = <RegisterReduxForm />;
98
expect(component).toMatchSnapshot();
109
});
1110

1211
it("when submitting button is disabled", () => {
13-
const component = new TestHelper().withProps({submitting: true}).mount(RegisterReduxForm);
12+
const component = <RegisterReduxForm submitting={true} />;
1413
expect(component).toMatchSnapshot();
1514
});
1615

1716
it("when submitting button is enabled", () => {
18-
const component = new TestHelper().withProps({submitting: false}).mount(RegisterReduxForm);
17+
const component = <RegisterReduxForm submitting={false} />;
1918
expect(component).toMatchSnapshot();
2019
});
2120

2221
it("when clear button is disabled", () => {
23-
const component = new TestHelper().withProps({pristine: true}).mount(RegisterReduxForm);
22+
const component = <RegisterReduxForm pristine={true} />;
2423
expect(component).toMatchSnapshot();
2524
});
2625

2726
it("when clear button is enabled", () => {
28-
const component = new TestHelper().withProps({pristine: false, submitting: false}).mount(RegisterReduxForm);
27+
const component = <RegisterReduxForm pristine={false} submitting={false} />;
2928
expect(component).toMatchSnapshot();
3029
});
3130

32-
// Event simulation is limited for Shallow rendering, not sure why CounterPage.test.tsx works
3331
it("calls reset func when clear button is clicked", () => {
3432
const reset = jest.fn();
3533
const onSubmit = () => new Promise((resolve) => resolve());
@@ -40,7 +38,6 @@ describe("<RegisterForm />", () => {
4038
expect(reset).toHaveBeenCalled();
4139
});
4240

43-
// Event simulation is limited for Shallow rendering, not sure why CounterPage.test.tsx works
4441
it("calls handleSubmit func when form is submitted", () => {
4542
const handleSubmit = jest.fn();
4643
const onSubmit = () => new Promise((resolve) => resolve());

src/app/components/RegisterForm.tsx

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as React from "react";
2+
import {FormattedMessage} from "react-intl";
23
import { Field, FormProps } from "redux-form";
34
const { reduxForm } = require("redux-form");
45
import {
5-
aol, email, matchedPwd, maxLength, minLength, minValue,
6-
numberType, renderField, required, tooOld
6+
aol, CustomField, email, matchedPwd, maxLength, minLength,
7+
minValue, numberType, required, tooOld
78
} from "../helpers/FormHelper";
89

910
export interface IFormData {
@@ -18,21 +19,63 @@ interface IProps {
1819
onSubmit: (values: IFormData) => Promise<any>;
1920
}
2021

22+
/*tslint:disable:jsx-no-multiline-js*/
2123
class RegisterForm extends React.Component<FormProps<IFormData, IProps, null> & IProps, null> {
2224
public render(): JSX.Element {
2325
const {handleSubmit, pristine, reset, submitting} = this.props;
2426

2527
return (
2628
<form onSubmit={handleSubmit}>
27-
<Field name="username" type="text" component={renderField} label="Username" validate={[required, maxLength(15)]}/>
28-
<Field name="password" type="password" component={renderField} label="Password" validate={[required, minLength(8)]}/>
29-
<Field name="confirmPassword" type="password" component={renderField} label="Confirm Password" validate={[required, matchedPwd]}/>
30-
<Field name="email" type="email" component={renderField} label="Email" validate={email} warn={aol}/>
31-
<Field name="age" type="number" component={renderField} label="Age" validate={[required, numberType, minValue(18)]} warn={tooOld}/>
29+
<Field
30+
name="username"
31+
type="text"
32+
component={CustomField}
33+
languageId="username"
34+
defaultMessage="Username"
35+
validate={[required("username.required", "Username is required"), maxLength("characters.max", "Must be {max} characters or less")(15)]}
36+
/>
37+
<Field
38+
name="password"
39+
type="password"
40+
component={CustomField}
41+
languageId="password"
42+
defaultMessage="Password"
43+
validate={[required("password.required", "Password is required"), minLength("characters.min", "Must be {min} characters or more")(8)]}
44+
/>
45+
<Field
46+
name="confirmPassword"
47+
type="password"
48+
component={CustomField}
49+
languageId="confirmpassword"
50+
defaultMessage="Confirm Password"
51+
validate={[required("confirmpassword.required", "Please confirm your password"), matchedPwd("password.unmatched", "Passwords are not matched")]}
52+
/>
53+
<Field
54+
name="email"
55+
type="email"
56+
component={CustomField}
57+
languageId="email"
58+
defaultMessage="Email"
59+
validate={email("email.invalid", "Invalid email format")}
60+
warn={aol("email.aol", "Really? You still use AOL for your email?")}
61+
/>
62+
<Field
63+
name="age"
64+
type="number"
65+
component={CustomField}
66+
languageId="age"
67+
defaultMessage="Age"
68+
validate={[
69+
required("age.required", "Age is required"),
70+
numberType("field.numbertype", "Must be a number"),
71+
minValue("register.minage", "You must be at least {min} years old!")(18)
72+
]}
73+
warn={tooOld("register.tooold", "You are too old for this!")}
74+
/>
3275
<div>
33-
<button type="submit" disabled={submitting}>Submit</button>
76+
<button type="submit" disabled={submitting}><FormattedMessage id="submit" defaultMessage="Submit" /></button>
3477
<button type="button" disabled={pristine || submitting} onClick={reset} style={{marginLeft: 10}}>
35-
Clear Values
78+
<FormattedMessage id="form.clearvalues" defaultMessage="Clear Values" />
3679
</button>
3780
</div>
3881
</form>

0 commit comments

Comments
 (0)