mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ACS-8834] KR page on Auth token expire page get cancel popup with continue refresh (#10250)
This commit is contained in:
@@ -20,15 +20,47 @@ import { MatDialog, MatDialogModule, MatDialogRef } from '@angular/material/dial
|
||||
import { Observable, Subject } from 'rxjs';
|
||||
import { UnsavedChangesDialogComponent } from './unsaved-changes-dialog.component';
|
||||
import { UnsavedChangesGuard } from './unsaved-changes.guard';
|
||||
import { AuthenticationService, AuthGuardService, NoopAuthModule } from '@alfresco/adf-core';
|
||||
|
||||
describe('UnsavedChangesGuard', () => {
|
||||
let guard: UnsavedChangesGuard;
|
||||
let authService: AuthenticationService;
|
||||
let authGuardService: AuthGuardService;
|
||||
|
||||
const expectGuardToBe = (condition: boolean, done: DoneFn, checkUnsaved?: boolean) => {
|
||||
(guard.canDeactivate() as Observable<boolean>).subscribe((allowed) => {
|
||||
if (checkUnsaved) {
|
||||
allowed = guard.unsaved;
|
||||
}
|
||||
condition ? expect(allowed).toBeTrue() : expect(allowed).toBeFalse();
|
||||
done();
|
||||
});
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [MatDialogModule]
|
||||
imports: [MatDialogModule, NoopAuthModule],
|
||||
providers: [
|
||||
{
|
||||
provide: AuthenticationService,
|
||||
useValue: {
|
||||
isLoggedIn: () => true
|
||||
}
|
||||
},
|
||||
{
|
||||
provide: AuthGuardService,
|
||||
useValue: {
|
||||
get withCredentials() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
guard = TestBed.inject(UnsavedChangesGuard);
|
||||
authService = TestBed.inject(AuthenticationService);
|
||||
TestBed.inject(AuthGuardService);
|
||||
authGuardService = TestBed.inject(AuthGuardService);
|
||||
});
|
||||
|
||||
describe('canDeactivate', () => {
|
||||
@@ -43,63 +75,73 @@ describe('UnsavedChangesGuard', () => {
|
||||
} as MatDialogRef<UnsavedChangesDialogComponent>);
|
||||
});
|
||||
|
||||
it('should return true if unsaved is set to false', () => {
|
||||
describe('with Auth', () => {
|
||||
beforeEach(() => {
|
||||
spyOn(authService, 'isLoggedIn').and.returnValue(true);
|
||||
});
|
||||
|
||||
it('should return true if unsaved is set to false and user is logged in', () => {
|
||||
guard.unsaved = false;
|
||||
expect(guard.canDeactivate()).toBeTrue();
|
||||
});
|
||||
|
||||
it('should return true if unsaved was not set', () => {
|
||||
it('should return true if unsaved was not set and user is logged in', () => {
|
||||
expect(guard.canDeactivate()).toBeTrue();
|
||||
});
|
||||
|
||||
it('should return true when unsaved is set to true and result of dialog is true', (done) => {
|
||||
guard.unsaved = true;
|
||||
|
||||
(guard.canDeactivate() as Observable<boolean>).subscribe((allowed) => {
|
||||
expect(allowed).toBeTrue();
|
||||
done();
|
||||
});
|
||||
expectGuardToBe(true, done);
|
||||
afterClosed$.next(true);
|
||||
});
|
||||
|
||||
it('should return false when unsaved is set to true and result of dialog is false', (done) => {
|
||||
guard.unsaved = true;
|
||||
|
||||
(guard.canDeactivate() as Observable<boolean>).subscribe((allowed) => {
|
||||
expect(allowed).toBeFalse();
|
||||
done();
|
||||
});
|
||||
afterClosed$.next(false);
|
||||
});
|
||||
|
||||
it('should keep unsaved set to true when unsaved was to true and result of dialog is false', (done) => {
|
||||
guard.unsaved = true;
|
||||
|
||||
(guard.canDeactivate() as Observable<boolean>).subscribe(() => {
|
||||
expect(guard.unsaved).toBeTrue();
|
||||
done();
|
||||
});
|
||||
afterClosed$.next(false);
|
||||
});
|
||||
|
||||
it('should set unsaved to false when unsaved is set to true and result of dialog is true', (done) => {
|
||||
guard.unsaved = true;
|
||||
|
||||
(guard.canDeactivate() as Observable<boolean>).subscribe(() => {
|
||||
expect(guard.unsaved).toBeFalse();
|
||||
done();
|
||||
});
|
||||
expectGuardToBe(false, done, true);
|
||||
afterClosed$.next(true);
|
||||
});
|
||||
|
||||
it('should return false if afterClosed subject value was undefined', (done) => {
|
||||
guard.unsaved = true;
|
||||
|
||||
(guard.canDeactivate() as Observable<boolean>).subscribe((result) => {
|
||||
expect(result).toBeFalse();
|
||||
done();
|
||||
});
|
||||
expectGuardToBe(false, done);
|
||||
afterClosed$.next(undefined);
|
||||
});
|
||||
|
||||
it('should return false when unsaved is set to true and result of dialog is false', (done) => {
|
||||
guard.unsaved = true;
|
||||
|
||||
expectGuardToBe(false, done);
|
||||
afterClosed$.next(false);
|
||||
});
|
||||
|
||||
it('should keep unsaved set to true when unsaved was to true and result of dialog is false', (done) => {
|
||||
guard.unsaved = true;
|
||||
|
||||
expectGuardToBe(true, done, true);
|
||||
afterClosed$.next(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Without auth', () => {
|
||||
beforeEach(() => {
|
||||
spyOn(authService, 'isLoggedIn').and.returnValue(false);
|
||||
});
|
||||
|
||||
it('should return true when user is logged out of authenticationService and authGuardBaseService.withCredentials returns false', (done) => {
|
||||
expectGuardToBe(true, done);
|
||||
afterClosed$.next(true);
|
||||
});
|
||||
|
||||
it('should return false when authGuardBaseService.withCredentials returns true', (done) => {
|
||||
guard.unsaved = true;
|
||||
spyOnProperty(authGuardService, 'withCredentials').and.returnValue(true);
|
||||
|
||||
expectGuardToBe(false, done);
|
||||
afterClosed$.next(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -17,11 +17,12 @@
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanDeactivate } from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { UnsavedChangesDialogComponent } from './unsaved-changes-dialog.component';
|
||||
import { map, tap } from 'rxjs/operators';
|
||||
import { UnsavedChangesDialogData } from './unsaved-changes-dialog.model';
|
||||
import { AuthenticationService, AuthGuardService } from '../../auth';
|
||||
|
||||
/**
|
||||
* Guard responsible for protecting leaving page with unsaved changes.
|
||||
@@ -33,7 +34,7 @@ export class UnsavedChangesGuard implements CanDeactivate<any> {
|
||||
unsaved = false;
|
||||
data: UnsavedChangesDialogData;
|
||||
|
||||
constructor(private dialog: MatDialog) {}
|
||||
constructor(private dialog: MatDialog, private authenticationService: AuthenticationService, private authGuardBaseService: AuthGuardService) {}
|
||||
|
||||
/**
|
||||
* Allows to deactivate route when there is no unsaved changes, otherwise displays dialog to confirm discarding changes.
|
||||
@@ -41,6 +42,10 @@ export class UnsavedChangesGuard implements CanDeactivate<any> {
|
||||
* @returns boolean | Observable<boolean> true when there is no unsaved changes or changes can be discarded, false otherwise.
|
||||
*/
|
||||
canDeactivate(): boolean | Observable<boolean> {
|
||||
if (!this.authenticationService.isLoggedIn() && !this.authGuardBaseService.withCredentials) {
|
||||
return of(true);
|
||||
}
|
||||
|
||||
return this.unsaved
|
||||
? this.dialog
|
||||
.open<UnsavedChangesDialogComponent>(UnsavedChangesDialogComponent, {
|
||||
|
Reference in New Issue
Block a user