- 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.
推薦閱讀
- Instant Apache Stanbol
- Building a Home Security System with Raspberry Pi
- Arduino開發實戰指南:LabVIEW卷
- JavaScript+jQuery開發實戰
- 深入淺出RxJS
- OpenShift在企業中的實踐:PaaS DevOps微服務(第2版)
- ASP.NET程序設計教程
- 深入淺出Serverless:技術原理與應用實踐
- 數據結構習題解析與實驗指導
- Keras深度學習實戰
- 詳解MATLAB圖形繪制技術
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- 區塊鏈國產化實踐指南:基于Fabric 2.0
- Getting Started with Python
- HTML5 WebSocket權威指南