Skip to content

Commit

Permalink
add delete affiliation unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
auumgn committed Mar 21, 2024
1 parent 96e31cc commit eec5874
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
55 changes: 50 additions & 5 deletions ui/src/app/affiliation/affiliation-delete.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,68 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'

import { AffiliationDeletePopupComponent } from './affiliation-delete.component'
import { AffiliationDeleteDialogComponent, AffiliationDeletePopupComponent } from './affiliation-delete.component'
import { RouterTestingModule } from '@angular/router/testing'
import { AffiliationService } from './service/affiliation.service'
import { EventService } from '../shared/service/event.service'
import { EventType } from '../app.constants'
import { Event } from '../shared/model/event.model'
import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { HttpResponse } from '@angular/common/http'
import { of } from 'rxjs'

describe('AffiliationDeleteComponent', () => {
let component: AffiliationDeletePopupComponent
let fixture: ComponentFixture<AffiliationDeletePopupComponent>
let component: AffiliationDeleteDialogComponent
let fixture: ComponentFixture<AffiliationDeleteDialogComponent>
let affiliationService: jasmine.SpyObj<AffiliationService>
let eventService: jasmine.SpyObj<EventService>

beforeEach(() => {
const affiliationServiceSpy = jasmine.createSpyObj('AffiliationService', ['delete'])
const eventServiceSpy = jasmine.createSpyObj('EventService', ['broadcast'])

TestBed.configureTestingModule({
declarations: [AffiliationDeletePopupComponent],
declarations: [AffiliationDeleteDialogComponent],
imports: [RouterTestingModule],
providers: [
NgbModal,
NgbActiveModal,
{ provide: AffiliationService, useValue: affiliationServiceSpy },
{ provide: EventService, useValue: eventServiceSpy },
],
})
fixture = TestBed.createComponent(AffiliationDeletePopupComponent)
affiliationService = TestBed.inject(AffiliationService) as jasmine.SpyObj<AffiliationService>
eventService = TestBed.inject(EventService) as jasmine.SpyObj<EventService>
fixture = TestBed.createComponent(AffiliationDeleteDialogComponent)
component = fixture.componentInstance
fixture.detectChanges()
})

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

it('should confirm the affiliation being deleted', () => {
affiliationService.delete.and.returnValue(of(true))
component.confirmDelete('id')

expect(eventService.broadcast).toHaveBeenCalledWith(
new Event(EventType.AFFILIATION_LIST_MODIFICATION, 'Deleted an affiliation')
)
})

it('should fail to delete the affiliation', () => {
affiliationService.delete.and.returnValue(of(false))
component.confirmDelete('id')

expect(eventService.broadcast).toHaveBeenCalledWith(
new Event(EventType.AFFILIATION_LIST_MODIFICATION, 'Failed to delete an affiliation')
)
})

it('should not call the assertion service without an id', () => {
affiliationService.delete.and.returnValue(of(false))
component.confirmDelete('')

expect(affiliationService.delete).toHaveBeenCalledTimes(0)
})
})
4 changes: 1 addition & 3 deletions ui/src/app/affiliation/affiliation-delete.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ export class AffiliationDeleteDialogComponent implements OnInit {
}

confirmDelete(id: string | undefined) {
console.log(id)

if (id) {
this.affiliationService.delete(id).subscribe((response) => {
if (response.body.deleted) {
if (response) {
this.eventService.broadcast(new Event(EventType.AFFILIATION_LIST_MODIFICATION, 'Deleted an affiliation'))
this.alertService.broadcast(AlertType.AFFILIATION_DELETED)
} else {
Expand Down
5 changes: 2 additions & 3 deletions ui/src/app/affiliation/affiliation-update.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class AffiliationUpdateComponent implements OnInit {

editForm = this.fb.group(
{
id: [''],
id: [null],
email: ['', [Validators.pattern(EMAIL_REGEXP), Validators.required]],
affiliationSection: ['', [Validators.required]],
departmentName: ['', [Validators.maxLength(4000)]],
Expand Down Expand Up @@ -249,8 +249,7 @@ export class AffiliationUpdateComponent implements OnInit {
save() {
this.isSaving = true
const assertion = this.createFromForm()

if (assertion.id !== undefined && assertion.id != null) {
if (assertion.id !== null && assertion.id !== undefined) {
this.affiliationService.update(assertion).subscribe({
next: () => {
this.onSaveSuccess()
Expand Down
4 changes: 2 additions & 2 deletions ui/src/app/affiliation/service/affiliation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class AffiliationService {
.pipe(map((res: HttpResponse<IAffiliation[]>) => this.convertToAffiliationPage(res)))
}

delete(id: string): Observable<HttpResponse<any>> {
return this.http.delete<any>(`${this.resourceUrl}/${id}`, { observe: 'response' })
delete(id: string): Observable<boolean> {
return this.http.delete<any>(`${this.resourceUrl}/${id}`).pipe(map((res: { deleted: boolean }) => res.deleted))
}

deleteFromOrcid(id: string): Observable<HttpResponse<any>> {
Expand Down

0 comments on commit eec5874

Please sign in to comment.