2017-06-20 28 views
0

我試圖改進我的測試覆蓋率,但我在測試組件的渲染方法內聲明的變量和函數時遇到問題。下面是一對夫婦,我是無法例子讓覆蓋:如何測試反應組件渲染函數中的函數和變量?

1)

cityStateZip = `${cityStateZip} - ${location.zipExtension}`; 

2)

directionsUrl = `maps://maps.apple.com/?saddr=My+Location&daddr=${gpsCoords.lat}+${gpsCoords.lng}`; 

3)

const onClick = (pricingTileId) => { 
    if (store.selectedPharmacy !== pricingTileId) { 
    store.setPharmacy(pricingTileId); 
    } 
}; 

下面的代碼:

class Tile extends Component { 
    const { location, store } = this.props; 
    render() { 
    let directionsUrl = `https://maps.google.com/?saddr=My+Location&daddr=${gpsCoords.lat}+${gpsCoords.lng}`; 
    if (navigator.platform.indexOf('iPhone') !== -1 
     || navigator.platform.indexOf('iPod') !== -1 
     || navigator.platform.indexOf('iPad') !== -1) { 
     directionsUrl = `maps://maps.apple.com/?saddr=My+Location&daddr=${gpsCoords.lat}+${gpsCoords.lng}`; 
    } 

    let cityStateZip = `${location.city}, ${location.state} ${location.zip}`; 
    if (location.zipExtension) { 
     cityStateZip = `${cityStateZip} - ${location.zipExtension}`; 
    } 

    const onClick = (pricingTileId) => { 
     if (store.selectedLocation !== pricingTileId) { 
     store.setLocation(pricingTileId); 
     } 
    }; 

    let selectedClass; 
    if (store.selectedLocation === id) { 
     selectedClass = 'selected'; 
    } 

    return (

    ) 

有沒有一種有效的方法來測試我忽略的渲染函數中聲明的變量和函數? (我使用Jest和Enzyme進行測試)。謝謝!

+0

作爲一般規則,這是很難測試邏輯_within_大函數做了很多事情。這就是爲什麼建議編寫幾個較小的方法,渲染功能可以使用;讓'getCityStateZip()'處理zip擴展邏輯,'getDirectionsUrl()'處理設備特定的邏輯 – Hamms

回答

2

你可以重構你的組件是這樣的:

class Tile extends Component { 

    isMobile =() => { 
    let mob = navigator.platform 

    if (mob.indexOf('Iphone')) return true 
    if (mob.indexOf('Ipad')) return true 
    if (mob.indexOf('Ipod')) return true 

    return false 
    } 

    isZipValid =() => !!this.props.location.zipExtension 
    isLocationValid = (id) => this.props.store.location === id 


    handleClick = (pricingTileId) => { 
    const { store } = this.props; 

    if (store.selectedLocation !== pricingTileId) { 
     store.setLocation(pricingTileId); 
    } 
    } 

    render() { 


    let directionsUrl 
    let selectedClass = isLocationValid() && 'selected'; 
    let cityStateZip = `${location.city}, ${location.state} ${location.zip}`; 

    if (!isMobile()) { 
     directionsUrl = `maps://maps.apple.com/?saddr=My+Location&daddr=${gpsCoords.lat}+${gpsCoords.lng}`; 
    } 

    if (isZipValid()) { 
     cityStateZip = `${cityStateZip} - ${location.zipExtension}`; 
    } 
    return (
     <div> Anything</div> 
    ) 
    } 

..

============== PART 2 ========= =========

您可以將它們提取到單獨的文件,如libhelpers ,然後將其導入到組件。

像這樣:

助手:

//helpers.js

export const isMobile = (mob) => { 

    if (mob.indexOf('Iphone')) return true 
    if (mob.indexOf('Ipad')) return true 
    if (mob.indexOf('Ipod')) return true 

    return false 
    } 

最後在組件上:

export { isMobile } from './helpers' 

    if(isMobile(navigator.platform)){ 

    //you just abstracted the function 

    } 

結論:你的isMobile()可以很容易地進行測試從幫手和供應到任何組件:)

現在,你可以通過功能方便地測試功能

我希望它幫你:d

請,

+0

有意義,謝謝!這可能是一個愚蠢的問題,但我對測試這種性質很陌生。我如何在我的測試中訪問這些方法?例如isMobile功能。我正在導入和控制檯記錄我的Tile組件,但似乎無法在任何位置找到這些方法。不太確定如何測試它們。非常感謝您的幫助:) – TigerBalm

+0

在我的帖子上面查看第二部分 –