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

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>
主站蜘蛛池模板: 岳池县| 汕尾市| 大兴区| 汝南县| 扬中市| 宽甸| 班戈县| 吉安市| 清原| 清河县| 东阿县| 马尔康县| 沙雅县| 郧西县| 资源县| 和龙市| 永川市| 嘉荫县| 林口县| 芦溪县| 慈利县| 镇康县| 恭城| 清镇市| 宾川县| 阳江市| 本溪市| 太保市| 隆化县| 塘沽区| 柏乡县| 遂宁市| 竹溪县| 安徽省| 新河县| 义马市| 南皮县| 巩留县| 澎湖县| 邻水| 洞头县|