Skip to content

Commit

Permalink
Merge pull request #1107 from ORCID/clean-up-tests
Browse files Browse the repository at this point in the history
Clean up tests
  • Loading branch information
bobcaprice authored Feb 7, 2024
2 parents ef1899e + 99dca16 commit f22f759
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 64 deletions.
6 changes: 2 additions & 4 deletions ui/src/app/account/login/login.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('LoginComponent', () => {
expect(component).toBeTruthy()
})

it('should call loginService.login and handle successful login', fakeAsync(() => {
it('should call loginService.login and handle successful login', () => {
const mockLoginResult = { mfaRequired: false }
loginService.login.and.returnValue(of(mockLoginResult))
accountService.getAccountData.and.returnValue(
Expand Down Expand Up @@ -68,11 +68,9 @@ describe('LoginComponent', () => {

component.login()

tick() // Wait for Observable to emit

expect(component.showMfa).toBe(false)
expect(component.authenticationError).toBe(false)
}))
})

it('should handle MFA required', fakeAsync(() => {
const mockLoginResult = { mfaRequired: true }
Expand Down
9 changes: 6 additions & 3 deletions ui/src/app/account/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AfterViewInit, Component, OnDestroy, Renderer2 } from '@angular/core'
import { AfterViewInit, Component, NgZone, OnDestroy, Renderer2 } from '@angular/core'
import { FormBuilder } from '@angular/forms'
import { Router } from '@angular/router'
import { AccountService } from '../service/account.service'
Expand Down Expand Up @@ -34,7 +34,8 @@ export class LoginComponent implements AfterViewInit, OnDestroy {
private router: Router,
private accountService: AccountService,
private fb: FormBuilder,
private eventService: EventService
private eventService: EventService,
private ngZone: NgZone
) {
this.sub = this.eventService.on(EventType.LOG_IN_SUCCESS).subscribe((e) => {
console.log(e.payload)
Expand Down Expand Up @@ -104,7 +105,9 @@ export class LoginComponent implements AfterViewInit, OnDestroy {
}

loginSuccess(): void {
this.router.navigate([''])
this.ngZone.run(() => {
this.router.navigate([''])
})

this.eventService.broadcast(new Event(EventType.LOG_IN_SUCCESS, 'logged in'))

Expand Down
44 changes: 22 additions & 22 deletions ui/src/app/account/password/password-reset-finish.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing'

import { PasswordResetFinishComponent } from './password-reset-finish.component';
import { AppModule } from 'src/app/app.module';
import { PasswordService } from '../service/password.service';
import { of, throwError } from 'rxjs';
import { PasswordResetFinishComponent } from './password-reset-finish.component'
import { AppModule } from 'src/app/app.module'
import { PasswordService } from '../service/password.service'
import { of, throwError } from 'rxjs'

describe('PasswordResetFinishComponent', () => {
let component: PasswordResetFinishComponent;
let fixture: ComponentFixture<PasswordResetFinishComponent>;
let service: PasswordService;
let component: PasswordResetFinishComponent
let fixture: ComponentFixture<PasswordResetFinishComponent>
let service: PasswordService

beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppModule],
declarations: [PasswordResetFinishComponent]
});
fixture = TestBed.createComponent(PasswordResetFinishComponent);
declarations: [PasswordResetFinishComponent],
})
fixture = TestBed.createComponent(PasswordResetFinishComponent)
service = fixture.debugElement.injector.get(PasswordService)
component = fixture.componentInstance;
fixture.detectChanges();
});
component = fixture.componentInstance
fixture.detectChanges()
})

it('should create', () => {
expect(component).toBeTruthy();
});
expect(component).toBeTruthy()
})

it('password should save successfully', () => {
spyOn(service, 'savePassword').and.returnValue(of(true))
component.finishReset();
component.finishReset()
expect(component.success).toEqual('OK')
});
})

it('password save should fail', () => {
spyOn(service, 'savePassword').and.returnValue(throwError(() => new Error('error')))
component.finishReset();
component.finishReset()
expect(component.error).toEqual('ERROR')
expect(component.success).toBeFalsy();
});
});
expect(component.success).toBeFalsy()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { EMAIL_NOT_FOUND_TYPE } from 'src/app/app.constants'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { By } from '@angular/platform-browser'
import { PasswordResetInitResult } from '../model/password-reset-init-result.model'
import { ReactiveFormsModule } from '@angular/forms'

