diff --git a/modules/ui/src/app/pages/risk-assessment/profile-form/profile-form.component.spec.ts b/modules/ui/src/app/pages/risk-assessment/profile-form/profile-form.component.spec.ts index 8691cbcf9..6cfa020f6 100644 --- a/modules/ui/src/app/pages/risk-assessment/profile-form/profile-form.component.spec.ts +++ b/modules/ui/src/app/pages/risk-assessment/profile-form/profile-form.component.spec.ts @@ -32,28 +32,34 @@ import { import { ProfileStatus } from '../../../model/profile'; import { RiskAssessmentStore } from '../risk-assessment.store'; import { TestRunService } from '../../../services/test-run.service'; -import { provideMockStore } from '@ngrx/store/testing'; import { of } from 'rxjs'; import { MatDialogRef } from '@angular/material/dialog'; import { SimpleDialogComponent } from '../../../components/simple-dialog/simple-dialog.component'; +import SpyObj = jasmine.SpyObj; describe('ProfileFormComponent', () => { let component: ProfileFormComponent; let fixture: ComponentFixture; let compiled: HTMLElement; + const testrunServiceMock: jasmine.SpyObj = jasmine.createSpyObj('testrunServiceMock', [ 'fetchQuestionnaireFormat', 'saveDevice', ]); + const mockRiskAssessmentStore: SpyObj = + jasmine.createSpyObj('RiskAssessmentStore', [ + 'updateSelectedProfile', + 'setIsOpenProfile', + ]); + beforeEach(async () => { await TestBed.configureTestingModule({ imports: [ProfileFormComponent, BrowserAnimationsModule], providers: [ - RiskAssessmentStore, { provide: TestRunService, useValue: testrunServiceMock }, - provideMockStore({}), + { provide: RiskAssessmentStore, useValue: mockRiskAssessmentStore }, ], }).compileComponents(); @@ -378,6 +384,112 @@ describe('ProfileFormComponent', () => { openSpy.calls.reset(); })); }); + + describe('close method', () => { + let storeSpy: jasmine.Spy; + let openDialogSpy: jasmine.Spy; + let deleteCopyEmitSpy: jasmine.Spy; + + beforeEach(() => { + mockRiskAssessmentStore.setIsOpenProfile.calls.reset(); + storeSpy = mockRiskAssessmentStore.setIsOpenProfile; + openDialogSpy = spyOn(component, 'openCloseDialog'); + deleteCopyEmitSpy = spyOn(component.deleteCopy, 'emit'); + }); + + it('should set isOpenProfile to false and return of(true) if profileHasNoChanges is true and not isCopyProfile', done => { + spyOn(component, 'profileHasNoChanges').and.returnValue(true); + component.isCopyProfile = false; + component.profileForm.markAsDirty(); + + component.close().subscribe(result => { + expect(result).toBeTrue(); + expect(storeSpy).toHaveBeenCalledWith(false); + expect(openDialogSpy).not.toHaveBeenCalled(); + expect(deleteCopyEmitSpy).not.toHaveBeenCalled(); + done(); + }); + }); + + it('should set isOpenProfile to false and return of(true) if profileForm is pristine and not isCopyProfile', done => { + spyOn(component, 'profileHasNoChanges').and.returnValue(false); + component.profileForm.markAsPristine(); + component.isCopyProfile = false; + + component.close().subscribe(result => { + expect(result).toBeTrue(); + expect(storeSpy).toHaveBeenCalledWith(false); + expect(openDialogSpy).not.toHaveBeenCalled(); + expect(deleteCopyEmitSpy).not.toHaveBeenCalled(); + done(); + }); + }); + + it('should open dialog if there are changes and not isCopyProfile, and dialog confirms (returns true)', done => { + spyOn(component, 'profileHasNoChanges').and.returnValue(false); + component.profileForm.markAsDirty(); + component.isCopyProfile = false; + openDialogSpy.and.returnValue(of(true)); + + component.close().subscribe(result => { + expect(result).toBeTrue(); + expect(openDialogSpy).toHaveBeenCalled(); + expect(storeSpy).toHaveBeenCalledWith(false); + expect(deleteCopyEmitSpy).not.toHaveBeenCalled(); + done(); + }); + }); + + it('should open dialog if there are changes and not isCopyProfile, and dialog cancels (returns false)', done => { + spyOn(component, 'profileHasNoChanges').and.returnValue(false); + component.profileForm.markAsDirty(); + component.isCopyProfile = false; + openDialogSpy.and.returnValue(of(false)); + + component.close().subscribe(result => { + expect(result).toBeFalse(); + expect(openDialogSpy).toHaveBeenCalled(); + // store.setIsOpenProfile should NOT be called if dialog is cancelled + expect(storeSpy).not.toHaveBeenCalled(); + expect(deleteCopyEmitSpy).not.toHaveBeenCalled(); + done(); + }); + }); + + it('should open dialog if isCopyProfile is true, dialog confirms, and emit deleteCopy', done => { + spyOn(component, 'profileHasNoChanges').and.returnValue(false); + component.profileForm.markAsDirty(); + component.isCopyProfile = true; + component.selectedProfile = { ...PROFILE_MOCK }; + openDialogSpy.and.returnValue(of(true)); + + component.close().subscribe(result => { + expect(result).toBeTrue(); + expect(openDialogSpy).toHaveBeenCalled(); + expect(storeSpy).toHaveBeenCalledWith(false); + expect(deleteCopyEmitSpy).toHaveBeenCalledWith( + component.selectedProfile + ); + done(); + }); + }); + + it('should open dialog if isCopyProfile is true, and dialog cancels', done => { + spyOn(component, 'profileHasNoChanges').and.returnValue(false); + component.profileForm.markAsDirty(); + component.isCopyProfile = true; + component.selectedProfile = { ...PROFILE_MOCK }; + openDialogSpy.and.returnValue(of(false)); + + component.close().subscribe(result => { + expect(result).toBeFalse(); + expect(openDialogSpy).toHaveBeenCalled(); + expect(storeSpy).not.toHaveBeenCalled(); + expect(deleteCopyEmitSpy).not.toHaveBeenCalled(); + done(); + }); + }); + }); }); function fillForm(component: ProfileFormComponent) { diff --git a/modules/ui/src/app/pages/risk-assessment/profile-form/profile-form.component.ts b/modules/ui/src/app/pages/risk-assessment/profile-form/profile-form.component.ts index 8f35abaa4..87d3b86f2 100644 --- a/modules/ui/src/app/pages/risk-assessment/profile-form/profile-form.component.ts +++ b/modules/ui/src/app/pages/risk-assessment/profile-form/profile-form.component.ts @@ -207,7 +207,7 @@ export class ProfileFormComponent implements OnInit, AfterViewInit { } private compareProfiles(profile1: Profile, profile2: Profile) { - if (profile1.name !== profile2.name) { + if (profile1.name !== profile2.name || this.isCopyProfile) { return false; } if ( @@ -327,13 +327,21 @@ export class ProfileFormComponent implements OnInit, AfterViewInit { } close(): Observable { - if (this.profileHasNoChanges() || this.profileForm.pristine) { + if ( + (this.profileHasNoChanges() || this.profileForm.pristine) && + !this.isCopyProfile + ) { this.store.setIsOpenProfile(false); return of(true); } return this.openCloseDialog().pipe( tap(res => { - if (res) this.store.setIsOpenProfile(false); + if (res) { + if (this.isCopyProfile && this.profile) { + this.deleteCopy.emit(this.profile); + } + this.store.setIsOpenProfile(false); + } }), map(res => !!res) );