2016-08-10 5 views
0

次のES6機能を考慮してください。私の意図は、Googleマップから方向データを取得することです。 Google Maps関数は、origin、destination、travelMode、およびcallbackをパラメータとして取ります。コールバックジェネレータの収益はアクティブ化されていません。

// Get direction data from Google API. Individual exports for testing 
 
export function* fetchDirections() { 
 
    const mapApiGoogle = yield select(selectMapApiGoogle()); 
 
    const google = mapApiGoogle.library; 
 
    const origin = yield select(selectOrigin()); 
 
    const destination = yield select(selectDestination()); 
 
    new google.maps.DirectionsService().route({ 
 
    origin: new google.maps.LatLng(origin.lat, origin.lng), 
 
    destination: new google.maps.LatLng(destination.lat, destination.lng), 
 
    travelMode: google.maps.TravelMode.DRIVING, 
 
    }, function* (result, status) { 
 
     if (status === google.maps.DirectionsStatus.OK) { 
 
     yield put(MapDirectionsRequestedGoogleSuccess(result)); 
 
     } else { 
 
     yield put(MapDirectionsRequestedGoogleError(result)); 
 
     } 
 
    } 
 
); 
 
} 
 

問題DirectionService()。ルート()関数が実行され、呼び出される結果とコールバックされたときに、私のfetchDirections()ジェネレータはコールバックでの歩留まりを認識しませんされています。コールバック関数で利回りを利用できるようにするには、上記のコードをどのように修正する必要がありますか?

更新日:通常、私はredux-sagaでジェネレータを使用します。テスト目的のために、私は以下のようにジェネレータを呼び出します。私は、コールバックジェネレータがインスタンス化されないので、内部のyieldに決して到達しないことに気付きました。 fetchDirections()から内部コールバックジェネレータを制御する方法や結果をテストする方法はありますか?

+0

どのように発電機を起動していますか?あなたはそれを使っている場所でコードを表示できますか? – nils

+0

参考までに、私はUPDATEセクションを追加しました。 – DsnKov

答えて

1

describe('fetchDirections',() => { 
 
    let fetchSaga = false; 
 
    const googleStub = { 
 
    maps: { 
 
     LatLng: function (lat, lng) { 
 
     return { lat, lng } 
 
     }, 
 
     TravelMode: { 
 
     DRIVING: 'DRIVING' 
 
     }, 
 
     DirectionsStatus: { 
 
     OK: 'OK' 
 
     } 
 
    } 
 
    }; 
 
    beforeEach(() => { 
 
    fetchSaga = fetchDirections(); 
 
    const selectDescriptor = fetchSaga.next().value; 
 
    expect(selectDescriptor).toEqual(select(selectMapApiGoogle())); 
 
    }); 
 
    it('should invoke directions api',() => { 
 
    googleStub.maps.DirectionsService =() => { 
 
     return { 
 
     route: function(origin, destination, travelMode, callback) { 
 
      callback('directions','OK'); 
 
     } 
 
     } 
 
    }; 
 
    const putDescriptor = fetchSaga.next({ library: googleStub }).value; 
 
    expect(putDescriptor).toEqual(put(MapDirectionsRequestedGoogleSuccess('directions'))); 
 
    }); 
 
});
関数*宣言(アスタリスク続い機能キーワード)はジェネレータオブジェクトを返すジェネレータ関数を定義します。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function *

まず、あなたはジェネレータ関数をインスタンス化し、それから戻っジェネレータオブジェクトを取得する必要があります。だから私はあなたが期待どおりここで仕事のためのコールバックはないと思う、それは初期化の後に停止します。

+0

これは良い点です。この場合、コールバックジェネレータはインスタンス化されません。ジェネレータから外部ライブラリによってトリガされたコールバックを制御する方法を教えてください。 – DsnKov

+0

おそらくコールバック関数をPromiseに変換する必要があります。実装が進行中です。 – DsnKov

+0

この部分は、新しいgoogle.maps.DirectionsService()。ルート({...})を約束します それからあなたはジェネレータとして解決された値を得て、いつものようにそれを扱うことができます。それは '* fetchDirections()'の外で処理されるべきです。 – skymk

関連する問題