Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: TT-188 refactor 'removeTo' to 'removeFrom' references
  • Loading branch information
thegreatyamori committed Mar 26, 2021
commit e5356d1147240906549ea7c7222b741bd87dc84d
6 changes: 3 additions & 3 deletions src/app/modules/users/services/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('UsersService', () => {
expect(grantRoleRequest.request.method).toBe('POST');
});

it('add group to a User', () => {
it('add user to group', () => {
const userId = 'userId';
const group = 'admin';
const addGroupURL = `${service.baseUrl}/${userId}/groups/add`;
Expand All @@ -62,12 +62,12 @@ describe('UsersService', () => {
expect(httpMock.expectOne(addGroupURL).request.method).toBe('POST');
});

it('remove group to a User', () => {
it('remove user from group', () => {
const userId = 'userId';
const group = 'admin';
const removeGroupURL = `${service.baseUrl}/${userId}/groups/remove`;

service.removeUserToGroup(userId, group).subscribe();
service.removeUserFromGroup(userId, group).subscribe();

expect(httpMock.expectOne(removeGroupURL).request.method).toBe('POST');
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/users/services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class UsersService {
});
}

removeUserToGroup(userId: string, group: string): Observable<User> {
removeUserFromGroup(userId: string, group: string): Observable<User> {
return this.http.post<User>(`${this.baseUrl}/${userId}/groups/remove`, {
group_name: group,
});
Expand Down
18 changes: 9 additions & 9 deletions src/app/modules/users/store/user.actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,24 @@ describe('UserActions', () => {
expect(action.type).toEqual(actions.UserActionTypes.ADD_USER_TO_GROUP_FAIL);
});

it('RemoveUserToGroup type is UserActionTypes.REMOVE_USER_TO_GROUP', () => {
it('RemoveUserFromGroup type is UserActionTypes.REMOVE_USER_FROM_GROUP', () => {
const userId = 'userId';
const groupName = 'groupName';
const action = new actions.RemoveUserToGroup(userId, groupName);
const action = new actions.RemoveUserFromGroup(userId, groupName);

expect(action.type).toEqual(actions.UserActionTypes.REMOVE_USER_TO_GROUP);
expect(action.type).toEqual(actions.UserActionTypes.REMOVE_USER_FROM_GROUP);
});

it('RemoveUserToGroupSuccess type is UserActionTypes.REMOVE_USER_TO_GROUP_SUCCESS', () => {
it('RemoveUserFromGroupSuccess type is UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS', () => {
const payload: User = { id: 'id', email: 'email', name: 'name' };
const action = new actions.RemoveUserToGroupSuccess(payload);
const action = new actions.RemoveUserFromGroupSuccess(payload);

expect(action.type).toEqual(actions.UserActionTypes.REMOVE_USER_TO_GROUP_SUCCESS);
expect(action.type).toEqual(actions.UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS);
});

it('RemoveUserToGroupFail type is UserActionTypes.REMOVE_USER_TO_GROUP_FAIL', () => {
const action = new actions.RemoveUserToGroupFail('error');
it('RemoveUserFromGroupFail type is UserActionTypes.REMOVE_USER_FROM_GROUP_FAIL', () => {
const action = new actions.RemoveUserFromGroupFail('error');

expect(action.type).toEqual(actions.UserActionTypes.REMOVE_USER_TO_GROUP_FAIL);
expect(action.type).toEqual(actions.UserActionTypes.REMOVE_USER_FROM_GROUP_FAIL);
});
});
24 changes: 12 additions & 12 deletions src/app/modules/users/store/user.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export enum UserActionTypes {
ADD_USER_TO_GROUP = '[User] ADD_USER_TO_GROUP',
ADD_USER_TO_GROUP_SUCCESS = '[User] ADD_USER_TO_GROUP_SUCCESS',
ADD_USER_TO_GROUP_FAIL = '[User] ADD_USER_TO_GROUP_FAIL',
REMOVE_USER_TO_GROUP = '[User] REMOVE_USER_TO_GROUP',
REMOVE_USER_TO_GROUP_SUCCESS = '[User] REMOVE_USER_TO_GROUP_SUCCESS',
REMOVE_USER_TO_GROUP_FAIL = '[User] REMOVE_USER_TO_GROUP_FAIL',
REMOVE_USER_FROM_GROUP = '[User] REMOVE_USER_FROM_GROUP',
REMOVE_USER_FROM_GROUP_SUCCESS = '[User] REMOVE_USER_FROM_GROUP_SUCCESS',
REMOVE_USER_FROM_GROUP_FAIL = '[User] REMOVE_USER_FROM_GROUP_FAIL',
DEFAULT_USER = '[USER] DEFAULT_USER',
}

Expand Down Expand Up @@ -79,18 +79,18 @@ export class AddUserToGroupFail implements Action {
constructor(public error: string) {}
}

export class RemoveUserToGroup implements Action {
public readonly type = UserActionTypes.REMOVE_USER_TO_GROUP;
export class RemoveUserFromGroup implements Action {
public readonly type = UserActionTypes.REMOVE_USER_FROM_GROUP;
constructor(public userId: string, public groupName: string) {}
}

export class RemoveUserToGroupSuccess implements Action {
public readonly type = UserActionTypes.REMOVE_USER_TO_GROUP_SUCCESS;
export class RemoveUserFromGroupSuccess implements Action {
public readonly type = UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS;
constructor(readonly payload: User) {}
}

export class RemoveUserToGroupFail implements Action {
public readonly type = UserActionTypes.REMOVE_USER_TO_GROUP_FAIL;
export class RemoveUserFromGroupFail implements Action {
public readonly type = UserActionTypes.REMOVE_USER_FROM_GROUP_FAIL;
constructor(public error: string) {}
}

Expand All @@ -112,6 +112,6 @@ export type UserActions =
| AddUserToGroup
| AddUserToGroupSuccess
| AddUserToGroupFail
| RemoveUserToGroup
| RemoveUserToGroupSuccess
| RemoveUserToGroupFail;
| RemoveUserFromGroup
| RemoveUserFromGroupSuccess
| RemoveUserFromGroupFail;
22 changes: 11 additions & 11 deletions src/app/modules/users/store/user.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,39 +142,39 @@ describe('UserEffects', () => {
});
});

it('action type is REMOVE_USER_TO_GROUP_SUCCESS when service is executed succesfully', async () => {
it('action type is REMOVE_USER_FROM_GROUP_SUCCESS when service is executed succesfully', async () => {
const userId = 'userId';
const groupName = 'groupName';
actions$ = of({
type: UserActionTypes.REMOVE_USER_TO_GROUP,
type: UserActionTypes.REMOVE_USER_FROM_GROUP,
userId,
groupName,
});

spyOn(toastrService, 'success');
spyOn(service, 'removeUserToGroup').and.returnValue(of(user));
spyOn(service, 'removeUserFromGroup').and.returnValue(of(user));

effects.removeUserToGroup$.subscribe((action) => {
expect(toastrService.success).toHaveBeenCalledWith('Remove user to group success');
expect(action.type).toEqual(UserActionTypes.REMOVE_USER_TO_GROUP_SUCCESS);
effects.removeUserFromGroup$.subscribe((action) => {
expect(toastrService.success).toHaveBeenCalledWith('Remove user from group success');
expect(action.type).toEqual(UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS);
});
});

it('action type is REMOVE_USER_TO_GROUP_FAIL when service is executed succesfully', async () => {
it('action type is REMOVE_USER_FROM_GROUP_FAIL when service is executed succesfully', async () => {
const userId = 'userId';
const groupName = 'groupName';
actions$ = of({
type: UserActionTypes.REMOVE_USER_TO_GROUP,
type: UserActionTypes.REMOVE_USER_FROM_GROUP,
userId,
groupName,
});

spyOn(toastrService, 'error');
spyOn(service, 'removeUserToGroup').and.returnValue(throwError({ error: { message: 'error' } }));
spyOn(service, 'removeUserFromGroup').and.returnValue(throwError({ error: { message: 'error' } }));

effects.removeUserToGroup$.subscribe((action) => {
effects.removeUserFromGroup$.subscribe((action) => {
expect(toastrService.error).toHaveBeenCalled();
expect(action.type).toEqual(UserActionTypes.REMOVE_USER_TO_GROUP_FAIL);
expect(action.type).toEqual(UserActionTypes.REMOVE_USER_FROM_GROUP_FAIL);
});
});
});
14 changes: 7 additions & 7 deletions src/app/modules/users/store/user.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ export class UserEffects {
);

@Effect()
removeUserToGroup$: Observable<Action> = this.actions$.pipe(
ofType(actions.UserActionTypes.REMOVE_USER_TO_GROUP),
map((action: actions.RemoveUserToGroup) => action),
removeUserFromGroup$: Observable<Action> = this.actions$.pipe(
ofType(actions.UserActionTypes.REMOVE_USER_FROM_GROUP),
map((action: actions.RemoveUserFromGroup) => action),
mergeMap((action) =>
this.userService.removeUserToGroup(action.userId, action.groupName).pipe(
this.userService.removeUserFromGroup(action.userId, action.groupName).pipe(
map((response) => {
this.toastrService.success('Remove user to group success');
return new actions.RemoveUserToGroupSuccess(response);
this.toastrService.success('Remove user from group success');
return new actions.RemoveUserFromGroupSuccess(response);
}),
catchError((error) => {
this.toastrService.error(error.error.message);
return of(new actions.RemoveUserToGroupFail(error));
return of(new actions.RemoveUserFromGroupFail(error));
})
)
)
Expand Down
16 changes: 8 additions & 8 deletions src/app/modules/users/store/user.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,35 @@ describe('userReducer', () => {
expect(state.isLoading).toEqual(false);
});

it('on RemoveUserToGroup, isLoading is true', () => {
it('on RemoveUserFromGroup, isLoading is true', () => {
const userId = 'userId';
const groupName = 'groupName';
const action = new actions.RemoveUserToGroup(userId, groupName);
const action = new actions.RemoveUserFromGroup(userId, groupName);
const state = userReducer(initialState, action);

expect(state.isLoading).toEqual(true);
});

it('on RemoveUserToGroupSuccess, user groups should change', () => {
it('on RemoveUserFromGroupSuccess, user groups should change', () => {
const currentState: UserState = {
data: [{ id: 'id', name: 'name', email: 'email', groups: ['group'] }],
isLoading: false,
message: '',
};
const userWithGroupRemoved: User = { id: 'id', name: 'name', email: 'email', groups: null };
const action = new actions.RemoveUserToGroupSuccess(userWithGroupRemoved);
const action = new actions.RemoveUserFromGroupSuccess(userWithGroupRemoved);
const state = userReducer(currentState, action);

expect(state.data).toEqual([userWithGroupRemoved]);
expect(state.isLoading).toEqual(false);
expect(state.message).toEqual('Remove user to group success');
expect(state.message).toEqual('Remove user from group success');
});

it('on RemoveUserToGroupFail, should show a message with an error message', () => {
const action = new actions.RemoveUserToGroupFail('error');
it('on RemoveUserFromGroupFail, should show a message with an error message', () => {
const action = new actions.RemoveUserFromGroupFail('error');
const state = userReducer(initialState, action);

expect(state.message).toEqual('Something went wrong removing user to group');
expect(state.message).toEqual('Something went wrong removing user from group');
expect(state.isLoading).toEqual(false);
});

Expand Down
10 changes: 5 additions & 5 deletions src/app/modules/users/store/user.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,26 @@ export const userReducer = (state: UserState = initialState, action: UserActions
};
}

case UserActionTypes.REMOVE_USER_TO_GROUP: {
case UserActionTypes.REMOVE_USER_FROM_GROUP: {
return {
...state,
isLoading: true,
};
}
case UserActionTypes.REMOVE_USER_TO_GROUP_SUCCESS: {
case UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS: {
const index = userData.findIndex((user) => user.id === action.payload.id);
userData[index] = action.payload;
return {
data: userData,
isLoading: false,
message: 'Remove user to group success',
message: 'Remove user from group success',
};
}
case UserActionTypes.REMOVE_USER_TO_GROUP_FAIL: {
case UserActionTypes.REMOVE_USER_FROM_GROUP_FAIL: {
return {
...state,
isLoading: false,
message: 'Something went wrong removing user to group',
message: 'Something went wrong removing user from group',
};
}
default:
Expand Down