2017-09-22 8 views
0

私は次のReactコンポーネントをreduxストアに接続しています。冗談テストで(匿名)関数を比較するには?

import React, { Component } from 'react' 
import logo from './logo.svg' 
import './App.css' 
import { connect } from 'react-redux' 
import { getWeather } from './actions/WeatherActions' 
import WeatherComponent from './components/weatherComponent/WeatherComponent' 
import { get } from 'lodash' 

export class App extends Component { 

    componentDidMount() { 
    this.props.dispatch(getWeather()) 
    } 

    render() { 
    return (
     <div className="App"> 
      <div className="App-header"> 
      <img src={logo} className="App-logo" alt="logo" /> 
      <h2>Welcome to React</h2> 
      </div> 
      <WeatherComponent 
       weather={{ 
       location: get(this.props.weatherReducer.weather, 'name'), 
       temp: get(this.props.weatherReducer.weather, 'main.temp') 
       }} 
      /> 
     </div> 
    ) 
    } 
} 

export default connect((store) => { 
    return { 
    weatherReducer: store.weatherReducer, 
    } 
})(App) 

このコンポーネントは、componentDidMountコールバックを使用して、getWeatherアクションをディスパッチされます。 getWeatherアクションは、axiosの約束を解決したときに匿名メソッドを返しています。

import { GET_WEATHER_DONE, GET_WEATHER_ERROR } from './ActionTypes' 
import axios from 'axios' 

export function getWeather() { 
    let endpoint = 'http://api.openweathermap.org/data/2.5/weather?q=London&appid=2a345681ddcde393253af927097f5747' 

    return function (dispatch) { 
    return axios.get(endpoint) 
    .then((response) => { 
     return dispatch({ 
     type: GET_WEATHER_DONE, 
     payload: response.data 
     }) 
    }) 
    .catch((error) => { 
     return dispatch({ 
     type: GET_WEATHER_ERROR, 
     payload: error.response.data, 
     statuscode: error.response.status 
     }) 
    }) 
    } 
} 

私はgetWeatherアクションは取り付け時に送出されているかを確認するユニットテストを記述しようとしていますありません。このテストは以下のようになり、パスします。アクションの匿名メソッドを返すので

import React from 'react' 
import ReactDOM from 'react-dom' 
import App from './App' 
import configureMockStore from 'redux-mock-store' 
import thunk from 'redux-thunk' 
import * as actions from './actions/WeatherActions' 

describe('app container',() => { 
    const store = configureMockStore([thunk])({ 
    weatherReducer: { 
     weather: {} 
    } 
    }) 

    const dispatchSpy = jest.fn() 
    store.dispatch = dispatchSpy 

    it('dispatches getWeather() action upon rendering',() => { 
    ReactDOM.render(<App store={store} />, document.createElement('div')) 

    expect(dispatchSpy.mock.calls[0][0].toString()).toEqual(actions.getWeather().toString()) 
    }) 

}) 

は、私が行動を比較するために、私のモック時のtoStringメソッドを呼び出す必要があります。

このテストは、スナップショットテストを使用して再作成しました。

import React from 'react' 
import ReactDOM from 'react-dom' 
import App from './App' 
import configureMockStore from 'redux-mock-store' 
import thunk from 'redux-thunk' 

describe('app container',() => { 
    const store = configureMockStore([thunk])({ 
    weatherReducer: { 
     weather: {} 
    } 
    }) 

    const dispatchSpy = jest.fn() 
    store.dispatch = dispatchSpy 

    it('dispatches correct actions upon rendering',() => { 
    ReactDOM.render(<App store={store} />, document.createElement('div')) 

    let tree = dispatchSpy.mock.calls.toString() 
    expect(tree).toMatchSnapshot(); 
    }) 


}) 

は再び私は、次のスナップショットで、その結果、のtoStringメソッドを呼び出す必要があります。 糸テストを使用して、カバレッジを実行するとき

// Jest Snapshot v1, 

exports[`app container dispatches correct actions upon rendering 1`] = ` 
"function (dispatch) { 
    return _axios2.default.get(endpoint). 
    then(response => { 
     return dispatch({ 
     type: _ActionTypes.GET_WEATHER_DONE, 
     payload: response.data }); 

    }). 
    catch(error => { 
     return dispatch({ 
     type: _ActionTypes.GET_WEATHER_ERROR, 
     payload: error.response.data, 
     statuscode: error.response.status }); 

    }); 
    }" 
`; 

