Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProfileFormComponent>;
let compiled: HTMLElement;

const testrunServiceMock: jasmine.SpyObj<TestRunService> =
jasmine.createSpyObj('testrunServiceMock', [
'fetchQuestionnaireFormat',
'saveDevice',
]);

const mockRiskAssessmentStore: SpyObj<RiskAssessmentStore> =
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();

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -327,13 +327,21 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {
}

close(): Observable<boolean> {
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)
);
Expand Down