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

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>
主站蜘蛛池模板: 泽库县| 桐梓县| 遂昌县| 田东县| 马鞍山市| 凤城市| 平顶山市| 洛川县| 平顶山市| 高台县| 武城县| 精河县| 莒南县| 章丘市| 增城市| 林芝县| 呼图壁县| 晋城| 木兰县| 滁州市| 太和县| 澎湖县| 嘉义县| 西畴县| 乡城县| 墨脱县| 临颍县| 铜山县| 西城区| 满洲里市| 黑水县| 康定县| 齐河县| 天水市| 阆中市| 云浮市| 萝北县| 将乐县| 英超| 井冈山市| 凤冈县|