Tests for Upload widget

This commit is contained in:
Denys Vuika 2016-10-13 13:53:47 +01:00
parent d179accca7
commit cf287abdc1
3 changed files with 94 additions and 7 deletions

View File

@ -3,7 +3,7 @@
<label class="upload-widget__label" [attr.for]="field.id">{{field.name}}</label> <label class="upload-widget__label" [attr.for]="field.id">{{field.name}}</label>
<div> <div>
<i *ngIf="hasFile" class="material-icons upload-widget__icon">attachment</i> <i *ngIf="hasFile" class="material-icons upload-widget__icon">attachment</i>
<span *ngIf="hasFile" class="upload-widget__file">{{getUploadedFileName()}}</span> <span *ngIf="hasFile" class="upload-widget__file">{{displayText}}</span>
<input *ngIf="!hasFile" <input *ngIf="!hasFile"
#file #file
type="file" type="file"

View File

@ -0,0 +1,84 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { UploadWidget } from './upload.widget';
import { AlfrescoSettingsService, AlfrescoAuthenticationService, AlfrescoApiService } from 'ng2-alfresco-core';
import { FormFieldModel } from './../core/form-field.model';
import { FormFieldTypes } from '../core/form-field-types';
describe('UploadWidget', () => {
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 = '<fileName>';
widget.displayText = '<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();
});
});

View File

@ -29,6 +29,7 @@ export class UploadWidget extends WidgetComponent implements OnInit {
hasFile: boolean; hasFile: boolean;
fileName: string; fileName: string;
displayText: string;
constructor(private settingsService: AlfrescoSettingsService, constructor(private settingsService: AlfrescoSettingsService,
private authService: AlfrescoAuthenticationService) { private authService: AlfrescoAuthenticationService) {
@ -43,18 +44,19 @@ export class UploadWidget extends WidgetComponent implements OnInit {
this.hasFile = true; this.hasFile = true;
let file = this.field.value[0]; let file = this.field.value[0];
this.fileName = file.name; this.fileName = file.name;
this.displayText = decodeURI(file.name);
} }
} }
getUploadedFileName(): string {
return decodeURI(this.fileName);
}
reset() { reset() {
this.field.value = null;
this.field.json.value = null;
this.hasFile = false; this.hasFile = false;
this.fileName = null; this.fileName = null;
this.displayText = null;
if (this.field) {
this.field.value = null;
this.field.json.value = null;
}
} }
onFileChanged(event: any) { onFileChanged(event: any) {
@ -65,6 +67,7 @@ export class UploadWidget extends WidgetComponent implements OnInit {
this.hasFile = true; this.hasFile = true;
this.fileName = encodeURI(file.name); this.fileName = encodeURI(file.name);
this.displayText = file.name;
let formData: FormData = new FormData(); let formData: FormData = new FormData();
formData.append('file', file, this.fileName); formData.append('file', file, this.fileName);