mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
Added create group functionality
This commit is contained in:
@@ -34,6 +34,7 @@ export const DELETE_GROUP_SUCCESS = `${DELETE_GROUP}_${types.SUCCESS_SUFFIX}`;
|
|||||||
export const DELETE_GROUP_FAILURE = `${DELETE_GROUP}_${types.FAILURE_SUFFIX}`;
|
export const DELETE_GROUP_FAILURE = `${DELETE_GROUP}_${types.FAILURE_SUFFIX}`;
|
||||||
|
|
||||||
const GROUPS_URL = "groups";
|
const GROUPS_URL = "groups";
|
||||||
|
const CONTENT_TYPE_GROUP = "application/vnd.scmm-group+json;v=2";
|
||||||
|
|
||||||
// fetch groups
|
// fetch groups
|
||||||
|
|
||||||
@@ -85,6 +86,40 @@ export function fetchGroupsFailure(url: string, error: Error): Action {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createGroup(group: Group) {
|
||||||
|
return function(dispatch: Dispatch) {
|
||||||
|
dispatch(createGroupPending());
|
||||||
|
return apiClient
|
||||||
|
.postWithContentType(GROUPS_URL, group, CONTENT_TYPE_GROUP)
|
||||||
|
.then(() => dispatch(createGroupSuccess()))
|
||||||
|
.catch(error => {
|
||||||
|
dispatch(
|
||||||
|
createGroupFailure(
|
||||||
|
new Error(`Failed to create group ${group.name}: ${error.message}`)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createGroupPending() {
|
||||||
|
return {
|
||||||
|
type: CREATE_GROUP_PENDING
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createGroupSuccess() {
|
||||||
|
return {
|
||||||
|
type: CREATE_GROUP_SUCCESS
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createGroupFailure(error: Error) {
|
||||||
|
return {
|
||||||
|
type: CREATE_GROUP_FAILURE,
|
||||||
|
payload: error
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
//reducer
|
//reducer
|
||||||
function extractGroupsByNames(
|
function extractGroupsByNames(
|
||||||
@@ -104,7 +139,6 @@ function extractGroupsByNames(
|
|||||||
return groupsByNames;
|
return groupsByNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const reducerByName = (state: any, groupname: string, newGroupState: any) => {
|
const reducerByName = (state: any, groupname: string, newGroupState: any) => {
|
||||||
const newGroupsByNames = {
|
const newGroupsByNames = {
|
||||||
...state,
|
...state,
|
||||||
@@ -156,7 +190,6 @@ export default combineReducers({
|
|||||||
byNames: byNamesReducer
|
byNames: byNamesReducer
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// selectors
|
// selectors
|
||||||
|
|
||||||
const selectList = (state: Object) => {
|
const selectList = (state: Object) => {
|
||||||
|
|||||||
@@ -14,13 +14,16 @@ import reducer, {
|
|||||||
getGroupsFromState,
|
getGroupsFromState,
|
||||||
getFetchGroupsFailure,
|
getFetchGroupsFailure,
|
||||||
isFetchGroupsPending,
|
isFetchGroupsPending,
|
||||||
selectListAsCollection
|
selectListAsCollection,
|
||||||
} from "./groups"
|
createGroup,
|
||||||
|
CREATE_GROUP_SUCCESS,
|
||||||
|
CREATE_GROUP_PENDING,
|
||||||
|
CREATE_GROUP_FAILURE
|
||||||
|
} from "./groups";
|
||||||
const GROUPS_URL = "/scm/api/rest/v2/groups";
|
const GROUPS_URL = "/scm/api/rest/v2/groups";
|
||||||
|
|
||||||
const error = new Error("You have an error!");
|
const error = new Error("You have an error!");
|
||||||
|
|
||||||
|
|
||||||
const groupZaphod = {
|
const groupZaphod = {
|
||||||
creationDate: "2018-07-31T08:39:07.860Z",
|
creationDate: "2018-07-31T08:39:07.860Z",
|
||||||
description: "This is a group",
|
description: "This is a group",
|
||||||
@@ -40,14 +43,16 @@ const groupZaphod = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
_embedded: {
|
_embedded: {
|
||||||
members: [{
|
members: [
|
||||||
|
{
|
||||||
name: "userZaphod",
|
name: "userZaphod",
|
||||||
_links: {
|
_links: {
|
||||||
self: {
|
self: {
|
||||||
href: "http://localhost:3000/scm/api/rest/v2/users/userZaphod"
|
href: "http://localhost:3000/scm/api/rest/v2/users/userZaphod"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}]
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -139,10 +144,37 @@ describe("groups fetch()", () => {
|
|||||||
expect(actions[1].payload).toBeDefined();
|
expect(actions[1].payload).toBeDefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should successfully create group", () => {
|
||||||
|
fetchMock.postOnce(GROUPS_URL, {
|
||||||
|
status: 201
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = mockStore({});
|
||||||
|
return store.dispatch(createGroup(groupZaphod)).then(() => {
|
||||||
|
const actions = store.getActions();
|
||||||
|
expect(actions[0].type).toEqual(CREATE_GROUP_PENDING);
|
||||||
|
expect(actions[1].type).toEqual(CREATE_GROUP_SUCCESS);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should fail creating group on HTTP 500", () => {
|
||||||
|
fetchMock.postOnce(GROUPS_URL, {
|
||||||
|
status: 500
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = mockStore({});
|
||||||
|
return store.dispatch(createGroup(groupZaphod)).then(() => {
|
||||||
|
const actions = store.getActions();
|
||||||
|
expect(actions[0].type).toEqual(CREATE_GROUP_PENDING);
|
||||||
|
expect(actions[1].type).toEqual(CREATE_GROUP_FAILURE);
|
||||||
|
expect(actions[1].payload).toBeDefined();
|
||||||
|
expect(actions[1].payload instanceof Error).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("groups reducer", () => {
|
describe("groups reducer", () => {
|
||||||
|
|
||||||
it("should update state correctly according to FETCH_USERS_SUCCESS action", () => {
|
it("should update state correctly according to FETCH_USERS_SUCCESS action", () => {
|
||||||
const newState = reducer({}, fetchGroupsSuccess(responseBody));
|
const newState = reducer({}, fetchGroupsSuccess(responseBody));
|
||||||
|
|
||||||
@@ -192,9 +224,7 @@ describe("groups reducer", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
describe("selector tests", () => {
|
describe("selector tests", () => {
|
||||||
|
|
||||||
it("should return an empty object", () => {
|
it("should return an empty object", () => {
|
||||||
expect(selectListAsCollection({})).toEqual({});
|
expect(selectListAsCollection({})).toEqual({});
|
||||||
expect(selectListAsCollection({ groups: { a: "a" } })).toEqual({});
|
expect(selectListAsCollection({ groups: { a: "a" } })).toEqual({});
|
||||||
@@ -281,5 +311,4 @@ describe("selector tests", () => {
|
|||||||
it("should return undefined when fetch users did not fail", () => {
|
it("should return undefined when fetch users did not fail", () => {
|
||||||
expect(getFetchGroupsFailure({})).toBe(undefined);
|
expect(getFetchGroupsFailure({})).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user