Skip to content

Commit 72aa934

Browse files
committed
fix: TT-97 Add new tests and fix a typo errors
1 parent d3dc13f commit 72aa934

File tree

6 files changed

+55
-16
lines changed

6 files changed

+55
-16
lines changed

src/app/modules/users/components/users-list/users-list.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
<tr class="d-flex">
99
<th class="col">User Email</th>
1010
<th class="col">Names</th>
11-
<th class="col" *ngIf="isFlagOn">Roles</th>
11+
<th class="col" *ngIf="isUserRoleToggleOn">Roles</th>
1212
</tr>
1313
</thead>
1414
<app-loading-bar *ngIf="isLoading$ | async"></app-loading-bar>
1515
<tbody *ngIf="(isLoading$ | async) === false">
1616
<tr class="d-flex" *ngFor="let user of users">
1717
<td class="col">{{ user.email }}</td>
1818
<td class="col">{{ user.name }}</td>
19-
<td class="col text-center" *ngIf="isFlagOn">
19+
<td class="col text-center" *ngIf="isUserRoleToggleOn">
2020
<div>
2121
<ui-switch
2222
size="small"

src/app/modules/users/components/users-list/users-list.component.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing';
44
import { NgxPaginationModule } from 'ngx-pagination';
55
import { UsersListComponent } from './users-list.component';
66
import { UserActionTypes, UserState, LoadUsers, GrantRoleUser, RevokeRoleUser } from '../../store';
7+
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
78
import { ActionsSubject } from '@ngrx/store';
89
import { DataTablesModule } from 'angular-datatables';
9-
import { of } from 'rxjs';
10+
import { Observable, of } from 'rxjs';
1011

1112
describe('UsersListComponent', () => {
1213
let component: UsersListComponent;
1314
let fixture: ComponentFixture<UsersListComponent>;
1415
let store: MockStore<UserState>;
16+
let featureManagerService: FeatureManagerService;
1517
const actionSub: ActionsSubject = new ActionsSubject();
1618

1719
const state: UserState = {
@@ -37,6 +39,7 @@ describe('UsersListComponent', () => {
3739
declarations: [UsersListComponent],
3840
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
3941
}).compileComponents();
42+
featureManagerService = TestBed.inject(FeatureManagerService);
4043
})
4144
);
4245

@@ -78,7 +81,7 @@ describe('UsersListComponent', () => {
7881
component.ngOnInit();
7982

8083
expect(component.isFeatureToggleActivated).toHaveBeenCalled();
81-
expect(component.isFlagOn).toBe(true);
84+
expect(component.isUserRoleToggleOn).toBe(true);
8285
});
8386

8487
const actionsParams = [
@@ -187,6 +190,18 @@ describe('UsersListComponent', () => {
187190
});
188191
});
189192

