1

我正在尝试使用 jest 测试商店,但它没有注册 AppDispatcher。第一个注释行包括jest.mock允许 console.log 输出一个空的{ calls: [], instances: [] }. 我不想明确地调用它,因为 jest 应该自动模拟所有需求。为什么是AppDispatcher.register.mock未定义的?我先提供了商店代码,然后是测试。谢谢!

商店代码。

import AppDispatcher from '../dispatchers/AppDispatcher';
import AppEvent from '../constants/AppEvent';
import Store from './Store';

let _TrendingSocialStore = new Store();

export default _TrendingSocialStore;

AppDispatcher.register((payload) => {
  let action = payload.action;
  switch(action.type){
    case AppEvent.INIT:
      _TrendingSocialStore.data = {
        trending: action.data.trending,
        location: action.data.location
      };
      _TrendingSocialStore.emitChange();
    break;
  }
});

测试代码。

jest.dontMock('../server/isomorphic/stores/TrendingSocialStore.js');
jest.dontMock('../server/isomorphic/constants/AppEvent.js');
jest.dontMock('../server/isomorphic/dispatchers/AppDispatcher.js');
//jest.mock('../server/isomorphic/dispatchers/AppDispatcher.js');
TrendingSocialStore = require('../server/isomorphic/stores/TrendingSocialStore.js');
var AppDispatcher;
var AppEventConstants;
var AppEventConstants = require('../server/isomorphic/constants/AppEvent.js');
var AppDispatcher = require('../server/isomorphic/dispatchers/AppDispatcher.js');
console.log(AppDispatcher.register.mock)
//var callback = AppDispatcher.register.mock.calls[0][0]

trending = [{
  type: 'sponsored',
  business: {
    name: 'Lorem Ipsum',
    img: 'http://placehold.it/256x256',
    category: 'Coffee'
  },
  sponsor: {
    name: 'YP',
    copy: 'Ad by YP<sup>SM</sup>'
  }
}];

location = {
  id: 1234,
  name: 'University City, CA',
  searchPath: 'university-city',
  desc: 'University City\'s economy is anchored by the University of California, San Diego campus which forms the north part of the community. Southern areas of University City are composed of a mix of single family homes, a retirement community, and a few condominiums. The north-central area, known as the "UTC" area is composed of a dense mix of condominiums and apartment complexes.',
  region: {
    name: 'California',
    abbr: 'CA'
  }
};

// mock actions
var actionInit = {
  actionType: AppEventConstants.INIT,
  data: {
    trending: trending,
    location: location
  }
};


describe('TrendingSocialStore', function() {

  it('registers callback with the dispatcher', function() {
    expect(AppDispatcher.register.mock.calls.length).toBe(1);
  });

  it('grabs trending and location data', function() {
    callback(actionInit);
  });
});
4

0 回答 0