2017-01-11 51 views
-1

我有下面的反應代碼,我正在使用webpack進行單元測試。如何使用webpack單元測試下面的反應代碼

export function fetchFiltersIfNeeded(filterData) { 
    let sendDate = (new Date()).getTime(); 
    return (dispatch, getState) => { 
     if (shouldFetchFilters(getState(), filterData)) { 
      return dispatch(fetchFilters(filterData, sendDate)); 
     } 
    }; 
} 

如何測試功能fetchFiltersIfNeeded?我如何嘲笑它?

+0

這不是*反應代碼*,這是** **終極版 – CodinCat

回答

0

使用redux-mock-store嘲笑商店

import configureMockStore from 'redux-mock-store' 
import thunk from 'redux-thunk' 

const mockStore = configureMockStore([thunk]) 

describe('Actions',() => { 
    it('fetchFiltersIfNeeded',() => { 
    const store = mockStore({ /* you can put initial state here */ }) 
    store.dispatch(fetchFiltersIfNeeded(yourTestData)) 
    // store.getActions() will return an array of dispatched actions 
    expect(store.getActions()[0]).to.deep.equal({ type: 'FETCH_FILTERS' }) 
    }) 
}) 
相關問題