今 - --coverage、私のテストは、自分の行動にテキストを追加するためにイスタンブール失敗しています。出力は次のようになります。

FAIL src/App.snapshot.test.js 
    ● app container › dispatches correct actions upon rendering 

    expect(value).toMatchSnapshot() 

    Received value does not match stored snapshot 1. 

    - Snapshot 
    + Received 

    -"function (dispatch) { 
    - return _axios2.default.get(endpoint). 
    - then(response => { 
    -  return dispatch({ 
    -  type: _ActionTypes.GET_WEATHER_DONE, 
    -  payload: response.data }); 
    +"function (dispatch) {/* istanbul ignore next */cov_2rypo7bhf.f[1]++;cov_2rypo7bhf.s[2]++; 
    + return (/* istanbul ignore next */_axios2.default.get(endpoint). 
    +  then(response => {/* istanbul ignore next */cov_2rypo7bhf.f[2]++;cov_2rypo7bhf.s[3]++; 
    +  return dispatch({ 
    +   type: /* istanbul ignore next */_ActionTypes.GET_WEATHER_DONE, 
    +   payload: response.data }); 

    - }). 
    - catch(error => { 
    -  return dispatch({ 
    -  type: _ActionTypes.GET_WEATHER_ERROR, 
    -  payload: error.response.data, 
    -  statuscode: error.response.status }); 
    +  }). 
    +  catch(error => {/* istanbul ignore next */cov_2rypo7bhf.f[3]++;cov_2rypo7bhf.s[4]++; 
    +  return dispatch({ 
    +   type: /* istanbul ignore next */_ActionTypes.GET_WEATHER_ERROR, 
    +   payload: error.response.data, 
    +   statuscode: error.response.status }); 

    - }); 
    +  })); 
     }" 

     at Object.it (src/App.snapshot.test.js:21:18) 
     at Promise.resolve.then.el (node_modules/p-map/index.js:46:16) 

私が直面しています主な問題は、私は比較のためのtoStringメソッドを呼び出す必要があるという事実です。 jest testingで(匿名の)関数を比較する正しい方法は何ですか?

完全なソースは、だから私は私のテストでgetWeatherアクションをからかって、個々の呼び出しで、戻り値の型を検証することでの発送にできるテストコールだったhttps://github.com/wvanvlaenderen/react-redux-weathercomponent

+0

テストで 'toString'キャストに頼らないでください。テストコードを簡単にフックできるような方法でコードを書いてください。 – Halcyon

+1

私は 'componentDidMount'に' dispatch'を呼び出していますが、 'dispatch'を使って' getWeather'はどうですか?スナップショットテストを行うのは時期尚早です。代わりに 'dispatch'が正しいアクションで呼び出されていることを確認する必要があります –

答えて

0

で見つけることができます。

import React from 'react' 
import ReactDOM from 'react-dom' 
import App from './App' 
import configureMockStore from 'redux-mock-store' 
import thunk from 'redux-thunk' 
import * as actions from './actions/WeatherActions' 
import { spy } from 'sinon' 

describe('app container',() => { 
    const store = configureMockStore([thunk])({ 
    weatherReducer: { 
     weather: {} 
    } 
    }) 

    const dispatchSpy = spy(store, 'dispatch') 

    actions.getWeather = jest.fn().mockImplementation(() => { 
    return {type: 'fetching weather'} 
    }) 

    it('dispatches getWeather() action upon rendering',() => { 
    ReactDOM.render(<App store={store} />, document.createElement('div')) 

    expect(dispatchSpy.firstCall.returnValue.type).toEqual('fetching weather') 
    }) 

}) 

ディスパッチスパイでコールツリーをレンダリングすることで、スナップショットのテストが完了しました。

import React from 'react' 
import ReactDOM from 'react-dom' 
import App from './App' 
import { spy } from 'sinon' 
import configureMockStore from 'redux-mock-store' 
import thunk from 'redux-thunk' 
import * as actions from './actions/WeatherActions' 

describe('app container',() => { 
    const store = configureMockStore([thunk])({ 
    weatherReducer: { 
     weather: {} 
    } 
    }) 

    const dispatchSpy = spy(store, 'dispatch') 

    actions.getWeather = jest.fn().mockImplementation(() => { 
    return {type: 'fetching weather'} 
    }) 

    it('dispatches correct actions upon rendering',() => { 
    ReactDOM.render(<App store={store} />, document.createElement('div')) 

    expect(dispatchSpy.getCalls()).toMatchSnapshot(); 
    }) 

}) 
関連する問題