- 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>
);
推薦閱讀
- Learning Java Functional Programming
- Kibana Essentials
- Mastering AWS Lambda
- 自制編譯器
- Vue.js 3.x從入門到精通(視頻教學(xué)版)
- C語言程序設(shè)計基礎(chǔ)與實驗指導(dǎo)
- Oracle 12c中文版數(shù)據(jù)庫管理、應(yīng)用與開發(fā)實踐教程 (清華電腦學(xué)堂)
- Android 9 Development Cookbook(Third Edition)
- Learning Elixir
- SQL語言從入門到精通
- Visual Basic程序設(shè)計實驗指導(dǎo)(第4版)
- Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
- 劍指大數(shù)據(jù):企業(yè)級數(shù)據(jù)倉庫項目實戰(zhàn)(在線教育版)
- Lift Application Development Cookbook
- Android移動應(yīng)用項目化教程