- Mastering React Test:Driven Development
- Daniel Irvine
- 368字
- 2021-06-24 14:45:10
Solving a batch of tests
We want to now duplicate those six tests for the last-name field. But how do we approach this? Well, we do it test-by-test, just as we did with the first name field. Only this time, we should go much faster as our tests are one-liners and the production code is a copy and paste job.
So, for example, the first test will be this:
describe('last name field', () => {
itRendersAsATextBox('lastName');
});
That can be made to pass by adding the following line to our JSX, just below the first-name input field:
<input
type="text"
name="lastName"
/>
This is just the start for the input field: you'll need to complete it as you add in the next tests.
Go ahead and add the remaining five tests together with their implementation. Then, repeat the process for the phone number field. When adding the submit tests for the phone number, make sure to provide a string value made up of numbers, such as '012345'. Later in this book, we'll add validations to this field that will fail if you don't use the right values now.
If you want to see a listing of all tests in a file, you need to invoke Jest with just a single file. Run the npm test test/CustomerForm.test.js command to see what that looks like. Alternatively, you can run npx jest --verbose to run all tests with full test listings:
PASS test/CustomerForm.test.js
CustomerForm
? renders a form (28ms)
first name field
? renders as a text box (4ms)
? includes the existing value (3ms)
? renders a label (2ms)
? saves existing value when submitted (4ms)
? saves new value when submitted (5ms)
last name field
? renders as a text box (3ms)
? includes the existing value (2ms)
? renders a label (6ms)
? saves existing value when submitted (2ms)
? saves new value when submitted (3ms)
phone number field
? renders as a text box (2ms)
? includes the existing value (2ms)
? renders a label (2ms)
? saves existing value when submitted (3ms)
? saves new value when submitted (2ms)
- 大學計算機基礎(chǔ)(第二版)
- 數(shù)據(jù)庫系統(tǒng)教程(第2版)
- 垃圾回收的算法與實現(xiàn)
- 微服務(wù)與事件驅(qū)動架構(gòu)
- Web Application Development with R Using Shiny(Second Edition)
- YARN Essentials
- 小程序,巧運營:微信小程序運營招式大全
- MySQL數(shù)據(jù)庫基礎(chǔ)實例教程(微課版)
- Learning Hunk
- C語言程序設(shè)計教程
- D3.js By Example
- Visual Basic程序設(shè)計習題與上機實踐
- Django實戰(zhàn):Python Web典型模塊與項目開發(fā)
- Android應(yīng)用開發(fā)實戰(zhàn)(第2版)
- Learning Unreal Engine Game Development