implemented simplest reducer

This commit is contained in:
Maren Süwer
2018-10-05 11:15:19 +02:00
parent f3a3226fe2
commit db97fc89ea
3 changed files with 49 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ import auth from "./modules/auth";
import pending from "./modules/pending";
import failure from "./modules/failure";
import config from "./config/modules/config";
import indexResources from "./modules/indexResource";
import type { BrowserHistory } from "history/createBrowserHistory";
@@ -23,6 +24,7 @@ function createReduxStore(history: BrowserHistory) {
router: routerReducer,
pending,
failure,
indexResources,
users,
repos,
repositoryTypes,

View File

@@ -53,3 +53,23 @@ export function fetchIndexResourcesFailure(err: Error): Action {
payload: err
};
}
// reducer
export default function reducer(
state: Object = {},
action: Action = { type: "UNKNOWN" }
): Object {
if (!action.payload) {
return state;
}
switch (action.type) {
case FETCH_INDEXRESOURCES_SUCCESS:
return {
...state,
indexResources: action.payload
};
default:
return state;
}
}

View File

@@ -1,11 +1,12 @@
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import fetchMock from "fetch-mock";
import {
import reducer, {
FETCH_INDEXRESOURCES_PENDING,
FETCH_INDEXRESOURCES_SUCCESS,
FETCH_INDEXRESOURCES_FAILURE,
fetchIndexResources
fetchIndexResources,
fetchIndexResourcesSuccess
} from "./indexResource";
const indexResourcesUnauthenticated = {
@@ -119,3 +120,27 @@ describe("fetch index resource", () => {
});
});
});
describe("index resources reducer", () => {
it("should return empty object, if state and action is undefined", () => {
expect(reducer()).toEqual({});
});
it("should return the same state, if the action is undefined", () => {
const state = { x: true };
expect(reducer(state)).toBe(state);
});
it("should return the same state, if the action is unknown to the reducer", () => {
const state = { x: true };
expect(reducer(state, { type: "EL_SPECIALE" })).toBe(state);
});
it("should store the index resources on FETCH_INDEXRESOURCES_SUCCESS", () => {
const newState = reducer(
{},
fetchIndexResourcesSuccess(indexResourcesAuthenticated)
);
expect(newState.indexResources).toBe(indexResourcesAuthenticated);
});
});