[AAE-2041] [APS-1] [ADF] download process audit log not working (#5568)

* [AAE-2041] [APS-1] [ADF] download process audit log not working

* * updates JS API

* * clean up
This commit is contained in:
dhrn
2020-03-30 14:06:21 +05:30
committed by GitHub
parent 8f3f5b3879
commit 7195a384e8
4 changed files with 59 additions and 48 deletions

View File

@@ -17,32 +17,31 @@
import { Component } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { of, throwError } from 'rxjs';
import { ProcessService } from './../services/process.service';
import { ProcessAuditDirective } from './process-audit.directive';
import { setupTestBed, CoreModule } from '@alfresco/adf-core';
declare let jasmine: any;
import { setupTestBed, CoreModule, DownloadService } from '@alfresco/adf-core';
@Component({
selector: 'adf-basic-button',
template: `
<button id="auditButton"
adf-process-audit
[process-id]="currentProcessId"
[process-id]="1234"
[download]="download"
[fileName]="fileName"
[format]="format"
(error)="onAuditError($event)"
(clicked)="onAuditClick($event)">My button
</button>`
})
class BasicButtonComponent {
download: boolean = false;
fileName: string;
format: string;
onAuditClick() {}
onAuditError() {}
}
describe('ProcessAuditDirective', () => {
@@ -68,6 +67,7 @@ describe('ProcessAuditDirective', () => {
'dCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G');
return new Blob([pdfData], {type: 'application/pdf'});
}
const blob = createFakePdfBlob();
setupTestBed({
imports: [
@@ -84,18 +84,13 @@ describe('ProcessAuditDirective', () => {
fixture = TestBed.createComponent(BasicButtonComponent);
component = fixture.componentInstance;
service = TestBed.get(ProcessService);
jasmine.Ajax.install();
});
afterEach(() => {
jasmine.Ajax.uninstall();
});
afterEach(() => fixture.destroy());
it('should fetch the pdf Blob when the format is pdf', fakeAsync(() => {
component.fileName = 'FakeAuditName';
component.format = 'pdf';
const blob = createFakePdfBlob();
spyOn(service, 'fetchProcessAuditPdfById').and.returnValue(of(blob));
spyOn(component, 'onAuditClick').and.callThrough();
@@ -109,7 +104,40 @@ describe('ProcessAuditDirective', () => {
});
button.click();
}));
it('should download the audit in PDF format (default mode)', fakeAsync(() => {
component.fileName = 'FakeAuditName';
component.download = true;
const downloadService = TestBed.get(DownloadService);
spyOn(service, 'fetchProcessAuditPdfById').and.returnValue(of(blob));
spyOn(downloadService, 'downloadBlob').and.stub();
fixture.detectChanges();
const button = fixture.nativeElement.querySelector('#auditButton');
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(downloadService.downloadBlob).toHaveBeenCalledWith(blob, 'FakeAuditName.pdf');
});
button.click();
}));
it('should throw error if download audit failed', fakeAsync(() => {
const expectedError = 'Failed to get audit';
spyOn(service, 'fetchProcessAuditPdfById').and.returnValue(throwError(expectedError));
spyOn(component, 'onAuditError').and.stub();
fixture.detectChanges();
const button = fixture.nativeElement.querySelector('#auditButton');
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(component.onAuditError).toHaveBeenCalledWith(expectedError);
});
button.click();
}));
it('should fetch the json info when the format is json', fakeAsync(() => {
@@ -145,13 +173,11 @@ describe('ProcessAuditDirective', () => {
});
button.click();
}));
it('should fetch the pdf Blob as default when the format is UNKNOW', fakeAsync(() => {
component.fileName = 'FakeAuditName';
component.format = 'fakeFormat';
const blob = createFakePdfBlob();
spyOn(service, 'fetchProcessAuditPdfById').and.returnValue(of(blob));
spyOn(component, 'onAuditClick').and.callThrough();
@@ -165,6 +191,5 @@ describe('ProcessAuditDirective', () => {
});
button.click();
}));
});

View File

@@ -17,7 +17,7 @@
/* tslint:disable:no-input-rename */
import { ContentService } from '@alfresco/adf-core';
import { DownloadService } from '@alfresco/adf-core';
import { Directive, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import { ProcessService } from './../services/process.service';
@@ -57,14 +57,11 @@ export class ProcessAuditDirective implements OnChanges {
@Output()
error: EventEmitter<any> = new EventEmitter<any>();
public audit: any;
/**
*
* @param translateService
* @param downloadService
* @param processListService
*/
constructor(private contentService: ContentService,
constructor(private downloadService: DownloadService,
private processListService: ProcessService) {
}
@@ -75,10 +72,7 @@ export class ProcessAuditDirective implements OnChanges {
}
isValidType() {
if (this.format && (this.isJsonFormat() || this.isPdfFormat())) {
return true;
}
return false;
return this.format && (this.isJsonFormat() || this.isPdfFormat());
}
setDefaultFormatType(): void {
@@ -92,24 +86,16 @@ export class ProcessAuditDirective implements OnChanges {
if (this.isPdfFormat()) {
this.processListService.fetchProcessAuditPdfById(this.processId).subscribe(
(blob: Blob) => {
this.audit = blob;
if (this.download) {
this.contentService.downloadBlob(this.audit, this.fileName + '.pdf');
this.downloadService.downloadBlob(blob, this.fileName + '.pdf');
}
this.clicked.emit({ format: this.format, value: this.audit, fileName: this.fileName });
this.clicked.emit({ format: this.format, value: blob, fileName: this.fileName });
},
(err) => {
this.error.emit(err);
});
(err) => this.error.emit(err));
} else {
this.processListService.fetchProcessAuditJsonById(this.processId).subscribe(
(res) => {
this.audit = res;
this.clicked.emit({ format: this.format, value: this.audit, fileName: this.fileName });
},
(err) => {
this.error.emit(err);
});
(res) => this.clicked.emit({ format: this.format, value: res, fileName: this.fileName }),
(err) => this.error.emit(err));
}
}