Skip to content

Commit

Permalink
NAS-125917: PR Update
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKarpov98 committed Oct 12, 2024
1 parent c7fbc7d commit 0c15ea0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { FormGroup, ReactiveFormsModule } from '@angular/forms';
import {
createHostFactory, SpectatorHost, mockProvider,
} from '@ngneat/spectator/jest';
import { TranslateService } from '@ngx-translate/core';
import { of, Subject } from 'rxjs';
import { DialogService } from 'app/modules/dialog/dialog.service';
import { IxSlideInRef } from 'app/modules/forms/ix-forms/components/ix-slide-in/ix-slide-in-ref';
import { FormChangeGuardForSlideInDirective } from './form-change-guard-for-slide-in.directive';

describe('FormChangeGuardForSlideInDirective', () => {
let spectator: SpectatorHost<FormChangeGuardForSlideInDirective<unknown>>;

const createHost = createHostFactory({
component: FormChangeGuardForSlideInDirective,
imports: [ReactiveFormsModule],
providers: [
mockProvider(DialogService, {
confirm: jest.fn(() => of(true)),
}),
mockProvider(TranslateService, {
instant: jest.fn((key: string) => key),
}),
{
provide: IxSlideInRef,
useFactory: () => ({
close: jest.fn(),
slideInClosed$: new Subject<void>(),
}),
},
],
});

beforeEach(() => {
spectator = createHost(`
<form [formGroup]="form" formChangeGuardForSlideIn></form>
`, {
hostProps: {
form: new FormGroup({}),
},
});
});

it('should set formChanged to true when form value changes', () => {
spectator.component.formGroup.markAsPristine();

spectator.component.formGroup.valueChanges.subscribe(() => {
expect(spectator.component.formChanged).toBe(true);
});
});

it('should emit close event if there are no unsaved changes', () => {
spectator.component.formGroup.markAsPristine();

spectator.detectChanges();

spectator.component.closeWithConfirmation().subscribe((shouldClose) => {
expect(shouldClose).toBe(true);
});
});

it('should call confirmation dialog if there are unsaved changes', () => {
const dialogService = spectator.inject(DialogService);

spectator.component.closeWithConfirmation().subscribe(() => {
expect(dialogService.confirm).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { IxSlideInRef } from 'app/modules/forms/ix-forms/components/ix-slide-in/
export class FormChangeGuardForSlideInDirective<T> implements OnInit {
@Input() formGroup: FormGroup;

private formChanged = false;
formChanged = false;

constructor(
private translate: TranslateService,
Expand All @@ -30,6 +30,23 @@ export class FormChangeGuardForSlideInDirective<T> implements OnInit {
this.overrideSlideInClose();
}

closeWithConfirmation(response?: T): Observable<boolean> {
if (!this.formChanged) {
this.emitClose(response);
return of(true);
}

return this.showConfirmDialog().pipe(
switchMap((shouldClose) => {
if (shouldClose) {
this.formChanged = false;
this.emitClose(response);
}
return of(shouldClose);
}),
);
}

private trackFormChanges(): void {
this.formGroup.valueChanges
.pipe(
Expand All @@ -47,23 +64,6 @@ export class FormChangeGuardForSlideInDirective<T> implements OnInit {
.subscribe();
}

private closeWithConfirmation(response?: T): Observable<boolean> {
if (!this.formChanged) {
this.emitClose(response);
return of(true);
}

return this.showConfirmDialog().pipe(
switchMap((shouldClose) => {
if (shouldClose) {
this.formChanged = false;
this.emitClose(response);
}
return of(shouldClose);
}),
);
}

private showConfirmDialog(): Observable<boolean> {
return this.dialogService.confirm({
title: this.translate.instant('Unsaved Changes'),
Expand Down

0 comments on commit 0c15ea0

Please sign in to comment.