mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
[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:
@@ -22,7 +22,7 @@ import { MatDialogRef } from '@angular/material';
|
||||
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
|
||||
import { NodesApiService, NotificationService, TranslationService } from '@alfresco/adf-core';
|
||||
import { NodesApiService, TranslationService } from '@alfresco/adf-core';
|
||||
import { FolderDialogComponent } from './folder.dialog';
|
||||
|
||||
describe('FolderDialogComponent', () => {
|
||||
@@ -31,7 +31,6 @@ describe('FolderDialogComponent', () => {
|
||||
let component: FolderDialogComponent;
|
||||
let translationService: TranslationService;
|
||||
let nodesApi: NodesApiService;
|
||||
let notificationService: NotificationService;
|
||||
let dialogRef;
|
||||
|
||||
beforeEach(async(() => {
|
||||
@@ -56,7 +55,7 @@ describe('FolderDialogComponent', () => {
|
||||
// entryComponents are not supported yet on TestBed, that is why this ugly workaround:
|
||||
// https://github.com/angular/angular/issues/10760
|
||||
TestBed.overrideModule(BrowserDynamicTestingModule, {
|
||||
set: {entryComponents: [ FolderDialogComponent ]}
|
||||
set: { entryComponents: [FolderDialogComponent] }
|
||||
});
|
||||
|
||||
TestBed.compileComponents();
|
||||
@@ -67,7 +66,6 @@ describe('FolderDialogComponent', () => {
|
||||
component = fixture.componentInstance;
|
||||
|
||||
nodesApi = TestBed.get(NodesApiService);
|
||||
notificationService = TestBed.get(NotificationService);
|
||||
|
||||
translationService = TestBed.get(TranslationService);
|
||||
spyOn(translationService, 'get').and.returnValue(Observable.of('message'));
|
||||
@@ -237,33 +235,45 @@ describe('FolderDialogComponent', () => {
|
||||
expect(component.handleError).toHaveBeenCalled();
|
||||
expect(dialogRef.close).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleError()', () => {
|
||||
it('should raise error for 409', () => {
|
||||
spyOn(notificationService, 'openSnackMessage').and.stub();
|
||||
describe('Error events ', () => {
|
||||
it('should raise error for 409', (done) => {
|
||||
const error = {
|
||||
message: '{ "error": { "statusCode" : 409 } }'
|
||||
};
|
||||
|
||||
const error = {
|
||||
message: '{ "error": { "statusCode" : 409 } }'
|
||||
};
|
||||
component.error.subscribe((message) => {
|
||||
expect(message).toBe('CORE.MESSAGES.ERRORS.EXISTENT_FOLDER');
|
||||
done();
|
||||
});
|
||||
|
||||
component.handleError(error);
|
||||
spyOn(nodesApi, 'createFolder').and.returnValue(Observable.throw(error));
|
||||
|
||||
expect(notificationService.openSnackMessage).toHaveBeenCalled();
|
||||
expect(translationService.get).toHaveBeenCalledWith('CORE.MESSAGES.ERRORS.EXISTENT_FOLDER');
|
||||
component.form.controls['name'].setValue('name');
|
||||
component.form.controls['description'].setValue('description');
|
||||
|
||||
component.submit();
|
||||
});
|
||||
|
||||
it('should raise generic error', (done) => {
|
||||
const error = {
|
||||
message: '{ "error": { "statusCode" : 123 } }'
|
||||
};
|
||||
|
||||
component.error.subscribe((message) => {
|
||||
expect(message).toBe('CORE.MESSAGES.ERRORS.GENERIC');
|
||||
done();
|
||||
});
|
||||
|
||||
spyOn(nodesApi, 'createFolder').and.returnValue(Observable.throw(error));
|
||||
|
||||
component.form.controls['name'].setValue('name');
|
||||
component.form.controls['description'].setValue('description');
|
||||
|
||||
component.submit();
|
||||
});
|
||||
});
|
||||
|
||||
it('should raise generic error', () => {
|
||||
spyOn(notificationService, 'openSnackMessage').and.stub();
|
||||
|
||||
const error = {
|
||||
message: '{ "error": { "statusCode" : 123 } }'
|
||||
};
|
||||
|
||||
component.handleError(error);
|
||||
|
||||
expect(notificationService.openSnackMessage).toHaveBeenCalled();
|
||||
expect(translationService.get).toHaveBeenCalledWith('CORE.MESSAGES.ERRORS.GENERIC');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -17,12 +17,12 @@
|
||||
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
|
||||
import { Component, Inject, OnInit, Optional } from '@angular/core';
|
||||
import { Component, Inject, OnInit, Optional, EventEmitter, Output } from '@angular/core';
|
||||
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
|
||||
|
||||
import { MinimalNodeEntryEntity } from 'alfresco-js-api';
|
||||
import { NodesApiService, NotificationService, TranslationService } from '@alfresco/adf-core';
|
||||
import { NodesApiService, TranslationService } from '@alfresco/adf-core';
|
||||
|
||||
import { forbidEndingDot, forbidOnlySpaces, forbidSpecialCharacters } from './folder-name.validators';
|
||||
|
||||
@@ -32,15 +32,21 @@ import { forbidEndingDot, forbidOnlySpaces, forbidSpecialCharacters } from './fo
|
||||
templateUrl: './folder.dialog.html'
|
||||
})
|
||||
export class FolderDialogComponent implements OnInit {
|
||||
|
||||
form: FormGroup;
|
||||
|
||||
folder: MinimalNodeEntryEntity = null;
|
||||
|
||||
/** Emitted when the edit/create folder give error for example a folder with same name already exist
|
||||
*/
|
||||
@Output()
|
||||
error: EventEmitter<any> = new EventEmitter<any>();
|
||||
|
||||
constructor(
|
||||
private formBuilder: FormBuilder,
|
||||
private dialog: MatDialogRef<FolderDialogComponent>,
|
||||
private nodesApi: NodesApiService,
|
||||
private translation: TranslationService,
|
||||
private notification: NotificationService,
|
||||
@Optional()
|
||||
@Inject(MAT_DIALOG_DATA)
|
||||
public data: any
|
||||
@@ -121,19 +127,17 @@ export class FolderDialogComponent implements OnInit {
|
||||
}
|
||||
|
||||
handleError(error: any): any {
|
||||
let i18nMessageString = 'CORE.MESSAGES.ERRORS.GENERIC';
|
||||
let errorMessage = 'CORE.MESSAGES.ERRORS.GENERIC';
|
||||
|
||||
try {
|
||||
const { error: { statusCode } } = JSON.parse(error.message);
|
||||
|
||||
if (statusCode === 409) {
|
||||
i18nMessageString = 'CORE.MESSAGES.ERRORS.EXISTENT_FOLDER';
|
||||
errorMessage = 'CORE.MESSAGES.ERRORS.EXISTENT_FOLDER';
|
||||
}
|
||||
} catch (err) { /* Do nothing, keep the original message */ }
|
||||
|
||||
this.translation.get(i18nMessageString).subscribe(message => {
|
||||
this.notification.openSnackMessage(message, 3000);
|
||||
});
|
||||
this.error.emit(this.translation.instant(errorMessage));
|
||||
|
||||
return error;
|
||||
}
|
||||
|
Reference in New Issue
Block a user