4

react+flux アプリでストアの単体テストを書いています。ここでモック ディスパッチャを設定する例に従いました。単体テストは次のようになります。

jest.dontMock "../../app/scripts/stores/item_store.coffee"
jest.dontMock "object-assign"

describe 'ItemStore', ->
  ShopConstants = require "../../app/scripts/constants/shop_constants.coffee"
  ShopDispatcher = undefined
  ItemStore = undefined
  callback = undefined

  actionBuildQueryString =
    source: "VIEW_ACTION"
    action:
      type: ShopConstants.ActionTypes.BUILD_QUERY_STRING
      size: "4"

  actionReceiveFilterRespData =
    source: "SERVER_ACTION"
    action:
      type: ShopConstants.ActionTypes.RECEIVE_FILTER_RESP_DATA
      data: {item: {} }

  beforeEach ->
    ShopConstants = require "../../app/scripts/constants/shop_constants.coffee"
    ShopDispatcher = require "../../app/scripts/dispatchers/shop_dispatcher.coffee"
    ItemStore = require "../../app/scripts/stores/item_store.coffee"
    callback = ShopDispatcher.register.mock.calls[0][0]

  it "registers a callback with the dispatcher", ->
    expect(ShopDispatcher.register.mock.calls.length).toBe(1)

item_store.coffee ファイルで、次のようにディスパッチャーに登録します。

ShopDispatcher.register (payload) ->
  action = payload.action

  switch action.type

    when ActionTypes.BUILD_QUERY_STRING
      WebApiUtils.fetchItems(payload)

    when ActionTypes.RECEIVE_FILTER_RESP_DATA
      _setItems(action.data)

  ItemStore.emitChange()

モックされた Dispatcher がコールバックを登録することを期待していました。これは、jest にモックしないように指示した実際の item_store ファイルで発生するためです。ただ、ShopDispatcher.registerが未定義なので登録されていないのですが、なぜかよくわかりません。どんな助けでも大歓迎です。

4

1 に答える 1

0

私も同じ問題に直面していました。を使用する代わりに、 を ShopDispatcher.register.mock.calls[0][0]試してくださいShopDispatcher.dispatch。以下のコードは私にとって完璧に機能します(タイプスクリプトを使用)。

beforeEach(function () {   
    dispatcher = require("../../../src/App/Dispatcher");
    localeStore = require("../../../src/stores/localestore");
    localeAction = require("../../../src/actions/Locale/LocaleAction");
}

it("should translate the element with the value in current locale JSON", function () {
   localeChangeAction = new localeAction(true, locale, localeJson);
   dispatcher.dispatch(localeChangeAction);

   var translatedText = localeStore.instance.TranslateText("login.login-header");
        expect(translatedText).toEqual("Login header");
});
于 2016-01-12T15:50:25.973 に答える