From eec5874802ecb359c8a0209d24a23c58b9cefba6 Mon Sep 17 00:00:00 2001 From: andrej romanov <50377758+auumgn@users.noreply.github.com> Date: Thu, 21 Mar 2024 17:43:15 +0200 Subject: [PATCH] add delete affiliation unit tests --- .../affiliation-delete.component.spec.ts | 55 +++++++++++++++++-- .../affiliation-delete.component.ts | 4 +- .../affiliation-update.component.ts | 5 +- .../service/affiliation.service.ts | 4 +- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/ui/src/app/affiliation/affiliation-delete.component.spec.ts b/ui/src/app/affiliation/affiliation-delete.component.spec.ts index c33e6b6c4..53ff733a1 100644 --- a/ui/src/app/affiliation/affiliation-delete.component.spec.ts +++ b/ui/src/app/affiliation/affiliation-delete.component.spec.ts @@ -1,18 +1,38 @@ 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 + let component: AffiliationDeleteDialogComponent + let fixture: ComponentFixture + let affiliationService: jasmine.SpyObj + let eventService: jasmine.SpyObj 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 + eventService = TestBed.inject(EventService) as jasmine.SpyObj + fixture = TestBed.createComponent(AffiliationDeleteDialogComponent) component = fixture.componentInstance fixture.detectChanges() }) @@ -20,4 +40,29 @@ describe('AffiliationDeleteComponent', () => { 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) + }) }) diff --git a/ui/src/app/affiliation/affiliation-delete.component.ts b/ui/src/app/affiliation/affiliation-delete.component.ts index 7d8636e90..8dcf23cfe 100644 --- a/ui/src/app/affiliation/affiliation-delete.component.ts +++ b/ui/src/app/affiliation/affiliation-delete.component.ts @@ -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 { diff --git a/ui/src/app/affiliation/affiliation-update.component.ts b/ui/src/app/affiliation/affiliation-update.component.ts index e8e2a29da..aeedb9d09 100644 --- a/ui/src/app/affiliation/affiliation-update.component.ts +++ b/ui/src/app/affiliation/affiliation-update.component.ts @@ -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)]], @@ -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() diff --git a/ui/src/app/affiliation/service/affiliation.service.ts b/ui/src/app/affiliation/service/affiliation.service.ts index 70c28c84d..8e92daa9e 100644 --- a/ui/src/app/affiliation/service/affiliation.service.ts +++ b/ui/src/app/affiliation/service/affiliation.service.ts @@ -46,8 +46,8 @@ export class AffiliationService { .pipe(map((res: HttpResponse) => this.convertToAffiliationPage(res))) } - delete(id: string): Observable> { - return this.http.delete(`${this.resourceUrl}/${id}`, { observe: 'response' }) + delete(id: string): Observable { + return this.http.delete(`${this.resourceUrl}/${id}`).pipe(map((res: { deleted: boolean }) => res.deleted)) } deleteFromOrcid(id: string): Observable> {