Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
1 change: 1 addition & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
"src/styles.scss",
"node_modules/ngx-toastr/toastr.css",
"./node_modules/ngx-ui-switch/ui-switch.component.css",
"node_modules/datatables.net-buttons-dt/css/buttons.dataTables.css"
],
"scripts": [
Expand Down
35 changes: 20 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
"@angular/platform-browser": "~10.2.2",
"@angular/platform-browser-dynamic": "~10.2.2",
"@angular/router": "~10.2.2",
"@azure/app-configuration": "^1.1.0",
"@azure/identity": "^1.1.0",
"@ngrx/effects": "^10.0.1",
"@ngrx/store": "^10.0.1",
"@ngrx/store-devtools": "^10.0.1",
"@types/datatables.net-buttons": "^1.4.3",
"angular-datatables": "^9.0.2",
"@azure/app-configuration": "^1.1.0",
"@azure/identity": "^1.1.0",
"bootstrap": "^4.4.1",
"datatables.net": "^1.10.21",
"datatables.net-buttons": "^1.6.2",
Expand All @@ -46,6 +46,7 @@
"ngx-material-timepicker": "^5.5.3",
"ngx-pagination": "^5.0.0",
"ngx-toastr": "^12.0.1",
"ngx-ui-switch": "^10.0.2",
"rxjs": "~6.6.3",
"tslib": "^1.10.0",
"zone.js": "~0.10.2"
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { DialogComponent } from './modules/shared/components/dialog/dialog.compo
import { LoadingBarComponent } from './modules/shared/components/loading-bar/loading-bar.component';
import { UsersComponent } from './modules/users/pages/users.component';
import { UsersListComponent } from './modules/users/components/users-list/users-list.component';
import { UiSwitchModule } from 'ngx-ui-switch';
import {NgxMaterialTimepickerModule} from 'ngx-material-timepicker';
// tslint:disable-next-line: max-line-length
import { TechnologyReportTableComponent } from './modules/technology-report/components/technology-report-table/technology-report-table.component';
Expand Down Expand Up @@ -142,6 +143,7 @@ const maskConfig: Partial<IConfig> = {
DataTablesModule,
AutocompleteLibModule,
NgxMaterialTimepickerModule,
UiSwitchModule,
StoreModule.forRoot(reducers, {
metaReducers,
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
<table *ngIf="users" class="table table-sm table-bordered table-striped mb-0" datatable [dtTrigger]="dtTrigger">
<table
*ngIf="users && (isLoading$ | async) === false"
class="table table-sm table-bordered table-striped mb-0"
datatable
[dtOptions]="dtOptions"
>
<thead class="thead-blue">
<tr class="d-flex">
<th class="col-6">User Email</th>
<th class="col-6">Names</th>
<th class="col">User Email</th>
<th class="col">Names</th>
<th class="col" *ngIf="isFlagOn">Roles</th>
</tr>
</thead>
<app-loading-bar *ngIf="isLoading$ | async"></app-loading-bar>
<tbody *ngIf="(isLoading$ | async) === false">
<tr class="d-flex" *ngFor="let user of users">
<td class="col-sm-6">{{ user.email }}</td>
<td class="col-sm-6">{{ user.name }}</td>
<td class="col">{{ user.email }}</td>
<td class="col">{{ user.name }}</td>
<td class="col text-center" *ngIf="isFlagOn">
<div>
<ui-switch
size="small"
(change)="switchRole(user.id, user.roles, 'admin', 'time-tracker-admin')"
[checked]="user.roles.includes('time-tracker-admin')"
></ui-switch>
admin
<ui-switch
size="small"
(change)="switchRole(user.id, user.roles, 'test', 'time-tracker-tester')"
[checked]="user.roles.includes('time-tracker-tester')"
></ui-switch>
test
</div>
</td>
</tr>
</tbody>
</table>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing';

import { NgxPaginationModule } from 'ngx-pagination';
import { UsersListComponent } from './users-list.component';
import { UserActionTypes, UserState, LoadUsers } from '../../store';
import { UserActionTypes, UserState, LoadUsers, GrantRoleUser, RevokeRoleUser } from '../../store';
import { ActionsSubject } from '@ngrx/store';
import { DataTablesModule } from 'angular-datatables';
import { of } from 'rxjs';

describe('UsersListComponent', () => {
let component: UsersListComponent;
Expand All @@ -29,13 +30,15 @@ describe('UsersListComponent', () => {
message: '',
};

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [NgxPaginationModule, DataTablesModule],
declarations: [UsersListComponent],
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
}).compileComponents();
}));
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [NgxPaginationModule, DataTablesModule],
declarations: [UsersListComponent],
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
}).compileComponents();
})
);

