mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-06-02 17:34:51 +00:00
[ACA-2083] reset router cache on login/logout (#867)
* reset router cache on login/logout * Update src/app/app.routes.strategy.ts Co-Authored-By: DenysVuika <denys.vuika@gmail.com> * merge suggestion * [ACA-2083] reset content filters on logout * [ACA-2083] unit tests
This commit is contained in:
parent
d7db57a76a
commit
465646e87e
88
src/app/app.routes.strategy.spec.ts
Normal file
88
src/app/app.routes.strategy.spec.ts
Normal file
@ -0,0 +1,88 @@
|
||||
/*!
|
||||
* @license
|
||||
* Alfresco Example Content Application
|
||||
*
|
||||
* Copyright (C) 2005 - 2018 Alfresco Software Limited
|
||||
*
|
||||
* This file is part of the Alfresco Example Content Application.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { AppRouteReuseStrategy } from './app.routes.strategy';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AppTestingModule } from './testing/app-testing.module';
|
||||
|
||||
describe('AppRouteReuseStrategy', () => {
|
||||
let appRouteReuse: AppRouteReuseStrategy;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [AppTestingModule],
|
||||
providers: [AppRouteReuseStrategy]
|
||||
});
|
||||
|
||||
appRouteReuse = TestBed.get(AppRouteReuseStrategy);
|
||||
});
|
||||
|
||||
it('should allow detach if route is configured to be reused', () => {
|
||||
const route = <any>{
|
||||
routeConfig: {
|
||||
data: {
|
||||
reuse: true
|
||||
},
|
||||
path: 'tested-path'
|
||||
}
|
||||
};
|
||||
expect(appRouteReuse.shouldDetach(<any>route)).toBe(true);
|
||||
});
|
||||
|
||||
it('should store on routeCache', () => {
|
||||
const route = <any>{
|
||||
url: [],
|
||||
routeConfig: {
|
||||
data: {
|
||||
reuse: true
|
||||
},
|
||||
path: 'tested-path',
|
||||
component: {}
|
||||
},
|
||||
firstChild: null,
|
||||
children: []
|
||||
};
|
||||
appRouteReuse.store(route, { route: {} });
|
||||
expect(appRouteReuse.shouldAttach(<any>route)).toBe(true);
|
||||
});
|
||||
|
||||
it('should clear routeCache on resetCache', () => {
|
||||
const route = <any>{
|
||||
url: [],
|
||||
routeConfig: {
|
||||
data: {
|
||||
reuse: true
|
||||
},
|
||||
path: 'tested-path',
|
||||
component: {}
|
||||
},
|
||||
firstChild: null,
|
||||
children: []
|
||||
};
|
||||
appRouteReuse.store(route, { route: {} });
|
||||
appRouteReuse.resetCache();
|
||||
expect(appRouteReuse.shouldAttach(<any>route)).toBe(false);
|
||||
});
|
||||
});
|
@ -28,6 +28,7 @@ import {
|
||||
DetachedRouteHandle,
|
||||
ActivatedRouteSnapshot
|
||||
} from '@angular/router';
|
||||
import { ComponentRef } from '@angular/core';
|
||||
|
||||
interface RouteData {
|
||||
reuse: boolean;
|
||||
@ -41,6 +42,23 @@ interface RouteInfo {
|
||||
export class AppRouteReuseStrategy implements RouteReuseStrategy {
|
||||
private routeCache = new Map<string, RouteInfo>();
|
||||
|
||||
resetCache() {
|
||||
this.routeCache.forEach(value => {
|
||||
this.deactivateComponent(value.handle);
|
||||
});
|
||||
this.routeCache.clear();
|
||||
}
|
||||
|
||||
private deactivateComponent(handle: DetachedRouteHandle): void {
|
||||
if (!handle) {
|
||||
return;
|
||||
}
|
||||
const componentRef: ComponentRef<any> = handle['componentRef'];
|
||||
if (componentRef) {
|
||||
componentRef.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
shouldReuseRoute(
|
||||
future: ActivatedRouteSnapshot,
|
||||
curr: ActivatedRouteSnapshot
|
||||
|
@ -143,6 +143,7 @@ export class SearchInputComponent implements OnInit, OnDestroy {
|
||||
ngOnDestroy(): void {
|
||||
this.onDestroy$.next(true);
|
||||
this.onDestroy$.complete();
|
||||
this.removeContentFilters();
|
||||
}
|
||||
|
||||
onMenuOpened() {
|
||||
|
79
src/app/services/app.service.spec.ts
Normal file
79
src/app/services/app.service.spec.ts
Normal file
@ -0,0 +1,79 @@
|
||||
/*!
|
||||
* @license
|
||||
* Alfresco Example Content Application
|
||||
*
|
||||
* Copyright (C) 2005 - 2018 Alfresco Software Limited
|
||||
*
|
||||
* This file is part of the Alfresco Example Content Application.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { AppService } from './app.service';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AppTestingModule } from '../testing/app-testing.module';
|
||||
import { AuthenticationService } from '@alfresco/adf-core';
|
||||
import { AppRouteReuseStrategy } from '../app.routes.strategy';
|
||||
import { Subject } from 'rxjs';
|
||||
|
||||
describe('AppService', () => {
|
||||
let service: AppService;
|
||||
let auth: AuthenticationService;
|
||||
let routeReuse: AppRouteReuseStrategy;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [AppTestingModule],
|
||||
providers: [
|
||||
AppRouteReuseStrategy,
|
||||
{
|
||||
provide: AuthenticationService,
|
||||
useValue: {
|
||||
onLogin: new Subject<any>(),
|
||||
onLogout: new Subject<any>(),
|
||||
isLoggedIn: () => false
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
routeReuse = TestBed.get(AppRouteReuseStrategy);
|
||||
auth = TestBed.get(AuthenticationService);
|
||||
spyOn(routeReuse, 'resetCache').and.stub();
|
||||
|
||||
service = new AppService(auth, routeReuse);
|
||||
});
|
||||
|
||||
it('should reset route cache on login', async () => {
|
||||
auth.onLogin.next();
|
||||
await expect(routeReuse.resetCache).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should reset route cache on logout', async () => {
|
||||
auth.onLogout.next();
|
||||
await expect(routeReuse.resetCache).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should be ready after login', async () => {
|
||||
let isReady = false;
|
||||
service.ready$.subscribe(value => {
|
||||
isReady = value;
|
||||
});
|
||||
auth.onLogin.next();
|
||||
await expect(<any>isReady).toEqual(true);
|
||||
});
|
||||
});
|
@ -23,9 +23,11 @@
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Injectable, Inject } from '@angular/core';
|
||||
import { AuthenticationService } from '@alfresco/adf-core';
|
||||
import { Observable, BehaviorSubject } from 'rxjs';
|
||||
import { AppRouteReuseStrategy } from '../app.routes.strategy';
|
||||
import { RouteReuseStrategy } from '@angular/router';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@ -34,12 +36,20 @@ export class AppService {
|
||||
private ready: BehaviorSubject<boolean>;
|
||||
ready$: Observable<boolean>;
|
||||
|
||||
constructor(auth: AuthenticationService) {
|
||||
constructor(
|
||||
auth: AuthenticationService,
|
||||
@Inject(RouteReuseStrategy) routeStrategy: AppRouteReuseStrategy
|
||||
) {
|
||||
this.ready = new BehaviorSubject(auth.isLoggedIn());
|
||||
this.ready$ = this.ready.asObservable();
|
||||
|
||||
auth.onLogin.subscribe(e => {
|
||||
auth.onLogin.subscribe(() => {
|
||||
routeStrategy.resetCache();
|
||||
this.ready.next(true);
|
||||
});
|
||||
|
||||
auth.onLogout.subscribe(() => {
|
||||
routeStrategy.resetCache();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user