mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
added reducer
This commit is contained in:
@@ -83,3 +83,74 @@ export function fetchGroupsFailure(url: string, error: Error): Action {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//reducer
|
||||||
|
function extractGroupsByNames(
|
||||||
|
groups: Groups[],
|
||||||
|
groupNames: string[],
|
||||||
|
oldGroupsByNames: Object
|
||||||
|
) {
|
||||||
|
const groupsByNames = {};
|
||||||
|
|
||||||
|
for (let group of groups) {
|
||||||
|
groupsByNames[group.name] = group;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let groupName in oldGroupsByNames) {
|
||||||
|
groupsByNames[groupName] = oldGroupsByNames[groupName];
|
||||||
|
}
|
||||||
|
return groupsByNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const reducerByName = (state: any, groupname: string, newGroupState: any) => {
|
||||||
|
const newGroupsByNames = {
|
||||||
|
...state,
|
||||||
|
[groupname]: newGroupState
|
||||||
|
};
|
||||||
|
|
||||||
|
return newGroupsByNames;
|
||||||
|
};
|
||||||
|
|
||||||
|
function listReducer(state: any = {}, action: any = {}) {
|
||||||
|
switch (action.type) {
|
||||||
|
case FETCH_GROUPS_SUCCESS:
|
||||||
|
const groups = action.payload._embedded.groups;
|
||||||
|
const groupNames = groups.map(group => group.name);
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
entries: groupNames,
|
||||||
|
entry: {
|
||||||
|
groupCreatePermission: action.payload._links.create ? true : false,
|
||||||
|
page: action.payload.page,
|
||||||
|
pageTotal: action.payload.pageTotal,
|
||||||
|
_links: action.payload._links
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function byNamesReducer(state: any = {}, action: any = {}) {
|
||||||
|
switch (action.type) {
|
||||||
|
// Fetch all groups actions
|
||||||
|
case FETCH_GROUPS_SUCCESS:
|
||||||
|
const groups = action.payload._embedded.groups;
|
||||||
|
const groupNames = groups.map(group => group.name);
|
||||||
|
const byNames = extractGroupsByNames(groups, groupNames, state.byNames);
|
||||||
|
return {
|
||||||
|
...byNames
|
||||||
|
};
|
||||||
|
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default combineReducers({
|
||||||
|
list: listReducer,
|
||||||
|
byNames: byNamesReducer
|
||||||
|
});
|
||||||
|
|||||||
@@ -3,17 +3,66 @@ import configureMockStore from "redux-mock-store";
|
|||||||
import thunk from "redux-thunk";
|
import thunk from "redux-thunk";
|
||||||
import fetchMock from "fetch-mock";
|
import fetchMock from "fetch-mock";
|
||||||
|
|
||||||
import {fetchGroups,
|
import reducer, {
|
||||||
|
fetchGroups,
|
||||||
FETCH_GROUPS_PENDING,
|
FETCH_GROUPS_PENDING,
|
||||||
FETCH_GROUPS_SUCCESS,
|
FETCH_GROUPS_SUCCESS,
|
||||||
FETCH_GROUPS_FAILURE
|
FETCH_GROUPS_FAILURE,
|
||||||
|
fetchGroupsSuccess
|
||||||
} from "./groups"
|
} from "./groups"
|
||||||
const GROUPS_URL = "/scm/api/rest/v2/groups";
|
const GROUPS_URL = "/scm/api/rest/v2/groups";
|
||||||
|
|
||||||
const groupZaphod = {
|
const groupZaphod = {
|
||||||
|
creationDate: "2018-07-31T08:39:07.860Z",
|
||||||
|
description: "This is a group",
|
||||||
|
name: "zaphodGroup",
|
||||||
|
type: "xml",
|
||||||
|
properties: {},
|
||||||
|
members: ["userZaphod"],
|
||||||
|
_links: {
|
||||||
|
self: {
|
||||||
|
href: "http://localhost:3000/scm/api/rest/v2/groups/zaphodGroup"
|
||||||
|
},
|
||||||
|
delete: {
|
||||||
|
href: "http://localhost:3000/scm/api/rest/v2/groups/zaphodGroup"
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
href:"http://localhost:3000/scm/api/rest/v2/groups/zaphodGroup"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_embedded: {
|
||||||
|
members: [{
|
||||||
|
name:"userZaphod",
|
||||||
|
_links: {
|
||||||
|
self :{
|
||||||
|
href: "http://localhost:3000/scm/api/rest/v2/users/userZaphod"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const groupFord = {
|
const groupFord = {
|
||||||
|
creationDate: "2018-07-31T08:39:07.860Z",
|
||||||
|
description: "This is a group",
|
||||||
|
name: "fordGroup",
|
||||||
|
type: "xml",
|
||||||
|
properties: {},
|
||||||
|
members: [],
|
||||||
|
_links: {
|
||||||
|
self: {
|
||||||
|
href: "http://localhost:3000/scm/api/rest/v2/groups/fordGroup"
|
||||||
|
},
|
||||||
|
delete: {
|
||||||
|
href: "http://localhost:3000/scm/api/rest/v2/groups/fordGroup"
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
href:"http://localhost:3000/scm/api/rest/v2/groups/fordGroup"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_embedded: {
|
||||||
|
members: []
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const responseBody = {
|
const responseBody = {
|
||||||
@@ -43,9 +92,6 @@ const response = {
|
|||||||
responseBody
|
responseBody
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const error = new Error("KAPUTT");
|
|
||||||
|
|
||||||
describe("groups fetch()", () => {
|
describe("groups fetch()", () => {
|
||||||
const mockStore = configureMockStore([thunk]);
|
const mockStore = configureMockStore([thunk]);
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -85,3 +131,54 @@ describe("groups fetch()", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("groups reducer", () => {
|
||||||
|
|
||||||
|
it("should update state correctly according to FETCH_USERS_SUCCESS action", () => {
|
||||||
|
const newState = reducer({}, fetchGroupsSuccess(responseBody));
|
||||||
|
|
||||||
|
expect(newState.list).toEqual({
|
||||||
|
entries: ["zaphodGroup", "fordGroup"],
|
||||||
|
entry: {
|
||||||
|
groupCreatePermission: true,
|
||||||
|
page: 0,
|
||||||
|
pageTotal: 1,
|
||||||
|
_links: responseBody._links
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(newState.byNames).toEqual({
|
||||||
|
zaphodGroup: groupZaphod,
|
||||||
|
fordGroup: groupFord
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(newState.list.entry.groupCreatePermission).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should set groupCreatePermission to true if update link is present", () => {
|
||||||
|
const newState = reducer({}, fetchGroupsSuccess(responseBody));
|
||||||
|
|
||||||
|
expect(newState.list.entry.groupCreatePermission).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not replace whole byNames map when fetching users", () => {
|
||||||
|
const oldState = {
|
||||||
|
byNames: {
|
||||||
|
fordGroup: groupFord
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const newState = reducer(oldState, fetchGroupsSuccess(responseBody));
|
||||||
|
expect(newState.byNames["zaphodGroup"]).toBeDefined();
|
||||||
|
expect(newState.byNames["fordGroup"]).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should set userCreatePermission to true if create link is present", () => {
|
||||||
|
const newState = reducer({}, fetchGroupsSuccess(responseBody));
|
||||||
|
|
||||||
|
expect(newState.list.entry.groupCreatePermission).toBeTruthy();
|
||||||
|
expect(newState.list.entries).toEqual(["zaphodGroup", "fordGroup"]);
|
||||||
|
expect(newState.byNames["fordGroup"]).toBeTruthy();
|
||||||
|
expect(newState.byNames["zaphodGroup"]).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user