193+
const toggleValues = [true, false];
194+
toggleValues.map((toggleValue) => {
195+
it(`when FeatureToggle is ${toggleValue} should return true`, () => {
196+
spyOn(featureManagerService, 'isToggleEnabledForUser').and.returnValue(of(toggleValue));
197+
198+
const isFeatureToggleActivated: Observable<boolean> = component.isFeatureToggleActivated();
199+
200+
expect(featureManagerService.isToggleEnabledForUser).toHaveBeenCalled();
201+
isFeatureToggleActivated.subscribe((value) => expect(value).toEqual(toggleValue));
202+
});
203+
});
204+
190205
/*
191206
TODO: block commented on purpose so that when the tests pass and the Feature toggle is removed,
192207
the table will be rendered again with dtInstance and not with dtOptions

src/app/modules/users/components/users-list/users-list.component.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
1717
users: User[] = [];
1818
isLoading$: Observable<boolean>;
1919
loadUsersSubscription: Subscription;
20+
switchRoleSubscription: Subscription;
2021
dtTrigger: Subject<any> = new Subject();
2122
@ViewChild(DataTableDirective, { static: false })
2223
dtElement: DataTableDirective;
2324
dtOptions: any = {};
24-
isFlagOn;
25+
isUserRoleToggleOn;
2526

2627
constructor(
2728
private store: Store<User>,
@@ -33,7 +34,7 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
3334

3435
ngOnInit(): void {
3536
this.isFeatureToggleActivated().subscribe((flag) => {
36-
this.isFlagOn = flag;
37+
this.isUserRoleToggleOn = flag;
3738
});
3839
this.store.dispatch(new LoadUsers());
3940
this.loadUsersSubscription = this.actionsSubject$
@@ -43,7 +44,7 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
4344
this.rerenderDataTable();
4445
});
4546

46-
this.loadUsersSubscription = this.actionsSubject$
47+
this.switchRoleSubscription = this.actionsSubject$
4748
.pipe(
4849
filter(
4950
(action: any) =>
@@ -86,11 +87,7 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
8687
isFeatureToggleActivated() {
8788
return this.featureManagerService.isToggleEnabledForUser('ui-list-test-users').pipe(
8889
map((enabled) => {
89-
if (enabled === true) {
90-
return true;
91-
} else {
92-
return false;
93-
}
90+
return enabled === true ? true : false;
9491
})
9592
);
9693
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,31 @@ describe('UserEffects', () => {
7878
expect(action.type).toEqual(UserActionTypes.GRANT_USER_ROLE_FAIL);
7979
});
8080
});
81+
82+
it('action type is REVOKE_USER_ROLE_SUCCESS when service is executed sucessfully', async () => {
83+
const userId = 'userId';
84+
const roleId = 'roleId';
85+
actions$ = of({ type: UserActionTypes.REVOKE_USER_ROLE, userId, roleId });
86+
const serviceSpy = spyOn(service, 'revokeRole');
87+
spyOn(toastrService, 'success');
88+
serviceSpy.and.returnValue(of(user));
89+
90+
effects.revokeUserRole$.subscribe((action) => {
91+
expect(toastrService.success).toHaveBeenCalledWith('Revoke User Role Success');
92+
expect(action.type).toEqual(UserActionTypes.REVOKE_USER_ROLE_SUCCESS);
93+
});
94+
});
95+
96+
it('action type is REVOKE_USER_ROLE_FAIL when service is executed and fail', async () => {
97+
const userId = 'userId';
98+
const roleId = 'roleId';
99+
actions$ = of({ type: UserActionTypes.REVOKE_USER_ROLE, userId, roleId });
100+
spyOn(service, 'revokeRole').and.returnValue(throwError({ error: { message: 'error' } }));
101+
spyOn(toastrService, 'error');
102+
103+
effects.revokeUserRole$.subscribe((action) => {
104+
expect(toastrService.error).toHaveBeenCalled();
105+
expect(action.type).toEqual(UserActionTypes.REVOKE_USER_ROLE_FAIL);
106+
});
107+
});
81108
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('userReducer', () => {
5757
const action = new actions.GrantRoleUserFail('error');
5858
const state = userReducer(initialState, action);
5959

60-
expect(state.message).toEqual('Something went wrong granted user rol');
60+
expect(state.message).toEqual('Something went wrong granting user role');
6161
expect(state.isLoading).toEqual(false);
6262
});
6363

@@ -89,7 +89,7 @@ describe('userReducer', () => {
8989
const action = new actions.RevokeRoleUserFail('error');
9090
const state = userReducer(initialState, action);
9191

92-
expect(state.message).toEqual('Something went wrong revoking user rol');
92+
expect(state.message).toEqual('Something went wrong revoking user role');
9393
expect(state.isLoading).toEqual(false);
9494
});
9595

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const userReducer = (state: UserState = initialState, action: UserActions
5959
...state,
6060
data: state.data,
6161
isLoading: false,
62-
message: 'Something went wrong granted user rol',
62+
message: 'Something went wrong granting user role',
6363
};
6464
}
6565

@@ -85,7 +85,7 @@ export const userReducer = (state: UserState = initialState, action: UserActions
8585
...state,
8686
data: state.data,
8787
isLoading: false,
88-
message: 'Something went wrong revoking user rol',
88+
message: 'Something went wrong revoking user role',
8989
};
9090
}
9191
default:

0 commit comments

Comments
 (0)