diff --git a/scm-ui/src/createReduxStore.js b/scm-ui/src/createReduxStore.js index 8411326b53..86e8eb1a50 100644 --- a/scm-ui/src/createReduxStore.js +++ b/scm-ui/src/createReduxStore.js @@ -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, diff --git a/scm-ui/src/modules/indexResource.js b/scm-ui/src/modules/indexResource.js index 0e4b4e3cd0..c9b47dd676 100644 --- a/scm-ui/src/modules/indexResource.js +++ b/scm-ui/src/modules/indexResource.js @@ -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; + } +} diff --git a/scm-ui/src/modules/indexResource.test.js b/scm-ui/src/modules/indexResource.test.js index 8cc0137481..8794b66825 100644 --- a/scm-ui/src/modules/indexResource.test.js +++ b/scm-ui/src/modules/indexResource.test.js @@ -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); + }); +});