调度器不在jest单元测试中注册回调 [英] Dispatcher not registering callbacks in jest unit tests

查看:142
本文介绍了调度器不在jest单元测试中注册回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在react + flux应用程序中为商店编写单元测试。我按照这里设置模拟调度员的示例,我的单元测试如下所示:

I'm writing unit tests for a store in a react+flux app. I followed the example of setting up the mock dispatcher here, and my unit test looks like this:

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文件中,我向调度程序注册如下:

In my item_store.coffee file, I register with the dispatcher as so:

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注册回调,因为这发生在实际的item_store文件中,我告诉jest不要模拟。但是,由于ShopDispatcher.register是未定义的,它没有注册,但我不知道为什么。任何帮助是赞赏。

I expected the mocked Dispatcher to register the callbacks since that happens in the actual item_store file, which I have told jest to not mock. However, since ShopDispatcher.register is undefined, it's not being registered, but I am not quite sure why. Any help is appreciated.

推荐答案

我也面临同样的问题。而不是使用 ShopDispatcher.register.mock.calls [0] [0] 尝试 ShopDispatcher.dispatch 。 (使用类型脚本)。

I was also facing the same problem. Instead of using the ShopDispatcher.register.mock.calls[0][0] try the ShopDispatcher.dispatch. the below code working perfectly for me (using type script).

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");
});

这篇关于调度器不在jest单元测试中注册回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