Dispatcher n'inscrit pas les rappels dans les tests de jest unit

J'écris des tests unitaires pour un magasin dans une application de réaction + flux. J'ai suivi l'exemple de la mise en place de la réparatrice simulée ici , et mon test d'unité ressemble à ceci:

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) 

Dans mon fichier item_store.coffee, je m'inscris avec le répartiteur:

 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() 

Je m'attendais à ce que le Dispatcher moqué enregistre les rappels car cela se produit dans le fichier item_store réel, ce qui m'a dit de ne pas se moquer. Cependant, puisque ShopDispatcher.register est indéfini, il n'est pas enregistré, mais je ne sais pas trop pourquoi. Toute aide est appréciée.

J'étais également confronté au même problème. Au lieu d'utiliser ShopDispatcher.register.mock.calls[0][0] essayez le ShopDispatcher.dispatch . Le code ci-dessous fonctionne parfaitement pour moi (en utilisant le script de type).

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