Skip to content

Commit d3dc13f

Browse files
committed
fix: TT-97 add some test to feature
1 parent 2a0472e commit d3dc13f

File tree

2 files changed

+90
-16
lines changed

2 files changed

+90
-16
lines changed

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

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing';
33

44
import { NgxPaginationModule } from 'ngx-pagination';
55
import { UsersListComponent } from './users-list.component';
6-
import { UserActionTypes, UserState, LoadUsers } from '../../store';
6+
import { UserActionTypes, UserState, LoadUsers, GrantRoleUser, RevokeRoleUser } from '../../store';
77
import { ActionsSubject } from '@ngrx/store';
88
import { DataTablesModule } from 'angular-datatables';
9+
import { of } from 'rxjs';
910

1011
describe('UsersListComponent', () => {
1112
let component: UsersListComponent;
@@ -29,13 +30,15 @@ describe('UsersListComponent', () => {
2930
message: '',
3031
};
3132

32-
beforeEach(waitForAsync(() => {
33-
TestBed.configureTestingModule({
34-
imports: [NgxPaginationModule, DataTablesModule],
35-
declarations: [UsersListComponent],
36-
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
37-
}).compileComponents();
38-
}));
33+
beforeEach(
34+
waitForAsync(() => {
35+
TestBed.configureTestingModule({
36+
imports: [NgxPaginationModule, DataTablesModule],
37+
declarations: [UsersListComponent],
38+
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
39+
}).compileComponents();
40+
})
41+
);
3942

4043
beforeEach(() => {
4144
fixture = TestBed.createComponent(UsersListComponent);
@@ -68,9 +71,76 @@ describe('UsersListComponent', () => {
6871

6972
expect(component.users).toEqual(state.data);
7073
});
71-
/*
72-
TODO: block commented on purpose so that when the tests pass and the Feature toggle is removed,
73-
the table will be rendered again with dtInstance and not with dtOptions
74+
75+
it('When Component is created, should call the feature toggle method', () => {
76+
spyOn(component, 'isFeatureToggleActivated').and.returnValue(of(true));
77+
78+
component.ngOnInit();
79+
80+
expect(component.isFeatureToggleActivated).toHaveBeenCalled();
81+
expect(component.isFlagOn).toBe(true);
82+
});
83+
84+
const actionsParams = [
85+
{ actionType: UserActionTypes.GRANT_USER_ROLE_SUCCESS },
86+
{ actionType: UserActionTypes.REVOKE_USER_ROLE_SUCCESS },
87+
];
88+
89+
actionsParams.map((param) => {
90+
it(`When action ${param.actionType} is dispatched should triggered load Users action`, () => {
91+
spyOn(store, 'dispatch');
92+
93+
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
94+
const action = {
95+
type: param.actionType,
96+
payload: state.data,
97+
};
98+
99+
actionSubject.next(action);
100+
101+
expect(store.dispatch).toHaveBeenCalledWith(new LoadUsers());
102+
});
103+
});
104+
105+
const grantRoleTypes = [
106+
{ roleId: 'admin', roleValue: 'time-tracker-admin' },
107+
{ roleId: 'test', roleValue: 'time-tracker-tester' },
108+
];
109+
110+
grantRoleTypes.map((param) => {
111+
it(`When user switchRole to ${param.roleId} and don't have any role, should grant ${param.roleValue} Role`, () => {
112+
const roleId = param.roleId;
113+
const roleValue = param.roleValue;
114+
const userRoles = [];
115+
const userId = 'userId';
116+
117+
spyOn(store, 'dispatch');
118+
119+
component.switchRole(userId, userRoles, roleId, roleValue);
120+
121+
expect(store.dispatch).toHaveBeenCalledWith(new GrantRoleUser(userId, roleId));
122+
});
123+
});
124+
125+
const revokeRoleTypes = [
126+
{ roleId: 'admin', roleValue: 'time-tracker-admin', userRoles: ['time-tracker-admin'] },
127+
{ roleId: 'test', roleValue: 'time-tracker-tester', userRoles: ['time-tracker-tester'] },
128+
];
129+
130+
revokeRoleTypes.map((param) => {
131+
it(`When user switchRole to ${param.roleId} and have that rol asigned, should revoke ${param.roleValue} Role`, () => {
132+
const roleId = param.roleId;
133+
const roleValue = param.roleValue;
134+
const userRoles = param.userRoles;
135+
const userId = 'userId';
136+
137+
spyOn(store, 'dispatch');
138+
139+
component.switchRole(userId, userRoles, roleId, roleValue);
140+
141+
expect(store.dispatch).toHaveBeenCalledWith(new RevokeRoleUser(userId, roleId));
142+
});
143+
});
74144

75145
it('on success load users, the data of roles should be an array and role null', () => {
76146
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
@@ -117,7 +187,11 @@ describe('UsersListComponent', () => {
117187
});
118188
});
119189

120-
it('on success load users, the datatable should be reloaded', async () => {
190+
/*
191+
TODO: block commented on purpose so that when the tests pass and the Feature toggle is removed,
192+
the table will be rendered again with dtInstance and not with dtOptions
193+
194+
it('on success load users, the datatable should be reloaded', async () => {
121195
const actionSubject = TestBed.inject(ActionsSubject);
122196
const action = {
123197
type: UserActionTypes.LOAD_USERS_SUCCESS,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
2929
private featureManagerService: FeatureManagerService
3030
) {
3131
this.isLoading$ = store.pipe(delay(0), select(getIsLoading));
32-
this.isFeatureToggleAactivated().subscribe((flag) => {
33-
this.isFlagOn = flag;
34-
});
3532
}
3633

3734
ngOnInit(): void {
35+
this.isFeatureToggleActivated().subscribe((flag) => {
36+
this.isFlagOn = flag;
37+
});
3838
this.store.dispatch(new LoadUsers());
3939
this.loadUsersSubscription = this.actionsSubject$
4040
.pipe(filter((action: any) => action.type === UserActionTypes.LOAD_USERS_SUCCESS))
@@ -83,7 +83,7 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
8383
: this.store.dispatch(new GrantRoleUser(userId, roleId));
8484
}
8585

86-
isFeatureToggleAactivated() {
86+
isFeatureToggleActivated() {
8787
return this.featureManagerService.isToggleEnabledForUser('ui-list-test-users').pipe(
8888
map((enabled) => {
8989
if (enabled === true) {

0 commit comments

Comments
 (0)