- Mastering React Test:Driven Development
- Daniel Irvine
- 166字
- 2021-06-24 14:45:03
Extracting methods
The call to ReactDOM.render is the same in both methods. Since it's the same in both methods, it makes sense to pull it out.
However, rather than pull it out as-is, we can create a new function that takes the Appointment component as its parameter. This way, we can clearly see how our test data objects are woven through the object under test. If we hid that within an extracted method, the test would be less clear.
The parts of a test that you want to see are the parts that differ between tests. Usually, some data remains the same ( container in this example) and some differs ( customer in this example). Do your best to hide away whatever is the same and proudly display what differs.
Above the first test, write the following definition:
const render = component => ReactDOM.render(component, container);
Now, replace the call to ReactDOM.render in each test with this line:
render(<Appointment customer={customer} />);
Re-run your tests now—they should still be passing.
推薦閱讀
- Android開發精要
- Magento 2 Development Cookbook
- MATLAB 2020從入門到精通
- Python之光:Python編程入門與實戰
- Node.js開發指南
- Qlik Sense? Cookbook
- 大學計算機基礎
- 遠方:兩位持續創業者的點滴思考
- PhoneGap 4 Mobile Application Development Cookbook
- UI設計基礎培訓教程(全彩版)
- 從零開始:C語言快速入門教程
- PostgreSQL Developer's Guide
- Arduino Electronics Blueprints
- Java程序設計及應用開發
- Spring Web Services 2 Cookbook