2016-12-17 6 views
0

は、以下のReduxの-観察大作有する:
テストReduxの-観察エピックデバウンスオペレータ

export const mouseEventEpic = (action$, store) => 
 
\t action$ 
 
\t \t ::filter(action => action.type === MOUSE_OUT || action.type === MOUSE_OVER) 
 
\t \t ::debounceTime(200) 
 
\t \t ::map(action => getMappedAction(action, store)); 
 

 
const getMappedAction = (action, store) => { 
 
\t switch (action.type) { 
 
     case MOUSE_OVER: 
 
     return {type: "OVER"}; 
 
     case MOUSE_OUT: 
 
     return {type: "OUT"}; 
 
\t } 
 
};

及び以下の試験

import { expect } from 'chai'; 
 
import configureMockStore from 'redux-mock-store'; 
 
import { createEpicMiddleware } from 'redux-observable'; 
 
import { mouseEventEpic } from '...'; 
 
    
 
const epicMiddleware = createEpicMiddleware(mouseEventEpic); 
 
const mockStore = configureMockStore([epicMiddleware]); 
 

 
describe('Epic...',() => { 
 
\t let store; 
 

 
\t beforeEach(() => { 
 
\t \t store = mockStore({}); 
 
\t }); 
 

 
\t it('test...',() => {  
 
\t \t store.dispatch({type:'MOUSE_OVER'});  
 
\t \t expect(store.getActions()).to.deep.equal([]); 
 
\t }); 
 
});

store.getActions()は、1つのアクション( "MOUSE_OVER")を持つ配列を返します。一方、デバウンスを削除すると、別の(そして予想される)アクションが返されます。
私はテストでデバウンス演算子をスタブ/削除したいと思います。 this linkのアイデアに従うことを試みましたが、成功のないsinon stub関数を使用しました。
RxJSオペレータを模擬する方法や具体的にはデバウンス/スロットルに関するガイドラインがあります。
多くの方法がありますが...、モカ、チャイに反応

おかげ

答えて

0

の酵素を使用して、一つはそれをスタブするだろうが、それはあなたの叙事詩が開始された前にスタブアウトする必要がありますさもなければオペレータは既に呼び出されており、それをスタブすることは何もしません(これは試したときに起こったことかもしれません)。

は、私はあなたがミドルウェア/ストアを作成前に、beforeEachにあなたのミドルウェア/ストア作成ロジックを移動し、あまりにもそこにdebounceTimeをスタブアウトする必要があるだろうと信じています。

は限りスタブする方法として、sinonを使用して一つの例は以下のとおりです。

// inside `beforeEach()` 
const debounceTime = sinon.stub(
    Observable.prototype, 'debounceTime', 
    function() { 
    // returning the upstream Observable which 
    // means debounceTime is now effectively a 
    // a pass through (does nothing) 
    return this; 
    } 
); 

// example inside a test 
assert(debounceTime.withArgs(200).calledOnce); 

// later, probably in `afterEach()` 
debounceTime.restore(); 
0

私の2回目の試行。おそらくまだ呼び出されていないスタブが、後でいつか呼び出されるように見えます。再度、:: debounceTime(200)行を削除すると、すべて正常に動作します。

let sandbox, debounceTime, epicMiddleware, mockStore, store; 

beforeEach(() => { 
    sandbox = sinon.sandbox.create(); 
    debounceTime = sandbox.stub(
     Observable.prototype, 'debounceTime', 
     () => {    
      console.log('-----------------inside debounce stub') //never called... 
      return this; 
     } 
    ); 

    epicMiddleware = createEpicMiddleware(combineEpics(stackedOverTimeMouseEventEpic)); 
    mockStore = configureMockStore([epicMiddleware]); 
    store = mockStore({}); 
}); 

afterEach(() => { 
    epicMiddleware.replaceEpic(stackedOverTimeMouseEventEpic); 
    debounceTime.restore(); 
    sandbox.restore(); 
}); 

it('Epic...',() => { 
    const ret = store.dispatch({"type": "MOUSE_OVER"});  
    // expect(debounceTime.withArgs(200).calledOnce).to.be.true; //returns false 
    expect(store.getActions()).to.deep.equal([]);     //returns [{"type": "MOUSE_OVER"},{"type": "@@redux-observable/EPIC_END"}] 
});