Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…V-Frontend into 54-grade-programacao
  • Loading branch information
geraldovictor committed Nov 20, 2023
2 parents 891ee47 + 7ccefd8 commit 427a610
Show file tree
Hide file tree
Showing 16 changed files with 470 additions and 11 deletions.
2 changes: 1 addition & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
"maximumError": "2mb"
},
{
"type": "anyComponentStyle",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
"private": true,
"dependencies": {
"@angular/animations": "^15.2.0",
"@angular/cdk": "^15.2.9",
"@angular/common": "^15.2.0",
"@angular/compiler": "^15.2.0",
"@angular/core": "^15.2.0",
"@angular/forms": "^15.2.0",
"@angular/material": "^15.2.9",
"@angular/platform-browser": "^15.2.0",
"@angular/platform-browser-dynamic": "^15.2.0",
"@angular/router": "^15.2.0",
Expand Down
3 changes: 3 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { CheckCodeRestPasswordComponent } from './pages/check-code-rest-password
import { ResetPasswordComponent } from './pages/reset-password/reset-password.component';
import { AuthGuard } from './guard/auth.guard';
import { EditUserComponent } from './pages/edit-user/edit-user.component';
import { UpdateRoleComponent } from './pages/update-role/update-role.component';
import { AdminGuard } from './services/admin.guard';
import { SuggestAgendaComponent } from './pages/suggest-agenda/suggest-agenda.component';
import { ParticipateComponent } from './pages/participate/participate.component';
import { GridDaysComponent } from './pages/grid-days/grid-days.component';
Expand All @@ -34,6 +36,7 @@ const routes: Routes = [
{ path: 'editUser/:id', component: EditUserComponent, canActivate: [AuthGuard], },
{ path: 'grid-days', component: GridDaysComponent },
{ path: 'grid-days/:day', component: GridComponent },
{ path: 'update-role', component: UpdateRoleComponent, canActivate: [AdminGuard], }
];

@NgModule({
Expand Down
11 changes: 8 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Import
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { ReactiveFormsModule } from '@angular/forms';
import { ToastModule } from 'primeng/toast';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
import { ConfirmationService } from 'primeng/api';
Expand Down Expand Up @@ -30,6 +30,8 @@ import { CheckCodeRestPasswordComponent } from './pages/check-code-rest-password
import { AuthGuard } from './guard/auth.guard';
import { AuthService } from './services/auth.service';
import { EditUserComponent } from './pages/edit-user/edit-user.component';
import { UpdateRoleComponent } from './pages/update-role/update-role.component';
import {MatPaginatorModule} from '@angular/material/paginator';
import { MenuModule } from 'primeng/menu';
import { VideoCommentComponent } from './components/video-comment/video-comment.component';
import { SuggestAgendaComponent } from './pages/suggest-agenda/suggest-agenda.component';
Expand All @@ -47,13 +49,15 @@ import { ProgressSpinnerModule } from 'primeng/progressspinner';
ReactiveFormsModule,
ToastModule,
ConfirmDialogModule,
BrowserAnimationsModule,
OAuthModule.forRoot(),
InputTextModule,
DropdownModule,
ButtonModule,
ProgressSpinnerModule,
MenuModule
MenuModule,
FormsModule,
BrowserAnimationsModule,
MatPaginatorModule
],
declarations: [
AppComponent,
Expand All @@ -69,6 +73,7 @@ import { ProgressSpinnerModule } from 'primeng/progressspinner';
ResetPasswordComponent,
CheckCodeRestPasswordComponent,
EditUserComponent,
UpdateRoleComponent,
SuggestAgendaComponent,
ParticipateComponent,
GridComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/app/guard/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export class AuthGuard implements CanActivate {
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion src/app/interceptor/user-token-interceptor.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('TokenInterceptorService', () => {

it('should add header Authorization to request', () => {
localStorage.setItem('token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJqb2FvMTV2aWN0b3IwOEBnbWFpbC5jb20iLCJleHAiOjE2OTkzMTI5MzV9.1B9qBJt8rErwBKyD5JCdsPozsw86oQ38tdfDuMM2HFI');
userService.getAllUsers().subscribe((res) => {
userService.getAllUsers({}).subscribe((res) => {
expect(res).toBeTruthy();
});
const req = httpMock.expectOne(`${userService.usersAPIURL}/users`);
Expand Down
1 change: 1 addition & 0 deletions src/app/pages/profile/profile.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { HttpErrorResponse } from '@angular/common/http';

type ErrorResponseType = HttpErrorResponse;


@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
Expand Down
39 changes: 39 additions & 0 deletions src/app/pages/update-role/update-role.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
table {
border-collapse: collapse;
width: 100%;
border: 1px solid #ccc;
}


th {
background-color: #f2f2f2;
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}


tr:nth-child(even) {
background-color: #f2f2f2;
}

tr:nth-child(odd) {
background-color: #ffffff;
}


td {
border: 1px solid #ccc;
padding: 8px;
}


tr:hover {
background-color: #e0e0e0;
}

.filter-container {
display: flex;
align-items: flex-start;
column-gap: 6px;
}
79 changes: 79 additions & 0 deletions src/app/pages/update-role/update-role.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<div class="mx-5">
<div class="filter-container">
<div>
<label>Nome ou Email:</label>
<input
[(ngModel)]="filterInputValue"
(change)="filterUser()"
placeholder="Filtrar pelo nome do usuário"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-100 p-2.5 outline-none"
/>
</div>
<div>
<label>Vínculo:</label>
<select
(change)="filterUser()"
[(ngModel)]="filterConnectionValue"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-100 p-2.5 outline-none"
>
<option disabled hidden value="">Selecione um vínculo</option>
<option value="">Sem Filtro</option>
<option value="ESTUDANTE">Estudante</option>
<option value="PROFESSOR">Professor</option>
<option value="SERVIDOR">Servidor</option>
<option value="TERCEIRIZADO">Terceirizado</option>
<option value="ESTAGIARIO">Estagiario</option>
<option value="COMUNIDADE EXTERNA">Comunidade Externa</option>
</select>
</div>
</div>
<div *ngIf="users && users.length; else noUsers">
<table
class="table-auto mt-3"
style="border-collapse: collapse !important"
aria-label="Tabela de usuários"
>
<thead>
<tr>
<th>Nome</th>
<th>Email</th>
<th>Vínculo</th>
<th>Cargo</th>
<th>Ativo</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of users">
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.connection }}</td>
<td>
<select
class="bg-gray-100 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 outline-none"
[(ngModel)]="user.role"
(change)="updateUserRole(user.id)"
*ngIf="user.id !== userId; else readOnly"
>
<option value="USER">Usuário</option>
<option value="ADMIN">Administrador</option>
</select>
<ng-template #readOnly> Administrador </ng-template>
</td>
<td>{{ user.is_active ? "Sim" : "Não" }}</td>
</tr>
</tbody>
</table>
<mat-paginator
[pageIndex]="pageIndex"
[length]="total"
[pageSize]="pageSize"
[pageSizeOptions]="[5, 10, 25]"
aria-label="User Pagination"
(page)="onPaginateChange($event)"
></mat-paginator>
</div>

<ng-template #noUsers>
<h3>Nenhum usuário encontrado</h3>
</ng-template>
</div>
134 changes: 134 additions & 0 deletions src/app/pages/update-role/update-role.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { UserService } from 'src/app/services/user.service';
import { of, throwError } from 'rxjs';
import { FormsModule } from '@angular/forms';

import { UpdateRoleComponent } from './update-role.component';
import { MatPaginatorModule, PageEvent } from '@angular/material/paginator';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

const mockData = [
{
"connection": "PROFESSOR",
"id": 1,
"name": "Luisa",
"email": "[email protected]",
"role": "USER",
"is_active": null,
},
{
"connection": "ESTUDANTE",
"id": 2,
"name": "Renato",
"email": "[email protected]",
"role": "ADMIN",
"is_active": true,
},
{
"connection": "SERVIDOR",
"id": 3,
"name": "Paolla",
"email": "[email protected]",
"role": "USER",
"is_active": true,
}
]

class UserServiceMock {
constructor() { }
getAllUsers() {
return of(mockData);
}
updateUserRole() {
return of({});
}
}

describe('UpdateRoleComponent', () => {
let component: UpdateRoleComponent;
let fixture: ComponentFixture<UpdateRoleComponent>;
let userService: UserService;
let httpMock: HttpTestingController;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [UpdateRoleComponent],
imports: [HttpClientTestingModule, MatPaginatorModule, FormsModule, BrowserAnimationsModule],
providers: [{ provide: UserService, useValue: new UserServiceMock() }]
})
.compileComponents();

fixture = TestBed.createComponent(UpdateRoleComponent);
component = fixture.componentInstance;
userService = TestBed.inject(UserService);
httpMock = TestBed.inject(HttpTestingController);
});

it('should create', () => {
let adminToken: string = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJsdWNhc2NhbmRyYWRleDFAZ21haWwuY29tIiwicm9sZSI6IkFETUlOIiwiZXhwIjoxNzAwMTAwMDQxfQ.aDhw1xkK55bhUQCm6tSxX4LYxq8hP_b3T8gYUS449F8"
localStorage.setItem("token", adminToken)

component.users = mockData;
fixture.detectChanges();
expect(component).toBeTruthy();
});

it('should get all users', () => {
const mySpy = spyOn(userService, 'getAllUsers').and.callThrough();
component.getAllUsers();
expect(mySpy).toHaveBeenCalled();
});


it('should get all users and data headers is null', () => {
const mySpy = spyOn(userService, 'getAllUsers').and.returnValue(of({ headers: null, body: [] }));
component.getAllUsers();
expect(mySpy).toHaveBeenCalled();
expect(component.users).toEqual([]);
});



it('should get all users and return error', () => {
const mySpy = spyOn(userService, 'getAllUsers').and.returnValue(throwError(() => new Error('Erro')));
component.getAllUsers();
expect(mySpy).toHaveBeenCalled();
});

it('should update pageSize and pageIndex onPaginateChange', () => {
const event: PageEvent = {
length: 50,
pageIndex: 1,
pageSize: 10,
previousPageIndex: 0
};

component.onPaginateChange(event);

expect(component.pageSize).toEqual(event.pageSize);
expect(component.pageIndex).toEqual(event.pageIndex);
});

it('should filter users from get users', () => {
const mySpy = spyOn(userService, 'getAllUsers').and.callThrough();
component.filterUser();
expect(mySpy).toHaveBeenCalled();
});

it('should update user role', () => {
const mySpy = spyOn(userService, 'updateUserRole').and.callThrough();
component.updateUserRole(1);
expect(mySpy).toHaveBeenCalled();
});

it('should update user role and return error', () => {
const mySpy = spyOn(userService, 'updateUserRole').and.returnValue(throwError(() => new Error('Erro')));
component.updateUserRole(1);
expect(mySpy).toHaveBeenCalled();
});


});
Loading

0 comments on commit 427a610

Please sign in to comment.