- Mastering React Test:Driven Development
- Daniel Irvine
- 95字
- 2021-06-24 14:45:07
Extracting a form-finder method
We'll be writing quite a few tests for this form, so let's extract the call to querySelector into an arrow function. Add this just below the beforeEach definition:
const form = id => container.querySelector(`form[id="${id}"]`);
The expectation in your test then becomes this:
expect(form('customer')).not.toBeNull();
This is much more readable, because the CSS selector is abstracted away behind the function call. I'd encourage you to repeat that pattern in your tests. CSS selectors are a useful tool, but they detract from the "plain English" that you should be aiming for in your tests.
推薦閱讀
- Learning Java Functional Programming
- 精通Nginx(第2版)
- JIRA 7 Administration Cookbook(Second Edition)
- Learning Bayesian Models with R
- Python Data Analysis(Second Edition)
- Apache Mesos Essentials
- C#程序設計
- Teaching with Google Classroom
- Getting Started with Greenplum for Big Data Analytics
- 編程菜鳥學Python數據分析
- 從0到1:HTML5 Canvas動畫開發
- Node.js從入門到精通
- Mastering SciPy
- Android 5從入門到精通
- Spring Microservices