- 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.
推薦閱讀
- JavaScript百煉成仙
- INSTANT FreeMarker Starter
- Learning SAP Analytics Cloud
- C#程序設計教程
- Linux網絡程序設計:基于龍芯平臺
- 新編Premiere Pro CC從入門到精通
- Java Web基礎與實例教程
- Python編程與幾何圖形
- Julia高性能科學計算(第2版)
- C#程序設計教程(第3版)
- Quantum Computing and Blockchain in Business
- Learning Docker Networking
- Learning Android Application Testing
- C語言程序設計
- Python物理建模初學者指南(第2版)