[ADF-4364] Fix Back button action on Viewer Component (#4613)

* [ADF-3364] Fix Back button action on Viewer Component

* [ADF-4364] Fix Module providers

* [ADF-4364] Add unit tests

* [ADF-4364] Fix unit tests

* [ADF-4364] Fix lazy loading module of Viewer component
This commit is contained in:
davidcanonieto
2019-04-18 11:34:16 +02:00
committed by Eugenio Romano
parent 8395b0baa5
commit 181cee72d6
7 changed files with 186 additions and 10 deletions

View File

@@ -276,23 +276,21 @@ describe('Test PdfViewer component', () => {
document.body.removeChild(elementBlobTestComponent);
});
it('should Canvas be present', (done) => {
it('should Canvas be present', () => {
fixtureBlobTestComponent.detectChanges();
fixtureBlobTestComponent.whenStable().then(() => {
expect(elementBlobTestComponent.querySelector('.adf-pdfViewer')).not.toBeNull();
expect(elementBlobTestComponent.querySelector('.adf-viewer-pdf-viewer')).not.toBeNull();
done();
});
});
it('should Next an Previous Buttons be present', (done) => {
it('should Next an Previous Buttons be present', () => {
fixtureBlobTestComponent.detectChanges();
fixtureBlobTestComponent.whenStable().then(() => {
expect(elementBlobTestComponent.querySelector('#viewer-previous-page-button')).not.toBeNull();
expect(elementBlobTestComponent.querySelector('#viewer-next-page-button')).not.toBeNull();
done();
});
});

View File

@@ -18,18 +18,21 @@
import { Location } from '@angular/common';
import { SpyLocation } from '@angular/common/testing';
import { Component } from '@angular/core';
import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { ComponentFixture, TestBed, fakeAsync, tick, async } from '@angular/core/testing';
import { AlfrescoApiService, RenditionsService } from '../../services';
import { CoreModule } from '../../core.module';
import { throwError } from 'rxjs';
import { throwError, Observable } from 'rxjs';
import { EventMock } from '../../mock/event.mock';
import { RenderingQueueServices } from '../services/rendering-queue.services';
import { ViewerComponent } from './viewer.component';
import { setupTestBed } from '../../testing/setupTestBed';
import { AlfrescoApiServiceMock } from '../../mock/alfresco-api.service.mock';
import { NodeEntry } from '@alfresco/js-api';
import { PreviousRouteService } from 'core/services/previous-route.service';
import { RouterTestingModule } from '@angular/router/testing';
import { Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'adf-viewer-container-toolbar',
@@ -120,16 +123,28 @@ class ViewerWithCustomOpenWithComponent {
class ViewerWithCustomMoreActionsComponent {
}
class MockRouter {
navigate = jasmine.createSpy('navigate');
firstUrl = new NavigationEnd(0, '/files', '/files');
events = new Observable((observer) => {
observer.next(this.firstUrl);
observer.complete();
});
}
describe('ViewerComponent', () => {
let component: ViewerComponent;
let fixture: ComponentFixture<ViewerComponent>;
let alfrescoApiService: AlfrescoApiService;
let previousRouteService: PreviousRouteService;
let router: Router;
let element: HTMLElement;
setupTestBed({
imports: [
CoreModule.forRoot()
CoreModule.forRoot(),
RouterTestingModule
],
declarations: [
ViewerWithCustomToolbarComponent,
@@ -147,7 +162,9 @@ describe('ViewerComponent', () => {
}
}
},
{ provide: Router, useClass: MockRouter },
RenderingQueueServices,
PreviousRouteService,
{ provide: Location, useClass: SpyLocation }
]
});
@@ -158,6 +175,8 @@ describe('ViewerComponent', () => {
component = fixture.componentInstance;
alfrescoApiService = TestBed.get(AlfrescoApiService);
previousRouteService = TestBed.get(PreviousRouteService);
router = TestBed.get(Router);
});
describe('Extension Type Test', () => {
@@ -623,6 +642,47 @@ describe('ViewerComponent', () => {
});
});
it('should render close viewer button if it is not a shared link', (done) => {
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(element.querySelector('[data-automation-id="adf-toolbar-back"]')).toBeDefined();
expect(element.querySelector('[data-automation-id="adf-toolbar-back"]')).not.toBeNull();
done();
});
});
it('should go back when back button is clicked', async(() => {
spyOn(previousRouteService, 'getPreviousUrl').and.returnValue('home');
const button: HTMLButtonElement = element.querySelector('[data-automation-id="adf-toolbar-back"]') as HTMLButtonElement;
button.click();
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(router.navigate).toHaveBeenCalled();
});
}));
it('should render close viewer button if it is a shared link', (done) => {
spyOn(alfrescoApiService.getInstance().core.sharedlinksApi, 'getSharedLink')
.and.returnValue(Promise.reject({}));
component.sharedLinkId = 'the-Shared-Link-id';
component.urlFile = null;
component.mimeType = null;
component.ngOnChanges(null);
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(element.querySelector('[data-automation-id="adf-toolbar-back"]')).toBeNull();
done();
});
});
});
describe('View', () => {

View File

@@ -25,6 +25,7 @@ import { RenditionPaging, SharedLinkEntry, Node, RenditionEntry, NodeEntry } fro
import { BaseEvent } from '../../events';
import { AlfrescoApiService } from '../../services/alfresco-api.service';
import { LogService } from '../../services/log.service';
import { PreviousRouteService } from '../../services/previous-route.service';
import { ViewerMoreActionsComponent } from './viewer-more-actions.component';
import { ViewerOpenWithComponent } from './viewer-open-with.component';
import { ViewerSidebarComponent } from './viewer-sidebar.component';
@@ -32,6 +33,7 @@ import { ViewerToolbarComponent } from './viewer-toolbar.component';
import { Subscription } from 'rxjs';
import { ViewUtilService } from '../services/view-util.service';
import { AppExtensionService, ViewerExtensionRef } from '@alfresco/adf-extensions';
import { Router } from '@angular/router';
@Component({
selector: 'adf-viewer',
@@ -239,7 +241,9 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
private logService: LogService,
private location: Location,
private extensionService: AppExtensionService,
private el: ElementRef) {
private el: ElementRef,
private router: Router,
private previousRouteService: PreviousRouteService) {
}
isSourceDefined(): boolean {
@@ -304,6 +308,7 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
}
);
} else if (this.sharedLinkId) {
this.allowGoBack = false;
this.apiService.sharedLinksApi.getSharedLink(this.sharedLinkId).then(
(sharedLinkEntry: SharedLinkEntry) => {
@@ -479,7 +484,14 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
this.goBack.next(event);
if (!event.defaultPrevented) {
this.location.back();
const previousUrl = this.previousRouteService.getPreviousUrl();
if (previousUrl && previousUrl.includes('login') || window.history.length <= 2) {
this.router.navigate([{outlets: {overlay: null, primary: ['home']}}]);
} else {
this.location.back();
}
}
}
}