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

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>
主站蜘蛛池模板: 镇坪县| 察隅县| 鞍山市| 阿勒泰市| 离岛区| 华池县| 津市市| 湖南省| 台州市| 静宁县| 阳东县| 阿鲁科尔沁旗| 揭阳市| 东台市| 石狮市| 宕昌县| 任丘市| 高要市| 乐业县| 米泉市| 内江市| 嵩明县| 石屏县| 绥阳县| 辽源市| 太仆寺旗| 宜兴市| 长泰县| 常山县| 肥乡县| 社旗县| 稷山县| 尼玛县| 西乌珠穆沁旗| 宽城| 连山| 土默特左旗| 巴南区| 江都市| 丹阳市| 章丘市|