2

I am attempting to test a store using jest and it's not registering the AppDispatcher. The first commented line including jest.mock allowed the console.log to output an empty hash of { calls: [], instances: [] }. I don't want to explicitly call this because jest is supposed to auto-mock all of the requires. Why is AppDispatcher.register.mock undefined? I have provided the store code first and then the test. Thanks!

The store code.

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

The test code.

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

0 Answers0