调度程序未在开玩笑单元测试中注册回调
Dispatcher not registering callbacks in jest unit tests
我正在为 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()
我希望被模拟的调度程序注册回调,因为这发生在实际的item_store文件中,我已经告诉开玩笑不要嘲笑。 但是,由于ShopDispatcher.register未定义,因此没有注册,但我不太确定为什么。 任何帮助,不胜感激。
我也面临着同样的问题。与其使用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");
});
相关文章:
- AngularJS:我可以跳过函数参数回调吗
- 要求未定义JS回调参数
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 自引用回调
- 注册后是否可以定义HTML5自定义元素中的生命周期回调
- 如何在 JavaScript 中为嵌套脚本标记追加注册回调
- 在Javascript中为所选方法自动注册触发器回调
- 调度程序未在开玩笑单元测试中注册回调
- 检查和取消注册 document.ready() 回调
- jQuery SignalR client.off()函数只删除上次注册的回调
- JavaScript登录/注册回调函数
- 如何在dart中编写on-*事件/回调注册
- Q promise:都是按注册顺序调用的回调函数
- 如何在Firebase匿名登录中注册退出事件的回调
- ajax回调完成后重新注册javascript文件
- 如何对一个在依赖项上注册了回调的Angular服务进行单元测试?
- 如何检测或注册自定义元素加载并准备与之交互的回调
- 护照注册成功回调
- 如何注册一个回调并通过网站刷新持久化它
- 注册原型函数作为回调