From cf287abdc1f8b758fa69d5cab06ef53e2808698f Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Thu, 13 Oct 2016 13:53:47 +0100 Subject: [PATCH] Tests for Upload widget --- .../widgets/upload/upload.widget.html | 2 +- .../widgets/upload/upload.widget.spec.ts | 84 +++++++++++++++++++ .../widgets/upload/upload.widget.ts | 15 ++-- 3 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 ng2-components/ng2-activiti-form/src/components/widgets/upload/upload.widget.spec.ts diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/upload/upload.widget.html b/ng2-components/ng2-activiti-form/src/components/widgets/upload/upload.widget.html index 270d2c094b..ce0580310c 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/upload/upload.widget.html +++ b/ng2-components/ng2-activiti-form/src/components/widgets/upload/upload.widget.html @@ -3,7 +3,7 @@
attachment - {{getUploadedFileName()}} + {{displayText}} { + + let widget: UploadWidget; + let settingsService: AlfrescoSettingsService; + let authService: AlfrescoAuthenticationService; + + beforeEach(() => { + settingsService = new AlfrescoSettingsService(); + authService = new AlfrescoAuthenticationService(settingsService, new AlfrescoApiService()); + widget = new UploadWidget(settingsService, authService); + }); + + it('should setup with field data', () => { + const fileName = 'hello world'; + const encodedFileName = encodeURI(fileName); + + widget.field = new FormFieldModel(null, { + type: FormFieldTypes.UPLOAD, + value: [ + { name: encodedFileName } + ] + }); + + widget.ngOnInit(); + expect(widget.hasFile).toBeTruthy(); + expect(widget.fileName).toBe(encodeURI(fileName)); + expect(widget.displayText).toBe(fileName); + }); + + it('should require form field to setup', () => { + widget.field = null; + widget.ngOnInit(); + + expect(widget.hasFile).toBeFalsy(); + expect(widget.fileName).toBeUndefined(); + expect(widget.displayText).toBeUndefined(); + }); + + it('should reset local properties', () => { + widget.hasFile = true; + widget.fileName = ''; + widget.displayText = ''; + + widget.reset(); + expect(widget.hasFile).toBeFalsy(); + expect(widget.fileName).toBeNull(); + expect(widget.displayText).toBeNull(); + }); + + it('should reset field value', () => { + widget.field = new FormFieldModel(null, { + type: FormFieldTypes.UPLOAD, + value: [ + { name: 'filename' } + ] + }); + widget.reset(); + expect(widget.field.value).toBeNull(); + expect(widget.field.json.value).toBeNull(); + }); + +}); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/upload/upload.widget.ts b/ng2-components/ng2-activiti-form/src/components/widgets/upload/upload.widget.ts index ffc7c0cd29..788e875074 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/upload/upload.widget.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/upload/upload.widget.ts @@ -29,6 +29,7 @@ export class UploadWidget extends WidgetComponent implements OnInit { hasFile: boolean; fileName: string; + displayText: string; constructor(private settingsService: AlfrescoSettingsService, private authService: AlfrescoAuthenticationService) { @@ -43,18 +44,19 @@ export class UploadWidget extends WidgetComponent implements OnInit { this.hasFile = true; let file = this.field.value[0]; this.fileName = file.name; + this.displayText = decodeURI(file.name); } } - getUploadedFileName(): string { - return decodeURI(this.fileName); - } - reset() { - this.field.value = null; - this.field.json.value = null; this.hasFile = false; this.fileName = null; + this.displayText = null; + + if (this.field) { + this.field.value = null; + this.field.json.value = null; + } } onFileChanged(event: any) { @@ -65,6 +67,7 @@ export class UploadWidget extends WidgetComponent implements OnInit { this.hasFile = true; this.fileName = encodeURI(file.name); + this.displayText = file.name; let formData: FormData = new FormData(); formData.append('file', file, this.fileName);