Skip to content

Instantly share code, notes, and snippets.

@MrRoyce
Last active February 27, 2017 18:25
Show Gist options
  • Save MrRoyce/167af6e1b02745402efc8f91d65f7a48 to your computer and use it in GitHub Desktop.
Save MrRoyce/167af6e1b02745402efc8f91d65f7a48 to your computer and use it in GitHub Desktop.
import { createLogic } from 'redux-logic';
import * as ActionTypes from '../Types';
import {
getCategoriesSuccess,
getCategoriesFailure,
getCatSuccess,
getCatFailure,
voteCatSuccess,
voteCatFailure,
getFavoritesSuccess,
getFavoritesFailure,
favoriteCatSuccess,
favoriteCatFailure
} from './CatAPI.actions';
export const getFavoritesLogic = createLogic({
type : ActionTypes.GET_FAVORITES,
latest : true, // take latest only
processOptions : {
dispatchReturn : true,
successType : getFavoritesSuccess,
failType : getFavoritesFailure
},
// use CAT_API injected as httpClient
// from configureStore logic deps
process({ httpClient, CAT_API }) {
return httpClient({
url : `${CAT_API.url}/favorites`,
crossDomain: true })
.map(payload => payload) // use entire response with data and headers
.catch((err) => {
console.log('Error on getting favorites: ' + err);
});
}
});
export const favoriteCatLogic = createLogic({
type : ActionTypes.FAVORITE_CAT,
latest : true, // take latest only
processOptions : {
dispatchReturn : true,
successType : favoriteCatSuccess,
failType : favoriteCatFailure
},
// use CAT_API httpClient
// from configureStore logic deps
process({ httpClient, CAT_API, action }) {
return httpClient({
url : `${CAT_API.url}/setfav?image_id=${action.payload.image_id}`,
crossDomain: true })
.map(payload => payload) // use entire response with data and headers
.catch((err) => {
console.log('Error on favoriting cat: ' + err);
});
}
});
export const voteCatLogic = createLogic({
type : ActionTypes.VOTE_CAT,
latest : true, // take latest only
processOptions : {
dispatchReturn : true,
successType : voteCatSuccess,
failType : voteCatFailure
},
// use CAT_API injected as httpClient
// from configureStore logic deps
process({ httpClient, CAT_API, action }) {
return httpClient({
url : `${CAT_API.url}/vote?image_id=${action.payload.image_id}&score=${action.payload.score}`,
crossDomain: true })
.map(payload => payload) // use entire response with data and headers
.catch((err) => {
console.log('Error on voting for cat: ' + err);
});
}
});
export const getCatLogic = createLogic({
type : ActionTypes.GET_CAT,
latest : true, // take latest only
processOptions : {
dispatchReturn : true,
successType : getCatSuccess,
failType : getCatFailure
},
// use CAT_API injected as httpClient
// from configureStore logic deps
process({ httpClient, CAT_API, action }) {
const
// add the category if it was passed in
category = (action.payload.category) ? `?category=${action.payload.category}` : ''
;
return httpClient({ url : `${CAT_API.url}/cat${category}`, crossDomain: true})
.map(payload => payload) // use entire response with data and headers
.catch((err) => {
console.log('Error on getting cat: ' + err);
});
}
});
export const getCategoriesLogic = createLogic({
type : ActionTypes.LIST_CATEGORIES,
latest : true, // take latest only
processOptions : {
dispatchReturn : true,
successType : getCategoriesSuccess,
failType : getCategoriesFailure
},
// use CAT_API injected as httpClient
// from configureStore logic deps
process({ httpClient, CAT_API }) {
return httpClient({ url : `${CAT_API.url}/categories`, crossDomain: true})
.map(payload => payload) // use entire response with data and headers
.catch((err) => {
console.log('Error on getting categories: ' + err);
});
}
});
export default [
getCategoriesLogic,
getCatLogic,
voteCatLogic,
getFavoritesLogic,
favoriteCatLogic
];
import { expect } from 'chai';
import { createMockStore } from 'redux-logic-test';
import { Observable } from 'rxjs';
import * as ActionTypes from '../Types';
import { getCategoriesLogic } from './CatAPI.logic';
import reducer from '../reducers/CatAPI';
import { config } from '../Config';
const INITIAL_STATE = {
categories : [],
categories_loading : false,
favorites : [],
favorites_loading : false,
cat : null,
getting_favorites : false,
cat_loading : false,
cat_voting : false,
cat_favoriting : false,
gets : 0,
votes : 0,
favs : 0
};
const
categories = [
{ name : 'test1'}
];
const httpClient = () => {
return Observable.of({ // match shape of api results
data: categories
});
};
const injectedDeps = { // injected dependencies for logic
httpClient,
CAT_API : {
url : config.thecatapi.url,
key : config.thecatapi.key
}
};
const store = createMockStore({
initialState : INITIAL_STATE,
reducer,
logic : [getCategoriesLogic],
injectedDeps
});
it('gets the correct categories', () => {
store.dispatch({ type: ActionTypes.LIST_CATEGORIES }); // start fetching
return store.whenComplete(() => { // all logic has completed
expect(store.actions).toEqual([
{ type: ActionTypes.LIST_CATEGORIES },
{ type: ActionTypes.LIST_CATEGORIES_SUCCESS, payload: categories }
]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment