Skip to content

Instantly share code, notes, and snippets.

@lele85
Created September 29, 2017 08:59
Show Gist options
  • Save lele85/19149b6d1768c4a0096f51bdbda35e95 to your computer and use it in GitHub Desktop.
Save lele85/19149b6d1768c4a0096f51bdbda35e95 to your computer and use it in GitHub Desktop.
Test reducer with real actions and real store
import {
installJasmineAjax,
uninstallJasmineAjax,
generateApiSuccessResponse
} from "modules/common/lib/TestUtils";
import { episodeAdsFetchReducer } from "modules/episode/reducers/children/episodeAdsFetchReducer";
import {
episodeAdsFetch
} from "modules/episode/actions/episodeAdsFetchActions";
import { createStore, applyMiddleware } from "redux";
import thunkMiddleware from "redux-thunk";
const createMockStore = (reducer, middlewares) => {
const store = createStore(episodeAdsFetchReducer, applyMiddleware(...middlewares));
const history = [];
store.subscribe(() => {
history.push(store.getState());
});
return {
...store,
getHistory() {
return history;
}
}
}
fdescribe("episodeAdsFetchReducer", () => {
let store;
beforeEach(() => {
installJasmineAjax();
store = createMockStore(episodeAdsFetchReducer, [thunkMiddleware]);
});
afterEach(() => {
uninstallJasmineAjax();
});
it("Success", (done) => {
jasmine.Ajax.stubRequest(/\/v2\/users\/125\/revenuead\/episodes\/12/).andReturn(generateApiSuccessResponse({
"adrevenue": {
"episode_id": 12
}
}));
store.dispatch(episodeAdsFetch({userId:125,episodeId:12})).then(() =>{
expect(store.getHistory()).toEqualJSON([
{
"state": "LOADING",
"params": {
"userId": 125,
"episodeId": 12
}
},
{
"state": "SUCCESS",
"params": {
"userId": 125,
"episodeId": 12
},
"model": {
"episode_id": 12
}
}
]);
}).then(done);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment