官术网_书友最值得收藏!

Labeling the field

Let's add a label to the field so the user knows what they are typing in:

const labelFor = formElement =>
container.querySelector(`label[for="${formElement}"]`);

it('renders a label for the first name field', () => {
render(<CustomerForm />);
expect(labelFor('firstName')).not.toBeNull();
expect(labelFor('firstName').textContent).toEqual('First name');
});

Make this pass by changing the JSX fragment to read as follows:

<form id="customer">
<label htmlFor="firstName">First name</label>
<input
type="text"
name="firstName"
value={firstName}
readOnly
/>
</form>
The JSX htmlFor attribute sets the HTML for attribute. for couldn't be used in JSX because it is a reserved JavaScript keyword. The attribute is used to signify that the label matches a form element with the given ID, in this case,  firstName.

Now we need to ensure that our input has that same id, so that they match up. Add the following next test:

it('assigns an id that matches the label id to the first name field', () => {
render(<CustomerForm />);
expect(firstNameField().id).toEqual('firstName');
});

Making that pass is as simple as adding in the new attribute:

<form id="customer">
<label htmlFor="firstName">First name</label>
<input
type="text"
name="firstName"
id="firstName"
value={firstName}
readOnly
/>
</form>
主站蜘蛛池模板: 宁国市| 宽城| 轮台县| 富顺县| 明光市| 天全县| 武功县| 莆田市| 札达县| 文安县| 六安市| 河池市| 阜新| 青阳县| 敦化市| 禹城市| 凭祥市| 十堰市| 汝阳县| 灵璧县| 格尔木市| 左贡县| 云浮市| 邳州市| 敖汉旗| 彝良县| 柘城县| 武强县| 民权县| 湟中县| 井冈山市| 铁力市| 舟曲县| 兴山县| 略阳县| 无锡市| 通化县| 深水埗区| 陇南市| 山东省| 呼伦贝尔市|