Skip to content

Commit 2409eeb

Browse files
thegreatyamoriLEON12699
authored andcommitted
feat: TT-188 add ngrx flow & test
1 parent 5785bc5 commit 2409eeb

File tree

7 files changed

+275
-5
lines changed

7 files changed

+275
-5
lines changed

src/app/modules/users/models/users.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,3 @@ export interface User {
77
tenant_id?: string;
88
deleted?: string;
99
}
10-
11-
export interface UserState extends User {
12-
isLoading: boolean;
13-
error: string;
14-
}

src/app/modules/users/store/user.actions.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('UserActions', () => {
5353
expect(action.type).toEqual(actions.UserActionTypes.REVOKE_USER_ROLE_FAIL);
5454
});
5555

56+
<<<<<<< HEAD
5657
it('AddUserToGroup type is UserActionTypes.ADD_USER_TO_GROUP', () => {
5758
const userId = 'userId';
5859
const groupName = 'groupName';
@@ -93,5 +94,47 @@ describe('UserActions', () => {
9394
const action = new actions.RemoveUserFromGroupFail('error');
9495

9596
expect(action.type).toEqual(actions.UserActionTypes.REMOVE_USER_FROM_GROUP_FAIL);
97+
=======
98+
it('AddGroupToUser type is UserActionTypes.ADD_GROUP_TO_USER', () => {
99+
const userId = 'userId';
100+
const groupName = 'groupName';
101+
const action = new actions.AddGroupToUser(userId, groupName);
102+
103+
expect(action.type).toEqual(actions.UserActionTypes.ADD_GROUP_TO_USER);
104+
});
105+
106+
it('AddGroupToUserSuccess type is UserActionTypes.ADD_GROUP_TO_USER_SUCCESS', () => {
107+
const payload: User = { id: 'id', email: 'email', name: 'name' };
108+
const action = new actions.AddGroupToUserSuccess(payload);
109+
110+
expect(action.type).toEqual(actions.UserActionTypes.ADD_GROUP_TO_USER_SUCCESS);
111+
});
112+
113+
it('AddGroupToUserFail type is UserActionTypes.ADD_GROUP_TO_USER_FAIL', () => {
114+
const action = new actions.AddGroupToUserFail('error');
115+
116+
expect(action.type).toEqual(actions.UserActionTypes.ADD_GROUP_TO_USER_FAIL);
117+
});
118+
119+
it('RemoveGroupToUser type is UserActionTypes.REMOVE_GROUP_TO_USER', () => {
120+
const userId = 'userId';
121+
const groupName = 'groupName';
122+
const action = new actions.RemoveGroupToUser(userId, groupName);
123+
124+
expect(action.type).toEqual(actions.UserActionTypes.REMOVE_GROUP_TO_USER);
125+
});
126+
127+
it('RemoveGroupToUserSuccess type is UserActionTypes.REMOVE_GROUP_TO_USER_SUCCESS', () => {
128+
const payload: User = { id: 'id', email: 'email', name: 'name' };
129+
const action = new actions.RemoveGroupToUserSuccess(payload);
130+
131+
expect(action.type).toEqual(actions.UserActionTypes.REMOVE_GROUP_TO_USER_SUCCESS);
132+
});
133+
134+
it('RemoveGroupToUserFail type is UserActionTypes.REMOVE_GROUP_TO_USER_FAIL', () => {
135+
const action = new actions.RemoveGroupToUserFail('error');
136+
137+
expect(action.type).toEqual(actions.UserActionTypes.REMOVE_GROUP_TO_USER_FAIL);
138+
>>>>>>> feat: TT-188 add ngrx flow & test
96139
});
97140
});

src/app/modules/users/store/user.actions.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ export enum UserActionTypes {
1111
REVOKE_USER_ROLE = '[User] REVOKE_USER_ROLE',
1212
REVOKE_USER_ROLE_SUCCESS = '[User] REVOKE_USER_ROLE_SUCCESS',
1313
REVOKE_USER_ROLE_FAIL = '[User] REVOKE_USER_ROLE_FAIL',
14+
<<<<<<< HEAD
1415
ADD_USER_TO_GROUP = '[User] ADD_USER_TO_GROUP',
1516
ADD_USER_TO_GROUP_SUCCESS = '[User] ADD_USER_TO_GROUP_SUCCESS',
1617
ADD_USER_TO_GROUP_FAIL = '[User] ADD_USER_TO_GROUP_FAIL',
1718
REMOVE_USER_FROM_GROUP = '[User] REMOVE_USER_FROM_GROUP',
1819
REMOVE_USER_FROM_GROUP_SUCCESS = '[User] REMOVE_USER_FROM_GROUP_SUCCESS',
1920
REMOVE_USER_FROM_GROUP_FAIL = '[User] REMOVE_USER_FROM_GROUP_FAIL',
21+
=======
22+
ADD_GROUP_TO_USER = '[User] ADD_GROUP_TO_USER',
23+
ADD_GROUP_TO_USER_SUCCESS = '[User] ADD_GROUP_TO_USER_SUCCESS',
24+
ADD_GROUP_TO_USER_FAIL = '[User] ADD_GROUP_TO_USER_FAIL',
25+
REMOVE_GROUP_TO_USER = '[User] REMOVE_GROUP_TO_USER',
26+
REMOVE_GROUP_TO_USER_SUCCESS = '[User] REMOVE_GROUP_TO_USER_SUCCESS',
27+
REMOVE_GROUP_TO_USER_FAIL = '[User] REMOVE_GROUP_TO_USER_FAIL',
28+
>>>>>>> feat: TT-188 add ngrx flow & test
2029
DEFAULT_USER = '[USER] DEFAULT_USER',
2130
}
2231

@@ -64,6 +73,7 @@ export class RevokeRoleUserFail implements Action {
6473
constructor(public error: string) {}
6574
}
6675

76+
<<<<<<< HEAD
6777
export class AddUserToGroup implements Action {
6878
public readonly type = UserActionTypes.ADD_USER_TO_GROUP;
6979
constructor(public userId: string, public groupName: string) {}
@@ -91,6 +101,35 @@ export class RemoveUserFromGroupSuccess implements Action {
91101

92102
export class RemoveUserFromGroupFail implements Action {
93103
public readonly type = UserActionTypes.REMOVE_USER_FROM_GROUP_FAIL;
104+
=======
105+
export class AddGroupToUser implements Action {
106+
public readonly type = UserActionTypes.ADD_GROUP_TO_USER;
107+
constructor(public userId: string, public groupName: string) {}
108+
}
109+
110+
export class AddGroupToUserSuccess implements Action {
111+
public readonly type = UserActionTypes.ADD_GROUP_TO_USER_SUCCESS;
112+
constructor(readonly payload: User) {}
113+
}
114+
115+
export class AddGroupToUserFail implements Action {
116+
public readonly type = UserActionTypes.ADD_GROUP_TO_USER_FAIL;
117+
constructor(public error: string) {}
118+
}
119+
120+
export class RemoveGroupToUser implements Action {
121+
public readonly type = UserActionTypes.REMOVE_GROUP_TO_USER;
122+
constructor(public userId: string, public groupName: string) {}
123+
}
124+
125+
export class RemoveGroupToUserSuccess implements Action {
126+
public readonly type = UserActionTypes.REMOVE_GROUP_TO_USER_SUCCESS;
127+
constructor(readonly payload: User) {}
128+
}
129+
130+
export class RemoveGroupToUserFail implements Action {
131+
public readonly type = UserActionTypes.REMOVE_GROUP_TO_USER_FAIL;
132+
>>>>>>> feat: TT-188 add ngrx flow & test
94133
constructor(public error: string) {}
95134
}
96135

@@ -109,9 +148,18 @@ export type UserActions =
109148
| RevokeRoleUser
110149
| RevokeRoleUserSuccess
111150
| RevokeRoleUserFail
151+
<<<<<<< HEAD
112152
| AddUserToGroup
113153
| AddUserToGroupSuccess
114154
| AddUserToGroupFail
115155
| RemoveUserFromGroup
116156
| RemoveUserFromGroupSuccess
117157
| RemoveUserFromGroupFail;
158+
=======
159+
| AddGroupToUser
160+
| AddGroupToUserSuccess
161+
| AddGroupToUserFail
162+
| RemoveGroupToUser
163+
| RemoveGroupToUserSuccess
164+
| RemoveGroupToUserFail;
165+
>>>>>>> feat: TT-188 add ngrx flow & test

src/app/modules/users/store/user.effects.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,25 @@ describe('UserEffects', () => {
106106
});
107107
});
108108

109+
<<<<<<< HEAD
109110
it('action type is ADD_USER_TO_GROUP_SUCCESS when service is executed sucessfully', async () => {
110111
const userId = 'userId';
111112
const groupName = 'groupName';
112113
actions$ = of({
113114
type: UserActionTypes.ADD_USER_TO_GROUP,
115+
=======
116+
it('action type is ADD_GROUP_TO_USER_SUCCESS when service is executed sucessfully', async () => {
117+
const userId = 'userId';
118+
const groupName = 'groupName';
119+
actions$ = of({
120+
type: UserActionTypes.ADD_GROUP_TO_USER,
121+
>>>>>>> feat: TT-188 add ngrx flow & test
114122
userId,
115123
groupName,
116124
});
117125

118126
spyOn(toastrService, 'success');
127+
<<<<<<< HEAD
119128
spyOn(service, 'addUserToGroup').and.returnValue(of(user));
120129

121130
effects.addUserToGroup$.subscribe((action) => {
@@ -129,11 +138,27 @@ describe('UserEffects', () => {
129138
const groupName = 'groupName';
130139
actions$ = of({
131140
type: UserActionTypes.ADD_USER_TO_GROUP,
141+
=======
142+
spyOn(service, 'addGroupToUser').and.returnValue(of(user));
143+
144+
effects.addGroupToUser$.subscribe((action) => {
145+
expect(toastrService.success).toHaveBeenCalledWith('Add group to a user success');
146+
expect(action.type).toEqual(UserActionTypes.ADD_GROUP_TO_USER_SUCCESS);
147+
});
148+
});
149+
150+
it('action type is ADD_GROUP_TO_USER_FAIL when service is executed and fail', async () => {
151+
const userId = 'userId';
152+
const groupName = 'groupName';
153+
actions$ = of({
154+
type: UserActionTypes.ADD_GROUP_TO_USER,
155+
>>>>>>> feat: TT-188 add ngrx flow & test
132156
userId,
133157
groupName,
134158
});
135159

136160
spyOn(toastrService, 'error');
161+
<<<<<<< HEAD
137162
spyOn(service, 'addUserToGroup').and.returnValue(throwError({ error: { message: 'error' } }));
138163

139164
effects.addUserToGroup$.subscribe((action) => {
@@ -147,11 +172,27 @@ describe('UserEffects', () => {
147172
const groupName = 'groupName';
148173
actions$ = of({
149174
type: UserActionTypes.REMOVE_USER_FROM_GROUP,
175+
=======
176+
spyOn(service, 'addGroupToUser').and.returnValue(throwError({ error: { message: 'error' } }));
177+
178+
effects.addGroupToUser$.subscribe((action) => {
179+
expect(toastrService.error).toHaveBeenCalled();
180+
expect(action.type).toEqual(UserActionTypes.ADD_GROUP_TO_USER_FAIL);
181+
});
182+
});
183+
184+
it('action type is REMOVE_GROUP_TO_USER_SUCCESS when service is executed succesfully', async () => {
185+
const userId = 'userId';
186+
const groupName = 'groupName';
187+
actions$ = of({
188+
type: UserActionTypes.REMOVE_GROUP_TO_USER,
189+
>>>>>>> feat: TT-188 add ngrx flow & test
150190
userId,
151191
groupName,
152192
});
153193

154194
spyOn(toastrService, 'success');
195+
<<<<<<< HEAD
155196
spyOn(service, 'removeUserFromGroup').and.returnValue(of(user));
156197

157198
effects.removeUserFromGroup$.subscribe((action) => {
@@ -165,16 +206,39 @@ describe('UserEffects', () => {
165206
const groupName = 'groupName';
166207
actions$ = of({
167208
type: UserActionTypes.REMOVE_USER_FROM_GROUP,
209+
=======
210+
spyOn(service, 'removeGroupToUser').and.returnValue(of(user));
211+
212+
effects.removeGroupToUser$.subscribe((action) => {
213+
expect(toastrService.success).toHaveBeenCalledWith('Remove group to a user success');
214+
expect(action.type).toEqual(UserActionTypes.REMOVE_GROUP_TO_USER_SUCCESS);
215+
});
216+
});
217+
218+
it('action type is REMOVE_GROUP_TO_USER_FAIL when service is executed succesfully', async () => {
219+
const userId = 'userId';
220+
const groupName = 'groupName';
221+
actions$ = of({
222+
type: UserActionTypes.REMOVE_GROUP_TO_USER,
223+
>>>>>>> feat: TT-188 add ngrx flow & test
168224
userId,
169225
groupName,
170226
});
171227

172228
spyOn(toastrService, 'error');
229+
<<<<<<< HEAD
173230
spyOn(service, 'removeUserFromGroup').and.returnValue(throwError({ error: { message: 'error' } }));
174231

175232
effects.removeUserFromGroup$.subscribe((action) => {
176233
expect(toastrService.error).toHaveBeenCalled();
177234
expect(action.type).toEqual(UserActionTypes.REMOVE_USER_FROM_GROUP_FAIL);
235+
=======
236+
spyOn(service, 'removeGroupToUser').and.returnValue(throwError({ error: { message: 'error' } }));
237+
238+
effects.removeGroupToUser$.subscribe((action) => {
239+
expect(toastrService.error).toHaveBeenCalled();
240+
expect(action.type).toEqual(UserActionTypes.REMOVE_GROUP_TO_USER_FAIL);
241+
>>>>>>> feat: TT-188 add ngrx flow & test
178242
});
179243
});
180244
});

src/app/modules/users/store/user.effects.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class UserEffects {
6565
);
6666

6767
@Effect()
68+
<<<<<<< HEAD
6869
addUserToGroup$: Observable<Action> = this.actions$.pipe(
6970
ofType(actions.UserActionTypes.ADD_USER_TO_GROUP),
7071
map((action: actions.AddUserToGroup) => action),
@@ -77,12 +78,27 @@ export class UserEffects {
7778
catchError((error) => {
7879
this.toastrService.error(error.error.message);
7980
return of(new actions.AddUserToGroupFail(error));
81+
=======
82+
addGroupToUser$: Observable<Action> = this.actions$.pipe(
83+
ofType(actions.UserActionTypes.ADD_GROUP_TO_USER),
84+
map((action: actions.AddGroupToUser) => action),
85+
mergeMap((action) =>
86+
this.userService.addGroupToUser(action.userId, action.groupName).pipe(
87+
map((response) => {
88+
this.toastrService.success('Add group to a user success');
89+
return new actions.AddGroupToUserSuccess(response);
90+
}),
91+
catchError((error) => {
92+
this.toastrService.error(error.error.message);
93+
return of(new actions.AddGroupToUserFail(error));
94+
>>>>>>> feat: TT-188 add ngrx flow & test
8095
})
8196
)
8297
)
8398
);
8499

85100
@Effect()
101+
<<<<<<< HEAD
86102
removeUserFromGroup$: Observable<Action> = this.actions$.pipe(
87103
ofType(actions.UserActionTypes.REMOVE_USER_FROM_GROUP),
88104
map((action: actions.RemoveUserFromGroup) => action),
@@ -95,6 +111,20 @@ export class UserEffects {
95111
catchError((error) => {
96112
this.toastrService.error(error.error.message);
97113
return of(new actions.RemoveUserFromGroupFail(error));
114+
=======
115+
removeGroupToUser$: Observable<Action> = this.actions$.pipe(
116+
ofType(actions.UserActionTypes.REMOVE_GROUP_TO_USER),
117+
map((action: actions.RemoveGroupToUser) => action),
118+
mergeMap((action) =>
119+
this.userService.removeGroupToUser(action.userId, action.groupName).pipe(
120+
map((response) => {
121+
this.toastrService.success('Remove group to a user success');
122+
return new actions.RemoveGroupToUserSuccess(response);
123+
}),
124+
catchError((error) => {
125+
this.toastrService.error(error.error.message);
126+
return of(new actions.RemoveGroupToUserFail(error));
127+
>>>>>>> feat: TT-188 add ngrx flow & test
98128
})
99129
)
100130
)

0 commit comments

Comments
 (0)