I'm trying to test flux stores. I use ReduceStore from flux/utils, jest v0.7.0. When I dispatch an action with the mocked dispatcher I get an error:
Error: Invariant Violation: Store.__emitChange(): Must be invoked while dispatching.
If I use this store in a real browser, it works as expected.
Here is my store:
import {ReduceStore} from 'flux/utils';
import Immutable from 'immutable';
import Dispatcher from '../dispatcher';
import actionConstants from '../action-constants';
class EntryStore extends ReduceStore {
getInitialState() {
return Immutable.List();
}
reduce(state, action) {
switch (action.type) {
case actionConstants.ENTRIES_REQUEST_SUCCESS:
return state.merge(action.payload.entries);
default:
return state;
}
}
}
const instance = new EntryStore(Dispatcher);
export default instance;
And, here is a test file:
jest.autoMockOff();
jest.mock('../../dispatcher');
const actionConstants = require('../../action-constants');
describe('EntryStore', function() {
let Dispatcher, EntryStore, callback;
// mock entries
entries = [1, 2, 3];
// mock actions
const Immutable = require('immutable');
const entriesRequestSuccess = {
type: actionConstants.ENTRIES_REQUEST_SUCCESS,
payload: {
entries: Immutable.List(entries)
}
}
beforeEach(function() {
Dispatcher = require('../../dispatcher');
EntryStore = require('../entry-store');
callback = Dispatcher.register.mock.calls[0][0];
});
it('should update entries when entries request succeed', function(done) {
callback(entriesRequestSuccess);
let all = EntryStore.getState();
expect(all.size).toBe(3);
});
});
Aucun commentaire:
Enregistrer un commentaire