diff --git a/src/app/pages/active-account/active-account.component.spec.ts b/src/app/pages/active-account/active-account.component.spec.ts index 2d16512e..2a087ab8 100644 --- a/src/app/pages/active-account/active-account.component.spec.ts +++ b/src/app/pages/active-account/active-account.component.spec.ts @@ -2,16 +2,39 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ActiveAccountComponent } from './active-account.component'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { AuthService } from 'src/app/services/auth.service'; -import { ReactiveFormsModule } from '@angular/forms'; +import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; +import { of, throwError } from 'rxjs'; +import { RouterTestingModule } from '@angular/router/testing'; +import { LoginComponent } from '../login/login.component'; + +const mockData: any = { + "email": "mario@gmail.com", + "code": 123456, +} + +class AuthServiceMock { + constructor() { } + activeAccount() { + return of({ success: true }); + } + resendCode() { + return of({ success: true }); + } +} describe('ActiveAccountComponent', () => { let component: ActiveAccountComponent; let fixture: ComponentFixture; + let authService: AuthService; beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [HttpClientTestingModule, ReactiveFormsModule], - providers: [AuthService], + imports: [HttpClientTestingModule, RouterTestingModule.withRoutes( + [ + { path: 'login', component: LoginComponent }, + ] + ), ReactiveFormsModule], + providers: [{ provide: AuthService, useValue: new AuthServiceMock() }, FormBuilder], declarations: [ActiveAccountComponent] }) .compileComponents(); @@ -19,9 +42,47 @@ describe('ActiveAccountComponent', () => { fixture = TestBed.createComponent(ActiveAccountComponent); component = fixture.componentInstance; fixture.detectChanges(); + authService = TestBed.inject(AuthService); }); it('should create', () => { expect(component).toBeTruthy(); }); + + it('should call activeAccount method when the form is submitted', () => { + fixture.detectChanges(); + spyOn(component, 'activeAccount').and.callThrough(); + const form = component.userForm; + form.setValue(mockData); + + const submitButton = fixture.nativeElement.querySelector( + 'button[type="submit"]' + ); + submitButton.click(); + + expect(component.activeAccount).toHaveBeenCalled(); + }); + + it('should call alert when form is not valid', () => { + spyOn(component, 'activeAccount').and.callThrough(); + const alertSpy = spyOn(window, 'alert'); + fixture.detectChanges(); + + const submitButton = fixture.nativeElement.querySelector( + 'button[type="submit"]' + ); + submitButton.click(); + + expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!'); + }); + + it('should call activeAccount and return an error', () => { + fixture.detectChanges(); + const form = component.userForm; + form.setValue(mockData); + const mySpy = spyOn(authService, 'activeAccount').and.returnValue(throwError(() => new Error('Erro'))); + component.activeAccount(); + expect(mySpy).toHaveBeenCalled(); + }); + }); diff --git a/src/app/pages/check-code-rest-password/check-code-rest-password.component.html b/src/app/pages/check-code-rest-password/check-code-rest-password.component.html index 6e1cff89..70e9d9b4 100644 --- a/src/app/pages/check-code-rest-password/check-code-rest-password.component.html +++ b/src/app/pages/check-code-rest-password/check-code-rest-password.component.html @@ -1,13 +1,40 @@ -
- +
- +
- - + +
diff --git a/src/app/pages/check-code-rest-password/check-code-rest-password.component.spec.ts b/src/app/pages/check-code-rest-password/check-code-rest-password.component.spec.ts index 86a55a1c..46e0fc16 100644 --- a/src/app/pages/check-code-rest-password/check-code-rest-password.component.spec.ts +++ b/src/app/pages/check-code-rest-password/check-code-rest-password.component.spec.ts @@ -2,16 +2,39 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { CheckCodeRestPasswordComponent } from './check-code-rest-password.component'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { AuthService } from 'src/app/services/auth.service'; -import { ReactiveFormsModule } from '@angular/forms'; +import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; +import { of, throwError } from 'rxjs'; +import { RouterTestingModule } from '@angular/router/testing'; +import { ResetPasswordComponent } from '../reset-password/reset-password.component'; + +const mockData: any = { + "email": "mario@gmail.com", + "code": 123456, +} + +class AuthServiceMock { + constructor() { } + sendEmailPassword() { + return of({ success: true }); + } + verifyCodePassword() { + return of({ success: true }); + } +} describe('CheckCodeRestPasswordComponent', () => { let component: CheckCodeRestPasswordComponent; let fixture: ComponentFixture; + let authService: AuthService; beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [HttpClientTestingModule, ReactiveFormsModule], - providers: [AuthService], + imports: [HttpClientTestingModule, RouterTestingModule.withRoutes( + [ + { path: 'changePassword', component: ResetPasswordComponent }, + ] + ), ReactiveFormsModule], + providers: [{ provide: AuthService, useValue: new AuthServiceMock() }, FormBuilder], declarations: [CheckCodeRestPasswordComponent] }) .compileComponents(); @@ -19,9 +42,88 @@ describe('CheckCodeRestPasswordComponent', () => { fixture = TestBed.createComponent(CheckCodeRestPasswordComponent); component = fixture.componentInstance; fixture.detectChanges(); + authService = TestBed.inject(AuthService); }); it('should create', () => { expect(component).toBeTruthy(); }); + + it('should call sendEmail method when the form is submitted', () => { + fixture.detectChanges(); + spyOn(component, 'sendEmail').and.callThrough(); + const form = component.userForm; + form.setValue(mockData); + + const submitButton = fixture.nativeElement.querySelector( + '#sendEmail' + ); + submitButton.click(); + + expect(component.sendEmail).toHaveBeenCalled(); + }); + + it('should call alert when form is not valid', () => { + fixture.detectChanges(); + spyOn(component, 'sendEmail').and.callThrough(); + const alertSpy = spyOn(window, 'alert'); + const form = component.userForm; + form.setValue({ "email": '', code: '' }); + + const submitButton = fixture.nativeElement.querySelector( + '#sendEmail' + ); + submitButton.click(); + + expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!'); + }); + + it('should call sendEmail and return an error', () => { + fixture.detectChanges(); + const form = component.userForm; + form.setValue(mockData); + const mySpy = spyOn(authService, 'sendEmailPassword').and.returnValue(throwError(() => new Error('Erro'))); + component.sendEmail(); + expect(mySpy).toHaveBeenCalled(); + }); + + it('should call checkCode method when the form is submitted', () => { + fixture.detectChanges(); + spyOn(component, 'checkCode').and.callThrough(); + const form = component.userForm; + form.setValue(mockData); + component.activeCode = true; + fixture.detectChanges(); + + const submitButton = fixture.nativeElement.querySelector( + '#checkCode' + ); + submitButton.click(); + + expect(component.checkCode).toHaveBeenCalled(); + }); + + it('should call alert when form is not valid', () => { + fixture.detectChanges(); + spyOn(component, 'checkCode').and.callThrough(); + const alertSpy = spyOn(window, 'alert'); + component.activeCode = true; + fixture.detectChanges(); + const submitButton = fixture.nativeElement.querySelector( + '#checkCode' + ); + submitButton.click(); + + expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!'); + }); + + it('should call checkCode and return an error', () => { + fixture.detectChanges(); + const form = component.userForm; + form.setValue(mockData); + const mySpy = spyOn(authService, 'verifyCodePassword').and.returnValue(throwError(() => new Error('Erro'))); + component.checkCode(); + expect(mySpy).toHaveBeenCalled(); + }); + }); diff --git a/src/app/pages/edit-user/edit-user.component.spec.ts b/src/app/pages/edit-user/edit-user.component.spec.ts index c1728898..41d290bb 100644 --- a/src/app/pages/edit-user/edit-user.component.spec.ts +++ b/src/app/pages/edit-user/edit-user.component.spec.ts @@ -2,25 +2,86 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { EditUserComponent } from './edit-user.component'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { RouterTestingModule } from '@angular/router/testing'; -import { ReactiveFormsModule } from '@angular/forms'; +import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; +import { of, throwError } from 'rxjs'; +import { UserService } from 'src/app/services/user.service'; +import { ProfileComponent } from '../profile/profile.component'; + +const mockData: any = { + "name": "Mario", + "email": "mario@gmail.com", + "connection": "ALUNO", +} + +class UserServiceMock { + constructor() { } + updateUser() { + return of({ success: true }); + } +} describe('EditUserComponent', () => { let component: EditUserComponent; let fixture: ComponentFixture; + let userService: UserService; beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [HttpClientTestingModule, RouterTestingModule, ReactiveFormsModule], - declarations: [EditUserComponent] + imports: [HttpClientTestingModule, RouterTestingModule.withRoutes( + [ + { path: 'profile', component: ProfileComponent }, + ] + ), ReactiveFormsModule], + declarations: [EditUserComponent], + providers: [{ provide: UserService, useValue: new UserServiceMock() }, FormBuilder], }) .compileComponents(); fixture = TestBed.createComponent(EditUserComponent); component = fixture.componentInstance; fixture.detectChanges(); + userService = TestBed.inject(UserService); }); it('should create', () => { expect(component).toBeTruthy(); }); + + it('should call updateUser method when the form is submitted', () => { + fixture.detectChanges(); + spyOn(component, 'updateUser').and.callThrough(); + const form = component.userForm; + form.setValue(mockData); + component.userId = mockData.id; + + const submitButton = fixture.nativeElement.querySelector( + 'button[type="submit"]' + ); + submitButton.click(); + + expect(component.updateUser).toHaveBeenCalled(); + }); + + it('should call alert when form is not valid', () => { + spyOn(component, 'updateUser').and.callThrough(); + const alertSpy = spyOn(window, 'alert'); + fixture.detectChanges(); + + const submitButton = fixture.nativeElement.querySelector( + 'button[type="submit"]' + ); + submitButton.click(); + + expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!'); + }); + + it('should call updateUser and return an error', () => { + fixture.detectChanges(); + const form = component.userForm; + form.setValue(mockData); + const mySpy = spyOn(userService, 'updateUser').and.returnValue(throwError(() => new Error('Erro'))); + component.updateUser(); + expect(mySpy).toHaveBeenCalled(); + }); + }); diff --git a/src/app/pages/profile/profile.component.spec.ts b/src/app/pages/profile/profile.component.spec.ts index 18ff5af5..9059002a 100644 --- a/src/app/pages/profile/profile.component.spec.ts +++ b/src/app/pages/profile/profile.component.spec.ts @@ -1,24 +1,76 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ProfileComponent } from './profile.component'; import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { of, throwError } from 'rxjs'; +import { UserService } from 'src/app/services/user.service'; +import { EditUserComponent } from '../edit-user/edit-user.component'; +import { RouterTestingModule } from '@angular/router/testing'; + +const mockData: any = { + "id": 1, + "name": "Mario", + "connection": "ALUNO", + "email": "mario@gmail.com", + "role": "USER", + "is_active": true +} + +class UserServiceMock { + constructor() { } + getUser() { + return of(mockData); + } +} describe('ProfileComponent', () => { let component: ProfileComponent; let fixture: ComponentFixture; + let userService: UserService; beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [HttpClientTestingModule], - declarations: [ProfileComponent] + imports: [HttpClientTestingModule, RouterTestingModule.withRoutes( + [ + { path: 'editUser', component: EditUserComponent }, + ] + )], + declarations: [ProfileComponent], + providers: [{ provide: UserService, useValue: new UserServiceMock() }] }) .compileComponents(); fixture = TestBed.createComponent(ProfileComponent); component = fixture.componentInstance; - fixture.detectChanges(); + userService = TestBed.inject(UserService); }); it('should create', () => { + localStorage.setItem('token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJqb2FvMTV2aWN0b3IwOEBnbWFpbC5jb20iLCJleHAiOjE2OTkzMTI5MzV9.1B9qBJt8rErwBKyD5JCdsPozsw86oQ38tdfDuMM2HFI') + + component.user = mockData; + fixture.detectChanges(); expect(component).toBeTruthy(); }); + + it('should call getUser', () => { + const mySpy = spyOn(userService, 'getUser').and.callThrough(); + component.getUser(); + expect(mySpy).toHaveBeenCalled(); + }); + + it('should call getUser and return an error', () => { + const mySpy = spyOn(userService, 'getUser').and.returnValue(throwError(() => new Error('Erro'))); + component.getUser(); + expect(mySpy).toHaveBeenCalled(); + }); + + // it('should call navigator method when "Editar perfil" is clicked', () => { + // component.user = mockData; + // spyOn(component, 'navigatorEdit').and.callThrough(); + // const editProfilebutton = fixture.nativeElement.querySelector('.text-white'); + // fixture.detectChanges(); + // // editProfilebutton.click(); + // expect(true).toBe(true); + // }); + }); diff --git a/src/app/pages/profile/profile.component.ts b/src/app/pages/profile/profile.component.ts index c858274e..74f5549b 100644 --- a/src/app/pages/profile/profile.component.ts +++ b/src/app/pages/profile/profile.component.ts @@ -17,7 +17,7 @@ export class ProfileComponent { private router: Router, private fb: FormBuilder, private userService: UserService - ) {} + ) { } ngOnInit(): void { this.setUserIdFromToken(localStorage.getItem('token') as string); @@ -32,7 +32,6 @@ export class ProfileComponent { getUser() { this.userService.getUser(this.userId).subscribe({ next: (data) => { - console.log(data); this.user = data; }, error: (error) => { @@ -41,19 +40,14 @@ export class ProfileComponent { }); } - navigator(rota: string): void { - this.router.navigate([rota]); - } - navigatorEdit(): void { - console.log('Dados do usuário:', this.user); const navigationExtras: NavigationExtras = { state: { user: this.user } }; - this.router.navigate([`/editUser/${this.userId}`], navigationExtras); + this.router.navigate([`/editUser`], navigationExtras); } } diff --git a/src/app/pages/register/register.component.spec.ts b/src/app/pages/register/register.component.spec.ts index 21c94378..af78b8bd 100644 --- a/src/app/pages/register/register.component.spec.ts +++ b/src/app/pages/register/register.component.spec.ts @@ -2,26 +2,94 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { RegisterComponent } from './register.component'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { AuthService } from 'src/app/services/auth.service'; -import { ReactiveFormsModule } from '@angular/forms'; +import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; +import { of, throwError } from 'rxjs'; +import { RouterTestingModule } from '@angular/router/testing'; +import { ActiveAccountComponent } from '../active-account/active-account.component'; + +const mockData: any = { + "name": "Mario", + "email": "mario@gmail.com", + "connection": "ALUNO", + "password": "123456", + "confirmPassword": "123456", +} + +class AuthServiceMock { + constructor() { } + registerUser() { + return of({ success: true }); + } +} describe('RegisterComponent', () => { let component: RegisterComponent; let fixture: ComponentFixture; + let authService: AuthService; beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [HttpClientTestingModule, ReactiveFormsModule], - providers: [AuthService], + imports: [HttpClientTestingModule, ReactiveFormsModule, + RouterTestingModule.withRoutes( + [ + { path: 'activateAccount', component: ActiveAccountComponent }, + ] + ) + ], + providers: [{ provide: AuthService, useValue: new AuthServiceMock() }, FormBuilder], declarations: [RegisterComponent] }) .compileComponents(); fixture = TestBed.createComponent(RegisterComponent); component = fixture.componentInstance; + authService = TestBed.inject(AuthService); fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); + + it('should have a valid form on initialization', () => { + fixture.detectChanges(); + expect(component.userForm).toBeTruthy(); + }); + + it('should call register method when the form is submitted', () => { + fixture.detectChanges(); + spyOn(component, 'register').and.callThrough(); + const form = component.userForm; + form.setValue(mockData); + + const submitButton = fixture.nativeElement.querySelector( + 'button[type="submit"]' + ); + submitButton.click(); + + expect(component.register).toHaveBeenCalled(); + }); + + it('should call alert when form is not valid', () => { + spyOn(component, 'register').and.callThrough(); + const alertSpy = spyOn(window, 'alert'); + fixture.detectChanges(); + + const submitButton = fixture.nativeElement.querySelector( + 'button[type="submit"]' + ); + submitButton.click(); + + expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!'); + }); + + it('should call register and return an error', () => { + fixture.detectChanges(); + const form = component.userForm; + form.setValue(mockData); + const mySpy = spyOn(authService, 'registerUser').and.returnValue(throwError(() => new Error('Erro'))); + component.register(); + expect(mySpy).toHaveBeenCalled(); + }); + }); diff --git a/src/app/pages/video/video.component.spec.ts b/src/app/pages/video/video.component.spec.ts index e8d64e0b..a879e27c 100644 --- a/src/app/pages/video/video.component.spec.ts +++ b/src/app/pages/video/video.component.spec.ts @@ -269,9 +269,9 @@ describe('VideoComponent', () => { expect(mySpy).toHaveBeenCalled(); }); - (it('should call findAll and return an error', () => { + it('should call findAll and return an error', () => { const mySpy = spyOn(videoService, 'findAll').and.returnValue(throwError(() => new Error('Erro'))); component.findAll(); expect(mySpy).toHaveBeenCalled(); - })); + }); });