Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
auumgn committed Mar 6, 2024
1 parent e564b23 commit f7d477a
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 25 deletions.
10 changes: 5 additions & 5 deletions ui/src/app/affiliation/affiliations.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h1 id="page-heading" class="mt-3" i18n="@@gatewayApp.assertionServiceAssertion.
<button
id="jh-send-notifications"
class="btn btn-primary float-right send-notifications ml-1"
[routerLink]="['/', 'assertion', { outlets: { popup: 'notifications' } }]"
[routerLink]="['/', 'affiliations', { outlets: { popup: 'notifications' } }]"
>
<fa-icon [icon]="faPaperPlane"></fa-icon>
<span i18n="@@gatewayApp.assertionServiceAssertion.home.permissionNotificationsLabel.string">
Expand Down Expand Up @@ -45,7 +45,7 @@ <h1 id="page-heading" class="mt-3" i18n="@@gatewayApp.assertionServiceAssertion.
<button
id="jh-upload-entities"
class="btn btn-primary float-right jh-upload-entities upload-assertions ml-1"
[routerLink]="['/', 'assertion', { outlets: { popup: 'import' } }]"
[routerLink]="['/', 'affiliations', { outlets: { popup: 'import' } }]"
>
<fa-icon [icon]="faFileImport"></fa-icon>
<span i18n="@@gatewayApp.assertionServiceAssertion.home.uploadLabel.string">
Expand All @@ -55,7 +55,7 @@ <h1 id="page-heading" class="mt-3" i18n="@@gatewayApp.assertionServiceAssertion.
<button
id="jh-create-entity"
class="btn btn-primary float-right jh-create-entity create-assertion ml-1"
[routerLink]="['/assertion/new']"
[routerLink]="['/affiliations/new']"
>
<fa-icon [icon]="faPlus"></fa-icon>
<span i18n="@@gatewayApp.assertionServiceAssertion.home.createLabel.string"> Add affiliation </span>
Expand Down Expand Up @@ -215,15 +215,15 @@ <h1 id="page-heading" class="mt-3" i18n="@@gatewayApp.assertionServiceAssertion.
<div class="btn-group">
<button
type="submit"
[routerLink]="['/affiliation', affiliation.id, 'edit']"
[routerLink]="['/affiliations', affiliation.id, 'edit']"
class="btn btn-primary btn-sm ml-1"
>
<fa-icon [icon]="faPencilAlt"></fa-icon>
<span class="d-none d-md-inline" i18n="@@entity.action.edit.string">Edit</span>
</button>
<button
type="submit"
[routerLink]="['/', 'affiliation', { outlets: { popup: affiliation.id + '/delete' } }]"
[routerLink]="['/', 'affiliations', { outlets: { popup: affiliation.id + '/delete' } }]"
replaceUrl="true"
queryParamsHandling="merge"
class="btn btn-danger btn-sm ml-1"
Expand Down
165 changes: 150 additions & 15 deletions ui/src/app/affiliation/affiliations.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,156 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { AffiliationsComponent } from './affiliations.component';

import { ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { RouterTestingModule } from '@angular/router/testing'
import { EMPTY, of, throwError } from 'rxjs'
import { MemberService } from 'src/app/member/service/member.service'
import { AccountService, LoginService } from 'src/app/account'
import { By } from '@angular/platform-browser'
import { HasAnyAuthorityDirective } from 'src/app/shared/directive/has-any-authority.directive'
import { HttpHeaders, HttpResponse } from '@angular/common/http'
import { Member } from 'src/app/member/model/member.model'
import { AlertService } from '../shared/service/alert.service'
import { EventService } from '../shared/service/event.service'
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'
import { AffiliationsComponent } from './affiliations.component'
import { AffiliationService } from './service/affiliations.service'
import { Affiliation, AffiliationPage } from './model/affiliation.model'
describe('AffiliationsComponent', () => {
let component: AffiliationsComponent;
let fixture: ComponentFixture<AffiliationsComponent>;
let component: AffiliationsComponent
let fixture: ComponentFixture<AffiliationsComponent>
let affiliationService: jasmine.SpyObj<AffiliationService>
let accountService: jasmine.SpyObj<AccountService>
let alertService: jasmine.SpyObj<AlertService>
let eventService: jasmine.SpyObj<EventService>

beforeEach(() => {
const accountServiceSpy = jasmine.createSpyObj('AccountService', [
'getAccountData',
'isAuthenticated',
'hasAnyAuthority',
'isOrganizationOwner',
'getImageUrl',
'getSalesforceId',
])
const affiliationServiceSpy = jasmine.createSpyObj('AffiliationService', [
'query',
'findBySalesForceId',
'sendActivate',
])
const eventServiceSpy = jasmine.createSpyObj('EventService', ['on', 'broadcast'])
const alertServiceSpy = jasmine.createSpyObj('AlertService', ['on', 'broadcast'])

TestBed.configureTestingModule({
declarations: [AffiliationsComponent]
});
fixture = TestBed.createComponent(AffiliationsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
declarations: [AffiliationsComponent, HasAnyAuthorityDirective, LocalizePipe],
imports: [
ReactiveFormsModule,
RouterModule.forRoot([{ path: 'affiliations', component: AffiliationsComponent }]),
FormsModule,
],
providers: [
{ provide: AffiliationService, useValue: affiliationServiceSpy },
{ provide: AccountService, useValue: accountServiceSpy },
{ provide: EventService, useValue: eventServiceSpy },
{ provide: AlertService, useValue: alertServiceSpy },
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents()

fixture = TestBed.createComponent(AffiliationsComponent)
component = fixture.componentInstance
affiliationService = TestBed.inject(AffiliationService) as jasmine.SpyObj<AffiliationService>
accountService = TestBed.inject(AccountService) as jasmine.SpyObj<AccountService>
eventService = TestBed.inject(EventService) as jasmine.SpyObj<EventService>
alertService = TestBed.inject(AlertService) as jasmine.SpyObj<AlertService>

affiliationService.query.and.returnValue(of(new AffiliationPage([new Affiliation('123')], 1)))

accountService.getAccountData.and.returnValue(
of({
activated: true,
authorities: ['ROLE_USER', 'ROLE_ADMIN'],
email: '[email protected]',
firstName: 'name',
langKey: 'en',
lastName: 'surname',
imageUrl: 'url',
salesforceId: 'sfid',
loggedAs: false,
loginAs: 'sfid',
mainContact: false,
mfaEnabled: false,
})
)

accountService.hasAnyAuthority.and.returnValue(true)
eventService.on.and.returnValue(EMPTY)
})

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

it('should call load all on init', fakeAsync(() => {
component.ngOnInit()

expect(affiliationService.query).toHaveBeenCalled()
expect(component.affiliations![0]).toEqual(jasmine.objectContaining({ id: '123' }))
}))

it('should load a page', () => {
component.page = 1
component.loadPage()

expect(affiliationService.query).toHaveBeenCalled()
expect(component.affiliations![0]).toEqual(jasmine.objectContaining({ id: '123' }))
})

it('sort should be id,desc by default', () => {
const result = component.sort()

expect(result).toEqual(['id,desc'])
})

it('direction should be desc and id should be secondary sort column by default', () => {
component.sortColumn = 'name'
const result = component.sort()
expect(result).toEqual(['name,desc', 'id'])
})

it('updating sort column to different value should maintain sort direction', () => {
component.sortColumn = 'name'
let result = component.sort()
expect(result).toEqual(['name,desc', 'id'])

component.updateSort('email')
result = component.sort()
expect(result).toEqual(['email,desc', 'id'])
})

it('updating sort column with same value should flip sort direction', () => {
component.sortColumn = 'name'
let result = component.sort()
expect(result).toEqual(['name,desc', 'id'])

component.updateSort('name')
result = component.sort()
expect(result).toEqual(['name,asc', 'id'])
})

it('clear should reset page to zero', () => {
component.page = 10
component.clear()
expect(component.page).toEqual(0)
})

it('reset search should clear search term', () => {
component.searchTerm = 'what the user typed'
component.submittedSearchTerm = 'what the user typed'
component.resetSearch()
expect(component.searchTerm).toEqual('')
expect(component.submittedSearchTerm).toEqual('')
})
})
18 changes: 13 additions & 5 deletions ui/src/app/affiliation/affiliations.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export class AffiliationsComponent implements OnInit, OnDestroy {
links: any
totalItems: any
itemsPerPage: any
page: any
sortColumn: any
page = 1
sortColumn = 'id'
ascending: any
orcidBaseUrl: string | undefined = ORCID_BASE_URL
itemCount: string | undefined
Expand Down Expand Up @@ -126,7 +126,7 @@ export class AffiliationsComponent implements OnInit, OnDestroy {
}

loadPage() {
this.router.navigate(['/assertion'], {
this.router.navigate(['/affiliations'], {
queryParams: {
page: this.page,
size: this.itemsPerPage,
Expand All @@ -140,7 +140,7 @@ export class AffiliationsComponent implements OnInit, OnDestroy {
clear() {
this.page = 0
this.router.navigate([
'/assertion',
'/affiliations',
{
page: this.page,
sort: this.sortColumn + ',' + (this.ascending ? 'asc' : 'desc'),
Expand Down Expand Up @@ -221,6 +221,14 @@ export class AffiliationsComponent implements OnInit, OnDestroy {
}

ngOnDestroy() {
this.routeData!.unsubscribe()
if (this.notificationSubscription) {
this.notificationSubscription.unsubscribe()
}
if (this.eventSubscriber) {
this.eventSubscriber.unsubscribe()
}
if (this.importEventSubscriber) {
this.importEventSubscriber.unsubscribe()
}
}
}

0 comments on commit f7d477a

Please sign in to comment.