- 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.
推薦閱讀
- Vue 3移動Web開發與性能調優實戰
- HoloLens Beginner's Guide
- JIRA 7 Administration Cookbook(Second Edition)
- ASP.NET Core 2 and Vue.js
- Python Data Analysis(Second Edition)
- Hands-On Natural Language Processing with Python
- Microsoft Azure Storage Essentials
- Machine Learning in Java
- 一本書講透Java線程:原理與實踐
- .NET 4.5 Parallel Extensions Cookbook
- Visual Studio Code 權威指南
- Java Web從入門到精通(第3版)
- 分布式數據庫原理、架構與實踐
- 零基礎學C語言(第4版)
- Get Your Hands Dirty on Clean Architecture