beforeEach(() => {
fixture = TestBed.createComponent(UsersListComponent);
Expand Down Expand Up @@ -69,6 +72,76 @@ describe('UsersListComponent', () => {
expect(component.users).toEqual(state.data);
});

it('When Component is created, should call the feature toggle method', () => {
spyOn(component, 'isFeatureToggleActivated').and.returnValue(of(true));

component.ngOnInit();

expect(component.isFeatureToggleActivated).toHaveBeenCalled();
expect(component.isFlagOn).toBe(true);
});

const actionsParams = [
{ actionType: UserActionTypes.GRANT_USER_ROLE_SUCCESS },
{ actionType: UserActionTypes.REVOKE_USER_ROLE_SUCCESS },
];

actionsParams.map((param) => {
it(`When action ${param.actionType} is dispatched should triggered load Users action`, () => {
spyOn(store, 'dispatch');

const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
const action = {
type: param.actionType,
payload: state.data,
};

actionSubject.next(action);

expect(store.dispatch).toHaveBeenCalledWith(new LoadUsers());
});
});

const grantRoleTypes = [
{ roleId: 'admin', roleValue: 'time-tracker-admin' },
{ roleId: 'test', roleValue: 'time-tracker-tester' },
];

grantRoleTypes.map((param) => {
it(`When user switchRole to ${param.roleId} and don't have any role, should grant ${param.roleValue} Role`, () => {
const roleId = param.roleId;
const roleValue = param.roleValue;
const userRoles = [];
const userId = 'userId';

spyOn(store, 'dispatch');

component.switchRole(userId, userRoles, roleId, roleValue);

expect(store.dispatch).toHaveBeenCalledWith(new GrantRoleUser(userId, roleId));
});
});

const revokeRoleTypes = [
{ roleId: 'admin', roleValue: 'time-tracker-admin', userRoles: ['time-tracker-admin'] },
{ roleId: 'test', roleValue: 'time-tracker-tester', userRoles: ['time-tracker-tester'] },
];

revokeRoleTypes.map((param) => {
it(`When user switchRole to ${param.roleId} and have that rol asigned, should revoke ${param.roleValue} Role`, () => {
const roleId = param.roleId;
const roleValue = param.roleValue;
const userRoles = param.userRoles;
const userId = 'userId';

spyOn(store, 'dispatch');

component.switchRole(userId, userRoles, roleId, roleValue);

expect(store.dispatch).toHaveBeenCalledWith(new RevokeRoleUser(userId, roleId));
});
});

it('on success load users, the data of roles should be an array and role null', () => {
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
const action = {
Expand Down Expand Up @@ -114,7 +187,11 @@ describe('UsersListComponent', () => {
});
});

it('on success load users, the datatable should be reloaded', async () => {
/*
TODO: block commented on purpose so that when the tests pass and the Feature toggle is removed,
the table will be rendered again with dtInstance and not with dtOptions

it('on success load users, the datatable should be reloaded', async () => {
const actionSubject = TestBed.inject(ActionsSubject);
const action = {
type: UserActionTypes.LOAD_USERS_SUCCESS,
Expand All @@ -125,7 +202,7 @@ describe('UsersListComponent', () => {
actionSubject.next(action);

expect(component.dtElement.dtInstance.then).toHaveBeenCalled();
});
});*/

afterEach(() => {
component.dtTrigger.unsubscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular
import { ActionsSubject, select, Store } from '@ngrx/store';
import { DataTableDirective } from 'angular-datatables';
import { Observable, Subject, Subscription } from 'rxjs';
import { delay, filter } from 'rxjs/operators';
import { delay, filter, map } from 'rxjs/operators';
import { User } from '../../models/users';
import { LoadUsers, UserActionTypes } from '../../store/user.actions';
import { GrantRoleUser, LoadUsers, RevokeRoleUser, UserActionTypes } from '../../store/user.actions';
import { getIsLoading } from '../../store/user.selectors';
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';

@Component({
selector: 'app-users-list',
templateUrl: './users-list.component.html',
Expand All @@ -18,19 +20,41 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
dtTrigger: Subject<any> = new Subject();
@ViewChild(DataTableDirective, { static: false })
dtElement: DataTableDirective;
dtOptions: any = {};
isFlagOn;

constructor(private store: Store<User>, private actionsSubject$: ActionsSubject) {
constructor(
private store: Store<User>,
private actionsSubject$: ActionsSubject,
private featureManagerService: FeatureManagerService
) {
this.isLoading$ = store.pipe(delay(0), select(getIsLoading));
}

ngOnInit(): void {
this.isFeatureToggleActivated().subscribe((flag) => {
this.isFlagOn = flag;
});
this.store.dispatch(new LoadUsers());
this.loadUsersSubscription = this.actionsSubject$
.pipe(filter((action: any) => action.type === UserActionTypes.LOAD_USERS_SUCCESS))
.subscribe((action) => {
this.users = action.payload;
this.rerenderDataTable();
});
this.store.dispatch(new LoadUsers());

this.loadUsersSubscription = this.actionsSubject$
.pipe(
filter(
(action: any) =>
action.type === UserActionTypes.GRANT_USER_ROLE_SUCCESS ||
action.type === UserActionTypes.REVOKE_USER_ROLE_SUCCESS
)
)
.subscribe((action) => {
this.store.dispatch(new LoadUsers());
this.rerenderDataTable();
});
}

ngAfterViewInit(): void {
Expand All @@ -52,4 +76,22 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
this.dtTrigger.next();
}
}

switchRole(userId: string, userRoles: string[], roleId: string, roleValue: string) {
userRoles.includes(roleValue)
? this.store.dispatch(new RevokeRoleUser(userId, roleId))
: this.store.dispatch(new GrantRoleUser(userId, roleId));
}

isFeatureToggleActivated() {
return this.featureManagerService.isToggleEnabledForUser('ui-list-test-users').pipe(
map((enabled) => {
if (enabled === true) {
return true;
} else {
return false;
}
})
);
}
}
Loading