- 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項目開發(fā)入門教程
- Magento 2 Development Cookbook
- Linux網(wǎng)絡(luò)程序設(shè)計:基于龍芯平臺
- 精通Python設(shè)計模式(第2版)
- Java EE 8 Application Development
- Python算法指南:程序員經(jīng)典算法分析與實現(xiàn)
- Java編程的邏輯
- Python深度學(xué)習(xí):模型、方法與實現(xiàn)
- Extreme C
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- 大學(xué)計算機應(yīng)用基礎(chǔ)(Windows 7+Office 2010)(IC3)
- Python數(shù)據(jù)預(yù)處理技術(shù)與實踐
- 可視化H5頁面設(shè)計與制作:Mugeda標(biāo)準(zhǔn)教程
- Java面試一戰(zhàn)到底(基礎(chǔ)卷)
- 數(shù)據(jù)結(jié)構(gòu)與算法詳解