[ASD-2483] Validate folder name on change (#3088)

* notify service refactoring
get translate eliminitation in favor of instant
add error event where necessary
fix config problem during test

* fix delete notify test

* remove fdescribe

* fix core test

* errors

* fix types
This commit is contained in:
Eugenio Romano
2018-03-21 16:55:52 +00:00
committed by GitHub
parent de0fdd9ab4
commit 2951374cc0
25 changed files with 357 additions and 260 deletions

View File

@@ -15,36 +15,41 @@
* limitations under the License.
*/
import { Component, DebugElement } from '@angular/core';
import { Component, DebugElement, ViewChild } from '@angular/core';
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { AlfrescoApiService } from '../services/alfresco-api.service';
import { NotificationService } from '../services/notification.service';
import { NodeDeleteDirective } from './node-delete.directive';
@Component({
template: `
<div [adf-delete]="selection"
(delete)="done()">
<div id="delete-component" [adf-delete]="selection"
(delete)="onDelete($event)">
</div>`
})
class TestComponent {
selection = [];
done = jasmine.createSpy('done');
@ViewChild(NodeDeleteDirective)
deleteDirective;
onDelete = jasmine.createSpy('onDelete');
}
@Component({
template: `
<div [adf-node-permission]="selection"
<div id="delete-component" [adf-node-permission]="selection"
[adf-delete]="selection"
(delete)="done()">
(delete)="onDelete($event)">
</div>`
})
class TestWithPermissionsComponent {
selection = [];
done = jasmine.createSpy('done');
@ViewChild(NodeDeleteDirective)
deleteDirective;
onDelete = jasmine.createSpy('onDelete');
}
@Component({
@@ -53,18 +58,21 @@ class TestWithPermissionsComponent {
<div id="delete-permanent"
[adf-delete]="selection"
[permanent]="permanent"
(delete)="done()">
(delete)="onDelete($event)">
</div>`
})
class TestDeletePermanentComponent {
selection = [];
@ViewChild(NodeDeleteDirective)
deleteDirective;
permanent = true;
done = jasmine.createSpy('done');
onDelete = jasmine.createSpy('onDelete');
}
describe('NodeDeleteDirective', () => {
describe('NodeleteDirective', () => {
let fixture: ComponentFixture<TestComponent>;
let fixtureWithPermissions: ComponentFixture<TestWithPermissionsComponent>;
let fixtureWithPermanentComponent: ComponentFixture<TestDeletePermanentComponent>;
@@ -75,7 +83,6 @@ describe('NodeDeleteDirective', () => {
let componentWithPermissions: TestWithPermissionsComponent;
let componentWithPermanentDelete: TestDeletePermanentComponent;
let alfrescoApi: AlfrescoApiService;
let notification: NotificationService;
let nodeApi;
beforeEach(async(() => {
@@ -103,54 +110,60 @@ describe('NodeDeleteDirective', () => {
alfrescoApi = TestBed.get(AlfrescoApiService);
nodeApi = alfrescoApi.getInstance().nodes;
notification = TestBed.get(NotificationService);
});
}));
describe('Delete', () => {
beforeEach(() => {
spyOn(notification, 'openSnackMessage');
});
it('should do nothing if selection is empty', () => {
spyOn(nodeApi, 'deleteNode');
component.selection = [];
fixture.detectChanges();
element.triggerEventHandler('click', null);
element.nativeElement.click();
expect(nodeApi.deleteNode).not.toHaveBeenCalled();
});
it('should process node successfully', fakeAsync(() => {
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) => {
expect(message).toBe(
'CORE.DELETE_NODE.SINGULAR'
);
done();
});
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(notification.openSnackMessage).toHaveBeenCalledWith(
'CORE.DELETE_NODE.SINGULAR'
);
}));
fixture.whenStable().then(() => {
element.nativeElement.click();
});
});
it('should notify failed node deletion', fakeAsync(() => {
it('should notify failed node deletion', (done) => {
spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.reject('error'));
component.selection = [{ entry: { id: '1', name: 'name1' } }];
component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.ERROR_SINGULAR'
);
done();
});
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(notification.openSnackMessage).toHaveBeenCalledWith(
'CORE.DELETE_NODE.ERROR_SINGULAR'
);
}));
fixture.whenStable().then(() => {
element.nativeElement.click();
});
});
it('should notify nodes deletion', fakeAsync(() => {
it('should notify nodes deletion', (done) => {
spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.resolve());
component.selection = [
@@ -158,16 +171,21 @@ describe('NodeDeleteDirective', () => {
{ entry: { id: '2', name: 'name2' } }
];
component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.PLURAL'
);
done();
});
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(notification.openSnackMessage).toHaveBeenCalledWith(
'CORE.DELETE_NODE.PLURAL'
);
}));
fixture.whenStable().then(() => {
element.nativeElement.click();
});
});
it('should notify failed nodes deletion', fakeAsync(() => {
it('should notify failed nodes deletion', (done) => {
spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.reject('error'));
component.selection = [
@@ -175,16 +193,21 @@ describe('NodeDeleteDirective', () => {
{ entry: { id: '2', name: 'name2' } }
];
component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.ERROR_PLURAL'
);
done();
});
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(notification.openSnackMessage).toHaveBeenCalledWith(
'CORE.DELETE_NODE.ERROR_PLURAL'
);
}));
fixture.whenStable().then(() => {
element.nativeElement.click();
});
});
it('should notify partial deletion when only one node is successful', fakeAsync(() => {
it('should notify partial deletion when only one node is successful', (done) => {
spyOn(nodeApi, 'deleteNode').and.callFake((id) => {
if (id === '1') {
return Promise.reject('error');
@@ -198,16 +221,21 @@ describe('NodeDeleteDirective', () => {
{ entry: { id: '2', name: 'name2' } }
];
component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.PARTIAL_SINGULAR'
);
done();
});
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
expect(notification.openSnackMessage).toHaveBeenCalledWith(
'CORE.DELETE_NODE.PARTIAL_SINGULAR'
);
}));
fixture.whenStable().then(() => {
element.nativeElement.click();
});
});
it('should notify partial deletion when some nodes are successful', fakeAsync(() => {
it('should notify partial deletion when some nodes are successful', (done) => {
spyOn(nodeApi, 'deleteNode').and.callFake((id) => {
if (id === '1') {
return Promise.reject(null);
@@ -228,26 +256,31 @@ describe('NodeDeleteDirective', () => {
{ entry: { id: '3', name: 'name3' } }
];
fixture.detectChanges();
element.triggerEventHandler('click', null);
tick();
component.deleteDirective.delete.subscribe((message) => {
expect(message).toBe(
'CORE.DELETE_NODE.PARTIAL_PLURAL'
);
done();
});
expect(notification.openSnackMessage).toHaveBeenCalledWith(
'CORE.DELETE_NODE.PARTIAL_PLURAL'
);
}));
fixture.detectChanges();
fixture.whenStable().then(() => {
element.nativeElement.click();
});
});
it('should emit event when delete is done', fakeAsync(() => {
component.done.calls.reset();
component.onDelete.calls.reset();
spyOn(nodeApi, 'deleteNode').and.returnValue(Promise.resolve());
component.selection = <any> [{ entry: { id: '1', name: 'name1' } }];
fixture.detectChanges();
element.triggerEventHandler('click', null);
element.nativeElement.click();
tick();
expect(component.done).toHaveBeenCalled();
expect(component.onDelete).toHaveBeenCalled();
}));
it('should disable the button if no node are selected', fakeAsync(() => {
@@ -303,12 +336,12 @@ describe('NodeDeleteDirective', () => {
fixtureWithPermanentComponent.detectChanges();
componentWithPermanentDelete.selection = [
{ entry: { id: '1', name: 'name1'}
{ entry: { id: '1', name: 'name1' }
];
fixtureWithPermanentComponent.detectChanges();
elementWithPermanentDelete.triggerEventHandler('click', null);
elementWithPermanentDelete.nativeElement.click();
tick();
expect(deleteApi).toHaveBeenCalledWith('1', { permanent: true });
@@ -325,7 +358,7 @@ describe('NodeDeleteDirective', () => {
fixtureWithPermanentComponent.detectChanges();
elementWithPermanentDelete.triggerEventHandler('click', null);
elementWithPermanentDelete.nativeElement.click();
tick();
expect(deleteApi).toHaveBeenCalledWith('1');

View File

@@ -21,7 +21,6 @@ import { Directive, ElementRef, EventEmitter, HostListener, Input, OnChanges, Ou
import { MinimalNodeEntity, MinimalNodeEntryEntity, DeletedNodeEntity, DeletedNodeMinimalEntry } 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/fromPromise';
import 'rxjs/observable/forkJoin';
@@ -70,8 +69,7 @@ export class NodeDeleteDirective implements OnChanges {
this.process(this.selection);
}
constructor(private notification: NotificationService,
private alfrescoApiService: AlfrescoApiService,
constructor(private alfrescoApiService: AlfrescoApiService,
private translation: TranslationService,
private elementRef: ElementRef) {
}
@@ -99,11 +97,7 @@ export class NodeDeleteDirective implements OnChanges {
.subscribe((data: ProcessedNodeData[]) => {
const processedItems: ProcessStatus = this.processStatus(data);
this.notify(processedItems);
if (processedItems.someSucceeded) {
this.delete.emit();
}
this.delete.emit(this.getMessage(processedItems));
});
}
}
@@ -174,27 +168,23 @@ export class NodeDeleteDirective implements OnChanges {
);
}
private notify(status) {
this.getMessage(status).subscribe((message) => this.notification.openSnackMessage(message));
}
private getMessage(status): Observable<string> {
private getMessage(status): string {
if (status.allFailed && !status.oneFailed) {
return this.translation.get(
return this.translation.instant(
'CORE.DELETE_NODE.ERROR_PLURAL',
{ number: status.failed.length }
);
}
if (status.allSucceeded && !status.oneSucceeded) {
return this.translation.get(
return this.translation.instant(
'CORE.DELETE_NODE.PLURAL',
{ number: status.success.length }
);
}
if (status.someFailed && status.someSucceeded && !status.oneSucceeded) {
return this.translation.get(
return this.translation.instant(
'CORE.DELETE_NODE.PARTIAL_PLURAL',
{
success: status.success.length,
@@ -204,7 +194,7 @@ export class NodeDeleteDirective implements OnChanges {
}
if (status.someFailed && status.oneSucceeded) {
return this.translation.get(
return this.translation.instant(
'CORE.DELETE_NODE.PARTIAL_SINGULAR',
{
success: status.success.length,
@@ -214,14 +204,14 @@ export class NodeDeleteDirective implements OnChanges {
}
if (status.oneFailed && !status.someSucceeded) {
return this.translation.get(
return this.translation.instant(
'CORE.DELETE_NODE.ERROR_SINGULAR',
{ name: status.failed[0].entry.name }
);
}
if (status.oneSucceeded && !status.someFailed) {
return this.translation.get(
return this.translation.instant(
'CORE.DELETE_NODE.SINGULAR',
{ name: status.success[0].entry.name }
);

View File

@@ -76,7 +76,7 @@ describe('NodeRestoreDirective', () => {
}));
beforeEach(() => {
spyOn(translation, 'get').and.returnValue(Observable.of('message'));
spyOn(translation, 'instant').and.returnValue('message');
});
it('should not restore when selection is empty', () => {
@@ -201,7 +201,7 @@ describe('NodeRestoreDirective', () => {
element.triggerEventHandler('click', null);
tick();
expect(translation.get).toHaveBeenCalledWith(
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.PARTIAL_PLURAL',
{ number: 2 }
);
@@ -221,7 +221,7 @@ describe('NodeRestoreDirective', () => {
element.triggerEventHandler('click', null);
tick();
expect(translation.get).toHaveBeenCalledWith(
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.NODE_EXISTS',
{ name: 'name1' }
);
@@ -241,7 +241,7 @@ describe('NodeRestoreDirective', () => {
element.triggerEventHandler('click', null);
tick();
expect(translation.get).toHaveBeenCalledWith(
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.GENERIC',
{ name: 'name1' }
);
@@ -261,7 +261,7 @@ describe('NodeRestoreDirective', () => {
element.triggerEventHandler('click', null);
tick();
expect(translation.get).toHaveBeenCalledWith(
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.LOCATION_MISSING',
{ name: 'name1' }
);
@@ -288,7 +288,7 @@ describe('NodeRestoreDirective', () => {
element.triggerEventHandler('click', null);
tick();
expect(translation.get).toHaveBeenCalledWith(
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.PLURAL'
);
}));
@@ -305,7 +305,7 @@ describe('NodeRestoreDirective', () => {
element.triggerEventHandler('click', null);
tick();
expect(translation.get).toHaveBeenCalledWith(
expect(translation.instant).toHaveBeenCalledWith(
'CORE.RESTORE_NODE.SINGULAR',
{ name: 'name1' }
);

View File

@@ -51,16 +51,14 @@ export class NodeRestoreDirective {
this.recover(this.selection);
}
constructor(
private alfrescoApiService: AlfrescoApiService,
private translation: TranslationService,
private router: Router,
private notification: NotificationService
) {
constructor(private alfrescoApiService: AlfrescoApiService,
private translation: TranslationService,
private router: Router,
private notification: NotificationService) {
this.restoreProcessStatus = this.processStatus();
}
private recover(selection: any) {
private recover(selection: any) {
if (!selection.length) {
return;
}
@@ -109,7 +107,7 @@ export class NodeRestoreDirective {
private getDeletedNodes(): Observable<DeletedNodesPaging> {
const promise = this.alfrescoApiService.getInstance()
.core.nodesApi.getDeletedNodes({ include: [ 'path' ] });
.core.nodesApi.getDeletedNodes({ include: ['path'] });
return Observable.from(promise);
}
@@ -138,10 +136,10 @@ export class NodeRestoreDirective {
private navigateLocation(path: PathInfoEntity) {
const parent = path.elements[path.elements.length - 1];
this.router.navigate([ this.location, parent.id ]);
this.router.navigate([this.location, parent.id]);
}
private diff(selection , list, fromList = true): any {
private diff(selection, list, fromList = true): any {
const ids = selection.map(item => item.entry.id);
return list.filter(item => {
@@ -195,11 +193,11 @@ export class NodeRestoreDirective {
);
}
private getRestoreMessage(): Observable<string|any> {
private getRestoreMessage(): string {
const { restoreProcessStatus: status } = this;
if (status.someFailed && !status.oneFailed) {
return this.translation.get(
return this.translation.instant(
'CORE.RESTORE_NODE.PARTIAL_PLURAL',
{
number: status.fail.length
@@ -209,14 +207,14 @@ export class NodeRestoreDirective {
if (status.oneFailed && status.fail[0].statusCode) {
if (status.fail[0].statusCode === 409) {
return this.translation.get(
return this.translation.instant(
'CORE.RESTORE_NODE.NODE_EXISTS',
{
name: status.fail[0].entry.name
}
);
} else {
return this.translation.get(
return this.translation.instant(
'CORE.RESTORE_NODE.GENERIC',
{
name: status.fail[0].entry.name
@@ -226,7 +224,7 @@ export class NodeRestoreDirective {
}
if (status.oneFailed && !status.fail[0].statusCode) {
return this.translation.get(
return this.translation.instant(
'CORE.RESTORE_NODE.LOCATION_MISSING',
{
name: status.fail[0].entry.name
@@ -235,11 +233,11 @@ export class NodeRestoreDirective {
}
if (status.allSucceeded && !status.oneSucceeded) {
return this.translation.get('CORE.RESTORE_NODE.PLURAL');
return this.translation.instant('CORE.RESTORE_NODE.PLURAL');
}
if (status.allSucceeded && status.oneSucceeded) {
return this.translation.get(
return this.translation.instant(
'CORE.RESTORE_NODE.SINGULAR',
{
name: status.success[0].entry.name
@@ -251,17 +249,13 @@ export class NodeRestoreDirective {
private restoreNotification(): void {
const status = Object.assign({}, this.restoreProcessStatus);
Observable.zip(
this.getRestoreMessage(),
this.translation.get('CORE.RESTORE_NODE.VIEW')
).subscribe((messages) => {
const [ message, actionLabel ] = messages;
const action = (status.oneSucceeded && !status.someFailed) ? actionLabel : '';
let message = this.getRestoreMessage();
this.notification.openSnackMessageAction(message, action)
.onAction()
.subscribe(() => this.navigateLocation(status.success[0].entry.path));
});
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));
}
private refresh(): void {