mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-26 17:24:56 +00:00
Tests for Upload widget
This commit is contained in:
parent
d179accca7
commit
cf287abdc1
@ -3,7 +3,7 @@
|
||||
<label class="upload-widget__label" [attr.for]="field.id">{{field.name}}</label>
|
||||
<div>
|
||||
<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"
|
||||
#file
|
||||
type="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();
|
||||
});
|
||||
|
||||
});
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user