fix random test failing part 2 (#3395)

* fix random failing test core search/comment/auth/user

* fix node delete directive

* fix lint issues

* node restore fix

* fix comment test

* remove fdescribe

* fix tests and tslint

* fix upload test

* unsubscribe success event task test

* copy comment object during test

* use the data pipe performance improvement and standard usage

* uncomment random test

* fix comment date random failing test

* disposable unsubscribe

* fix start process

* remove fdescribe

* change start process test and remove commented code

* fix error event check double click

* clone object form test

* refactor date time test

* fix service mock

* fix test dropdown and context

* git hook lint

* fix language test

* unsubscribe documentlist event test

* fix disposable error

* fix console log service error document list

* unusbscribe search test

* clear input field

* remove wrong test
This commit is contained in:
Eugenio Romano
2018-05-29 11:18:17 +02:00
committed by Denys Vuika
parent 22006395c7
commit eb0f91c5db
43 changed files with 4475 additions and 4332 deletions

View File

@@ -16,7 +16,7 @@
*/
import { Component, DebugElement, ViewChild } from '@angular/core';
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { AlfrescoApiService } from '../services/alfresco-api.service';
import { NodeDeleteDirective } from './node-delete.directive';
@@ -38,7 +38,8 @@ class TestComponent {
@ViewChild(NodeDeleteDirective)
deleteDirective: NodeDeleteDirective;
onDelete(event) {}
onDelete(event) {
}
}
@Component({
@@ -89,6 +90,9 @@ describe('NodeDeleteDirective', () => {
let componentWithPermanentDelete: TestDeletePermanentComponent;
let alfrescoApi: AlfrescoApiService;
let nodeApi;
let deleteNodeSpy: any;
let purgeDeletedNodeSpy: any;
let disposableDelete: any;
setupTestBed({
imports: [
@@ -105,7 +109,12 @@ describe('NodeDeleteDirective', () => {
]
});
beforeEach(async(() => {
beforeEach(() => {
alfrescoApi = TestBed.get(AlfrescoApiService);
nodeApi = alfrescoApi.nodesApi;
deleteNodeSpy = spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.resolve());
purgeDeletedNodeSpy = spyOn(nodeApi, 'purgeDeletedNode').and.returnValue(Promise.resolve());
fixture = TestBed.createComponent(TestComponent);
fixtureWithPermissions = TestBed.createComponent(TestWithPermissionsComponent);
fixtureWithPermanentComponent = TestBed.createComponent(TestDeletePermanentComponent);
@@ -117,33 +126,30 @@ describe('NodeDeleteDirective', () => {
element = fixture.debugElement.query(By.directive(NodeDeleteDirective));
elementWithPermissions = fixtureWithPermissions.debugElement.query(By.directive(NodeDeleteDirective));
elementWithPermanentDelete = fixtureWithPermanentComponent.debugElement.query(By.directive(NodeDeleteDirective));
alfrescoApi = TestBed.get(AlfrescoApiService);
nodeApi = alfrescoApi.getInstance().nodes;
}));
});
afterEach(() => {
if (disposableDelete) {
disposableDelete.unsubscribe();
}
fixture.destroy();
});
describe('Delete', () => {
it('should do nothing if selection is empty', () => {
spyOn(nodeApi, 'deleteNode');
component.selection = [];
fixture.detectChanges();
element.nativeElement.click();
expect(nodeApi.deleteNode).not.toHaveBeenCalled();
expect(deleteNodeSpy).not.toHaveBeenCalled();
});
it('should process node successfully', (done) => {
spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.resolve());
component.selection = <any> [{ entry: { id: '1', name: 'name1' } }];
component.deleteDirective.delete.subscribe((message) => {
disposableDelete = component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.SINGULAR'
);
@@ -158,11 +164,11 @@ describe('NodeDeleteDirective', () => {
});
it('should notify failed node deletion', (done) => {
spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.reject('error'));
deleteNodeSpy.and.returnValue(Promise.reject('error'));
component.selection = [{ entry: { id: '1', name: 'name1' } }];
component.deleteDirective.delete.subscribe((message) => {
disposableDelete = component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.ERROR_SINGULAR'
);
@@ -177,14 +183,12 @@ describe('NodeDeleteDirective', () => {
});
it('should notify nodes deletion', (done) => {
spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.resolve());
component.selection = [
{ entry: { id: '1', name: 'name1' } },
{ entry: { id: '2', name: 'name2' } }
];
component.deleteDirective.delete.subscribe((message) => {
disposableDelete = component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.PLURAL'
);
@@ -199,14 +203,14 @@ describe('NodeDeleteDirective', () => {
});
it('should notify failed nodes deletion', (done) => {
spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.reject('error'));
deleteNodeSpy.and.returnValue(Promise.reject('error'));
component.selection = [
{ entry: { id: '1', name: 'name1' } },
{ entry: { id: '2', name: 'name2' } }
];
component.deleteDirective.delete.subscribe((message) => {
disposableDelete = component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.ERROR_PLURAL'
);
@@ -221,7 +225,7 @@ describe('NodeDeleteDirective', () => {
});
it('should notify partial deletion when only one node is successful', (done) => {
spyOn(nodeApi, 'deleteNode').and.callFake((id) => {
deleteNodeSpy.and.callFake((id) => {
if (id === '1') {
return Promise.reject('error');
} else {
@@ -234,7 +238,7 @@ describe('NodeDeleteDirective', () => {
{ entry: { id: '2', name: 'name2' } }
];
component.deleteDirective.delete.subscribe((message) => {
disposableDelete = component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.PARTIAL_SINGULAR'
);
@@ -249,7 +253,7 @@ describe('NodeDeleteDirective', () => {
});
it('should notify partial deletion when some nodes are successful', (done) => {
spyOn(nodeApi, 'deleteNode').and.callFake((id) => {
deleteNodeSpy.and.callFake((id) => {
if (id === '1') {
return Promise.reject(null);
}
@@ -269,7 +273,7 @@ describe('NodeDeleteDirective', () => {
{ entry: { id: '3', name: 'name3' } }
];
component.deleteDirective.delete.subscribe((message) => {
disposableDelete = component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.PARTIAL_PLURAL'
);
@@ -284,36 +288,39 @@ describe('NodeDeleteDirective', () => {
});
it('should emit event when delete is done', (done) => {
spyOn(alfrescoApi.nodesApi, 'deleteNode').and.returnValue(Promise.resolve());
component.selection = <any> [{ entry: { id: '1', name: 'name1' } }];
fixture.detectChanges();
element.nativeElement.click();
fixture.detectChanges();
component.deleteDirective.delete.subscribe(() => {
disposableDelete = component.deleteDirective.delete.subscribe(() => {
done();
});
});
it('should disable the button if no node are selected', fakeAsync(() => {
it('should disable the button if no node are selected', (done) => {
component.selection = [];
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(element.nativeElement.disabled).toEqual(true);
done();
});
});
expect(element.nativeElement.disabled).toEqual(true);
}));
it('should disable the button if selected node is null', fakeAsync(() => {
it('should disable the button if selected node is null', (done) => {
component.selection = null;
fixture.detectChanges();
expect(element.nativeElement.disabled).toEqual(true);
}));
fixture.whenStable().then(() => {
expect(element.nativeElement.disabled).toEqual(true);
done();
});
});
it('should enable the button if nodes are selected', fakeAsync(() => {
it('should enable the button if nodes are selected', (done) => {
component.selection = [
{ entry: { id: '1', name: 'name1' } },
{ entry: { id: '2', name: 'name2' } },
@@ -322,10 +329,13 @@ describe('NodeDeleteDirective', () => {
fixture.detectChanges();
expect(element.nativeElement.disabled).toEqual(false);
}));
fixture.whenStable().then(() => {
expect(element.nativeElement.disabled).toEqual(false);
done();
});
});
it('should not enable the button if adf-node-permission is present', fakeAsync(() => {
it('should not enable the button if adf-node-permission is present', (done) => {
elementWithPermissions.nativeElement.disabled = false;
componentWithPermissions.selection = [];
@@ -339,14 +349,15 @@ describe('NodeDeleteDirective', () => {
fixtureWithPermissions.detectChanges();
expect(elementWithPermissions.nativeElement.disabled).toEqual(false);
}));
fixture.whenStable().then(() => {
expect(elementWithPermissions.nativeElement.disabled).toEqual(false);
done();
});
});
describe('Permanent', () => {
it('should call the api with permanent delete option if permanent directive input is true', fakeAsync(() => {
let deleteApi = spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.resolve());
it('should call the api with permanent delete option if permanent directive input is true', (done) => {
fixtureWithPermanentComponent.detectChanges();
componentWithPermanentDelete.selection = [
@@ -356,14 +367,14 @@ describe('NodeDeleteDirective', () => {
fixtureWithPermanentComponent.detectChanges();
elementWithPermanentDelete.nativeElement.click();
tick();
expect(deleteApi).toHaveBeenCalledWith('1', { permanent: true });
}));
it('should call the trashcan api if permanent directive input is true and the file is already in the trashcan ', fakeAsync(() => {
let deleteApi = spyOn(nodeApi, 'purgeDeletedNode').and.returnValue(Promise.resolve());
fixture.whenStable().then(() => {
expect(deleteNodeSpy).toHaveBeenCalledWith('1', { permanent: true });
done();
});
});
it('should call the trashcan api if permanent directive input is true and the file is already in the trashcan ', (done) => {
fixtureWithPermanentComponent.detectChanges();
componentWithPermanentDelete.selection = [
@@ -373,10 +384,12 @@ describe('NodeDeleteDirective', () => {
fixtureWithPermanentComponent.detectChanges();
elementWithPermanentDelete.nativeElement.click();
tick();
expect(deleteApi).toHaveBeenCalledWith('1');
}));
fixture.whenStable().then(() => {
expect(purgeDeletedNodeSpy).toHaveBeenCalledWith('1');
done();
});
});
});
});

View File

@@ -16,28 +16,25 @@
*/
import { Component, DebugElement } from '@angular/core';
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { Observable } from 'rxjs/Observable';
import { TranslationService } from '../services';
import { AlfrescoApiService } from '../services/alfresco-api.service';
import { NotificationService } from '../services/notification.service';
import { NodeRestoreDirective } from './node-restore.directive';
import { setupTestBed } from '../testing/setupTestBed';
import { CoreModule } from '../core.module';
import { AlfrescoApiServiceMock } from '../mock/alfresco-api.service.mock';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
@Component({
template: `
<div [adf-restore]="selection"
(restore)="done()">
(restore)="doneSpy()">
</div>`
})
class TestComponent {
selection = [];
done = jasmine.createSpy('done');
doneSpy = jasmine.createSpy('doneSpy');
}
describe('NodeRestoreDirective', () => {
@@ -45,17 +42,15 @@ describe('NodeRestoreDirective', () => {
let element: DebugElement;
let component: TestComponent;
let alfrescoService: AlfrescoApiService;
let translation: TranslationService;
let notification: NotificationService;
let router: Router;
let nodesService;
let coreApi;
let directiveInstance;
let restoreNodeSpy: any;
setupTestBed({
imports: [
CoreModule.forRoot(),
RouterTestingModule
NoopAnimationsModule
],
declarations: [
TestComponent
@@ -65,7 +60,7 @@ describe('NodeRestoreDirective', () => {
]
});
beforeEach(async(() => {
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
element = fixture.debugElement.query(By.directive(NodeRestoreDirective));
@@ -74,18 +69,15 @@ describe('NodeRestoreDirective', () => {
alfrescoService = TestBed.get(AlfrescoApiService);
nodesService = alfrescoService.getInstance().nodes;
coreApi = alfrescoService.getInstance().core;
translation = TestBed.get(TranslationService);
notification = TestBed.get(NotificationService);
router = TestBed.get(Router);
}));
beforeEach(() => {
spyOn(translation, 'instant').and.returnValue('message');
restoreNodeSpy = spyOn(nodesService, 'restoreNode').and.returnValue(Promise.resolve());
spyOn(coreApi.nodesApi, 'getDeletedNodes').and.returnValue(Promise.resolve({
list: { entries: [] }
}));
});
it('should not restore when selection is empty', () => {
spyOn(nodesService, 'restoreNode');
component.selection = [];
fixture.detectChanges();
@@ -94,54 +86,47 @@ describe('NodeRestoreDirective', () => {
expect(nodesService.restoreNode).not.toHaveBeenCalled();
});
it('should not restore nodes when selection has nodes without path', () => {
spyOn(nodesService, 'restoreNode');
component.selection = [ { entry: { id: '1' } } ];
it('should not restore nodes when selection has nodes without path', (done) => {
component.selection = [{ entry: { id: '1' } }];
fixture.detectChanges();
element.triggerEventHandler('click', null);
fixture.whenStable().then(() => {
element.triggerEventHandler('click', null);
expect(nodesService.restoreNode).not.toHaveBeenCalled();
expect(nodesService.restoreNode).not.toHaveBeenCalled();
done();
});
});
it('should call restore when selection has nodes with path', fakeAsync(() => {
spyOn(directiveInstance, 'restoreNotification').and.callFake(() => null);
spyOn(nodesService, 'restoreNode').and.returnValue(Promise.resolve());
spyOn(coreApi.nodesApi, 'getDeletedNodes').and.returnValue(Promise.resolve({
list: { entries: [] }
}));
it('should call restore when selection has nodes with path', (done) => {
component.selection = [{ entry: { id: '1', path: ['somewhere-over-the-rainbow'] } }];
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(nodesService.restoreNode).toHaveBeenCalled();
}));
describe('refresh()', () => {
it('should reset selection', fakeAsync(() => {
spyOn(directiveInstance, 'restoreNotification').and.callFake(() => null);
spyOn(nodesService, 'restoreNode').and.returnValue(Promise.resolve());
spyOn(coreApi.nodesApi, 'getDeletedNodes').and.returnValue(Promise.resolve({
list: { entries: [] }
}));
fixture.whenStable().then(() => {
expect(nodesService.restoreNode).toHaveBeenCalled();
done();
});
});
describe('reset', () => {
it('should reset selection', (done) => {
component.selection = [{ entry: { id: '1', path: ['somewhere-over-the-rainbow'] } }];
directiveInstance.restore.subscribe(() => {
expect(directiveInstance.selection.length).toBe(0);
done();
});
fixture.detectChanges();
expect(directiveInstance.selection.length).toBe(1);
fixture.whenStable().then(() => {
expect(directiveInstance.selection.length).toBe(1);
element.triggerEventHandler('click', null);
});
});
element.triggerEventHandler('click', null);
tick();
expect(directiveInstance.selection.length).toBe(0);
}));
it('should reset status', fakeAsync(() => {
it('should reset status', () => {
directiveInstance.restoreProcessStatus.fail = [{}];
directiveInstance.restoreProcessStatus.success = [{}];
@@ -149,39 +134,34 @@ describe('NodeRestoreDirective', () => {
expect(directiveInstance.restoreProcessStatus.fail).toEqual([]);
expect(directiveInstance.restoreProcessStatus.success).toEqual([]);
}));
});
it('should emit event on finish', fakeAsync(() => {
spyOn(directiveInstance, 'restoreNotification').and.callFake(() => null);
spyOn(nodesService, 'restoreNode').and.returnValue(Promise.resolve());
spyOn(coreApi.nodesApi, 'getDeletedNodes').and.returnValue(Promise.resolve({
list: { entries: [] }
}));
it('should emit event on finish', (done) => {
spyOn(element.nativeElement, 'dispatchEvent');
directiveInstance.restore.subscribe(() => {
expect(component.doneSpy).toHaveBeenCalled();
done();
});
component.selection = [{ entry: { id: '1', path: ['somewhere-over-the-rainbow'] } }];
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(component.done).toHaveBeenCalled();
}));
});
});
describe('notification', () => {
beforeEach(() => {
spyOn(coreApi.nodesApi, 'getDeletedNodes').and.returnValue(Promise.resolve({
list: { entries: [] }
}));
});
it('should notify on multiple fails', fakeAsync(() => {
it('should notify on multiple fails', (done) => {
const error = { message: '{ "error": {} }' };
spyOn(notification, 'openSnackMessageAction').and.returnValue({ onAction: () => Observable.throw(null) });
directiveInstance.restore.subscribe((event) => {
expect(event.message).toEqual('CORE.RESTORE_NODE.PARTIAL_PLURAL');
done();
});
spyOn(nodesService, 'restoreNode').and.callFake((id) => {
restoreNodeSpy.and.callFake((id) => {
if (id === '1') {
return Promise.resolve();
}
@@ -203,19 +183,18 @@ describe('NodeRestoreDirective', () => {
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
});
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.PARTIAL_PLURAL',
{ number: 2 }
);
}));
it('should notify fail when restored node exist, error 409', fakeAsync(() => {
it('should notify fail when restored node exist, error 409', (done) => {
const error = { message: '{ "error": { "statusCode": 409 } }' };
spyOn(notification, 'openSnackMessageAction').and.returnValue({ onAction: () => Observable.throw(null) });
spyOn(nodesService, 'restoreNode').and.returnValue(Promise.reject(error));
directiveInstance.restore.subscribe((event) => {
expect(event.message).toEqual('CORE.RESTORE_NODE.NODE_EXISTS');
done();
});
restoreNodeSpy.and.returnValue(Promise.reject(error));
component.selection = [
{ entry: { id: '1', name: 'name1', path: ['somewhere-over-the-rainbow'] } }
@@ -223,19 +202,18 @@ describe('NodeRestoreDirective', () => {
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
});
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.NODE_EXISTS',
{ name: 'name1' }
);
}));
it('should notify fail when restored node returns different statusCode', fakeAsync(() => {
it('should notify fail when restored node returns different statusCode', (done) => {
const error = { message: '{ "error": { "statusCode": 404 } }' };
spyOn(notification, 'openSnackMessageAction').and.returnValue({ onAction: () => Observable.throw(null) });
spyOn(nodesService, 'restoreNode').and.returnValue(Promise.reject(error));
restoreNodeSpy.and.returnValue(Promise.reject(error));
directiveInstance.restore.subscribe((event) => {
expect(event.message).toEqual('CORE.RESTORE_NODE.GENERIC');
done();
});
component.selection = [
{ entry: { id: '1', name: 'name1', path: ['somewhere-over-the-rainbow'] } }
@@ -243,19 +221,17 @@ describe('NodeRestoreDirective', () => {
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
});
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.GENERIC',
{ name: 'name1' }
);
}));
it('should notify fail when restored node location is missing', fakeAsync(() => {
it('should notify fail when restored node location is missing', (done) => {
const error = { message: '{ "error": { } }' };
spyOn(notification, 'openSnackMessageAction').and.returnValue({ onAction: () => Observable.throw(null) });
spyOn(nodesService, 'restoreNode').and.returnValue(Promise.reject(error));
restoreNodeSpy.and.returnValue(Promise.reject(error));
directiveInstance.restore.subscribe((event) => {
expect(event.message).toEqual('CORE.RESTORE_NODE.LOCATION_MISSING');
done();
});
component.selection = [
{ entry: { id: '1', name: 'name1', path: ['somewhere-over-the-rainbow'] } }
@@ -263,17 +239,17 @@ describe('NodeRestoreDirective', () => {
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
});
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.LOCATION_MISSING',
{ name: 'name1' }
);
}));
it('should notify success when restore multiple nodes', (done) => {
it('should notify success when restore multiple nodes', fakeAsync(() => {
spyOn(notification, 'openSnackMessageAction').and.returnValue({ onAction: () => Observable.throw(null) });
spyOn(nodesService, 'restoreNode').and.callFake((id) => {
directiveInstance.restore.subscribe((event) => {
expect(event.message).toEqual('CORE.RESTORE_NODE.PLURAL');
done();
});
restoreNodeSpy.and.callFake((id) => {
if (id === '1') {
return Promise.resolve();
}
@@ -290,16 +266,15 @@ describe('NodeRestoreDirective', () => {
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.PLURAL'
);
}));
});
it('should notify success on restore selected node', fakeAsync(() => {
spyOn(notification, 'openSnackMessageAction').and.returnValue({ onAction: () => Observable.throw(null) });
spyOn(nodesService, 'restoreNode').and.returnValue(Promise.resolve());
it('should notify success on restore selected node', (done) => {
directiveInstance.restore.subscribe((event) => {
expect(event.message).toEqual('CORE.RESTORE_NODE.SINGULAR');
done();
});
component.selection = [
{ entry: { id: '1', name: 'name1', path: ['somewhere-over-the-rainbow'] } }
@@ -307,36 +282,8 @@ describe('NodeRestoreDirective', () => {
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.SINGULAR',
{ name: 'name1' }
);
}));
});
it('should navigate to restored node location onAction', fakeAsync(() => {
spyOn(router, 'navigate');
spyOn(nodesService, 'restoreNode').and.returnValue(Promise.resolve());
spyOn(notification, 'openSnackMessageAction').and.returnValue({ onAction: () => Observable.of({}) });
component.selection = [
{
entry: {
id: '1',
name: 'name1',
path: {
elements: ['somewhere-over-the-rainbow']
}
}
}
];
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(router.navigate).toHaveBeenCalled();
}));
});
});

View File

@@ -18,11 +18,9 @@
/* tslint:disable:component-selector no-input-rename */
import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core';
import { Router } from '@angular/router';
import { DeletedNodeEntry, DeletedNodesPaging, PathInfoEntity } from 'alfresco-js-api';
import { DeletedNodeEntry, DeletedNodesPaging } from 'alfresco-js-api';
import { Observable } from 'rxjs/Observable';
import { AlfrescoApiService } from '../services/alfresco-api.service';
import { NotificationService } from '../services/notification.service';
import { TranslationService } from '../services/translation.service';
import 'rxjs/add/observable/from';
import 'rxjs/add/observable/zip';
@@ -52,9 +50,7 @@ export class NodeRestoreDirective {
}
constructor(private alfrescoApiService: AlfrescoApiService,
private translation: TranslationService,
private router: Router,
private notification: NotificationService) {
private translation: TranslationService) {
this.restoreProcessStatus = this.processStatus();
}
@@ -65,36 +61,35 @@ export class NodeRestoreDirective {
const nodesWithPath = this.getNodesWithPath(selection);
if (selection.length && !nodesWithPath.length) {
if (selection.length && nodesWithPath.length) {
this.restoreNodesBatch(nodesWithPath)
.do((restoredNodes) => {
const status = this.processStatus(restoredNodes);
this.restoreProcessStatus.fail.push(...status.fail);
this.restoreProcessStatus.success.push(...status.success);
})
.mergeMap(() => this.getDeletedNodes())
.subscribe(
(deletedNodesList: any) => {
const { entries: nodelist } = deletedNodesList.list;
const { fail: restoreErrorNodes } = this.restoreProcessStatus;
const selectedNodes = this.diff(restoreErrorNodes, selection, false);
const remainingNodes = this.diff(selectedNodes, nodelist);
if (!remainingNodes.length) {
this.notification();
} else {
this.recover(remainingNodes);
}
}
);
} else {
this.restoreProcessStatus.fail.push(...selection);
this.restoreNotification();
this.refresh();
this.notification();
return;
}
this.restoreNodesBatch(nodesWithPath)
.do((restoredNodes) => {
const status = this.processStatus(restoredNodes);
this.restoreProcessStatus.fail.push(...status.fail);
this.restoreProcessStatus.success.push(...status.success);
})
.mergeMap(() => this.getDeletedNodes())
.subscribe(
(deletedNodesList: any) => {
const { entries: nodelist } = deletedNodesList.list;
const { fail: restoreErrorNodes } = this.restoreProcessStatus;
const selectedNodes = this.diff(restoreErrorNodes, selection, false);
const remainingNodes = this.diff(selectedNodes, nodelist);
if (!remainingNodes.length) {
this.restoreNotification();
this.refresh();
} else {
this.recover(remainingNodes);
}
}
);
}
private restoreNodesBatch(batch: DeletedNodeEntry[]): Observable<DeletedNodeEntry[]> {
@@ -133,12 +128,6 @@ export class NodeRestoreDirective {
});
}
private navigateLocation(path: PathInfoEntity) {
const parent = path.elements[path.elements.length - 1];
this.router.navigate([this.location, parent.id]);
}
private diff(selection, list, fromList = true): any {
const ids = selection.map(item => item.entry.id);
@@ -246,21 +235,19 @@ export class NodeRestoreDirective {
}
}
private restoreNotification(): void {
private notification(): void {
const status = Object.assign({}, this.restoreProcessStatus);
let message = this.getRestoreMessage();
this.reset();
const action = (status.oneSucceeded && !status.someFailed) ? this.translation.instant('CORE.RESTORE_NODE.VIEW') : '';
this.notification.openSnackMessageAction(message, action)
.onAction()
.subscribe(() => this.navigateLocation(status.success[0].entry.path));
this.restore.emit({ message: message, action: action });
}
private refresh(): void {
private reset(): void {
this.restoreProcessStatus.reset();
this.selection = [];
this.restore.emit();
}
}