describe('Component Tests', () => {
describe('PasswordResetInitComponent', () => {
let fixture: ComponentFixture<PasswordResetInitComponent>
let comp: PasswordResetInitComponent
beforeEach(() => {
fixture = TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
imports: [HttpClientTestingModule, ReactiveFormsModule],
declarations: [PasswordResetInitComponent],
}).createComponent(PasswordResetInitComponent)
comp = fixture.componentInstance
Expand Down
1 change: 0 additions & 1 deletion ui/src/app/account/settings/settings.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export class SettingsComponent implements OnInit {
const enabled = this.mfaForm.get('mfaEnabled')!.value
if (enabled) {
const otp = this.mfaForm.get('verificationCode')!.value
console.log('about to set otp on ' + this.mfaSetup)
this.mfaSetup.otp = otp
this.accountService.enableMfa(this.mfaSetup).subscribe((codes: string[] | null) => {
if (codes) {
Expand Down
13 changes: 7 additions & 6 deletions ui/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { TestBed } from '@angular/core/testing'
import { RouterTestingModule } from '@angular/router/testing'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { AppComponent } from './app.component'
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientTestingModule],
declarations: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents()
})

Expand All @@ -18,9 +20,8 @@ describe('AppComponent', () => {
})

it(`should have as title 'ui'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('ui');
});

});
const fixture = TestBed.createComponent(AppComponent)
const app = fixture.componentInstance
expect(app.title).toEqual('ui')
})
})
7 changes: 1 addition & 6 deletions ui/src/app/layout/navbar/navbar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@
>
<fa-icon [icon]="faBars"></fa-icon>
</a>
<div
class="navbar-collapse collapse"
id="navbarResponsive"
[ngbCollapse]="isNavbarCollapsed"
[ngSwitch]="isAuthenticated()"
>
<div class="navbar-collapse collapse" id="navbarResponsive" [ngSwitch]="isAuthenticated()">
<ul class="navbar-nav ml-auto">
<!-- jhipster-needle-add-element-to-menu - JHipster will add new menu items here -->
<li
Expand Down
3 changes: 3 additions & 0 deletions ui/src/app/layout/navbar/navbar.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { By } from '@angular/platform-browser'
import { HasAnyAuthorityDirective } from 'src/app/shared/directive/has-any-authority.directive'
import { HttpResponse } from '@angular/common/http'
import { Member } from 'src/app/member/model/member.model'
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome'
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'

describe('NavbarComponent', () => {
let component: NavbarComponent
Expand Down Expand Up @@ -38,6 +40,7 @@ describe('NavbarComponent', () => {
{ provide: MemberService, useValue: memberServiceSpy },
{ provide: AccountService, useValue: accountServiceSpy },
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents()

fixture = TestBed.createComponent(NavbarComponent)
Expand Down
2 changes: 0 additions & 2 deletions ui/src/app/shared/service/language.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class LanguageService {
// something has to be done
if (currentLang == null) {
// add langCode to url
console.log('adding locale to url')
this.windowLocationService.updateWindowLocation(
this.windowLocationService.getWindowLocationOrigin() +
'/' +
Expand All @@ -45,7 +44,6 @@ export class LanguageService {
)
} else {
// remove current lang code and replace with users lang
console.log('changing locale in url')
this.windowLocationService.updateWindowLocation(
this.windowLocationService.getWindowLocationOrigin() +
this.windowLocationService.getWindowLocationPathname().replace(currentLang, langCode)
Expand Down
2 changes: 2 additions & 0 deletions ui/src/app/user/users.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { LocalizePipe } from '../shared/pipe/localize'
import { EventType } from 'src/app/app.constants'
import { Event } from '../shared/model/event.model'
import { RouterModule } from '@angular/router'
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'
describe('UsersComponent', () => {
let component: UsersComponent
let fixture: ComponentFixture<UsersComponent>
Expand Down Expand Up @@ -54,6 +55,7 @@ describe('UsersComponent', () => {
{ provide: EventService, useValue: eventServiceSpy },
{ provide: AlertService, useValue: alertServiceSpy },
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents()

fixture = TestBed.createComponent(UsersComponent)
Expand Down
43 changes: 24 additions & 19 deletions ui/src/app/user/users.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnDestroy, OnInit } from '@angular/core'
import { Component, NgZone, OnDestroy, OnInit } from '@angular/core'
import { IUser } from './model/user.model'
import { Subscription, filter } from 'rxjs'
import { UserService } from './service/user.service'
Expand Down Expand Up @@ -61,7 +61,8 @@ export class UsersComponent implements OnInit, OnDestroy {
protected accountService: AccountService,
protected activatedRoute: ActivatedRoute,
protected router: Router,
protected eventService: EventService
protected eventService: EventService,
private ngZone: NgZone
) {
this.itemsPerPage = ITEMS_PER_PAGE
this.routeData = this.activatedRoute.data.subscribe((data) => {
Expand Down Expand Up @@ -136,28 +137,32 @@ export class UsersComponent implements OnInit, OnDestroy {
}

loadPage() {
this.router.navigate(['/users'], {
queryParams: {
page: this.page,
size: this.itemsPerPage,
sort: this.sortColumn + ',' + (this.ascending ? 'asc' : 'desc'),
filter: this.submittedSearchTerm ? this.submittedSearchTerm : '',
},
this.ngZone.run(() => {
this.router.navigate(['/users'], {
queryParams: {
page: this.page,
size: this.itemsPerPage,
sort: this.sortColumn + ',' + (this.ascending ? 'asc' : 'desc'),
filter: this.submittedSearchTerm ? this.submittedSearchTerm : '',
},
})
this.loadAll()
})
this.loadAll()
}

clear() {
this.page = 0
this.router.navigate([
'/users',
{
page: this.page,
sort: this.sortColumn + ',' + (this.ascending ? 'asc' : 'desc'),
filter: this.submittedSearchTerm ? this.submittedSearchTerm : '',
},
])
this.loadAll()
this.ngZone.run(() => {
this.router.navigate([
'/users',
{
page: this.page,
sort: this.sortColumn + ',' + (this.ascending ? 'asc' : 'desc'),
filter: this.submittedSearchTerm ? this.submittedSearchTerm : '',
},
])
this.loadAll()
})
}

sort() {
Expand Down

0 comments on commit f22f759

Please sign in to comment.