- Mastering React Test:Driven Development
- Daniel Irvine
- 154字
- 2021-06-24 14:45:08
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>
推薦閱讀
- PHP動(dòng)態(tài)網(wǎng)站程序設(shè)計(jì)
- Java入門經(jīng)典(第6版)
- Mastering Selenium WebDriver
- Vue.js 2 and Bootstrap 4 Web Development
- Java從入門到精通(第4版)
- Android 9 Development Cookbook(Third Edition)
- JavaScript+jQuery開(kāi)發(fā)實(shí)戰(zhàn)
- Processing互動(dòng)編程藝術(shù)
- Easy Web Development with WaveMaker
- HTML5+CSS3+JavaScript Web開(kāi)發(fā)案例教程(在線實(shí)訓(xùn)版)
- 深入分布式緩存:從原理到實(shí)踐
- Building Microservices with .NET Core
- Practical Microservices
- 從零開(kāi)始構(gòu)建深度前饋神經(jīng)網(wǎng)絡(luò):Python+TensorFlow 2.x
- Qt 5.12實(shí)戰(zhàn)