- Mastering React Test:Driven Development
- Daniel Irvine
- 149字
- 2021-06-24 14:45:08
Accepting text input
The Git tag for this section is accepting-text-input.
Let's render an HTML text input field onto the page. Add the following test to test/CustomerForm.test.js:
it('renders the first name field as a text box', () => {
render(<CustomerForm />);
const field = form('customer').elements.firstName;
expect(field).not.toBeNull();
expect(field.tagName).toEqual('INPUT');
expect(field.type).toEqual('text');
});
This test makes use of the DOM form API: any form allows access to all of its input elements using the elements indexer. This is a simpler way of accessing form fields than CSS selectors, so I prefer to use it when it's an option.
There are three expectations in this test:
- For there to be a form element with the name firstName
- For it to be an input element
- For it to have a type of text
Let's make them all pass. Update CustomerForm to include a single input field, as shown:
export const CustomerForm = () => (
<form id="customer">
<input
type="text"
name="firstName"
/>
</form>
);
推薦閱讀
- iOS Game Programming Cookbook
- jQuery Mobile Web Development Essentials(Third Edition)
- NativeScript for Angular Mobile Development
- 深入淺出RxJS
- Getting Started with NativeScript
- Jenkins Continuous Integration Cookbook(Second Edition)
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- Visualforce Developer’s guide
- Java語言程序設計教程
- 零基礎輕松學C++:青少年趣味編程(全彩版)
- Android智能手機APP界面設計實戰教程
- Visual C++程序設計全程指南
- Android熱門應用開發詳解
- WCF 4.5 Multi-Layer Services Development with Entity Framework(Third Edition)
- Learning Behavior:driven Development with JavaScript