#46 Fixed unit test

This commit is contained in:
mauriziovitale84
2016-05-24 17:03:48 +01:00
parent bfd04f6475
commit 69c490732d
7 changed files with 117 additions and 46 deletions

View File

@@ -35,6 +35,10 @@ export class TranslationMock {
console.log('mock');
}
addComponent() {
}
public get(key: string|Array<string>, interpolateParams?: Object): Observable<string|any> {
if (!key) {
throw new Error('Parameter "key" required');

View File

@@ -0,0 +1,28 @@
/*!
* @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.
*/
export class UploadServiceMock {
private _baseUrlPath: string = 'fakebaseurlpath';
private _url: string = 'fakeurl';
private _formFields: Object = {};
public setOptions(options: any): void {
this._url = options._url || this._url;
this._baseUrlPath = options.baseUrlPath || this._baseUrlPath;
this._formFields = options.formFields != null ? options.formFields : this._formFields;
}
}

View File

@@ -15,21 +15,38 @@
* limitations under the License.
*/
import { it, describe, expect, injectAsync, TestComponentBuilder } from 'angular2/testing';
import { TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS } from 'angular2/platform/testing/browser';
import { it, describe, expect, injectAsync, beforeEachProviders, TestComponentBuilder, setBaseTestProviders } from 'angular2/testing';
import { provide } from 'angular2/core';
import { FileUploadingDialogComponent } from './file-uploading-dialog.component';
import { FileModel } from '../models/file.model';
import { AlfrescoTranslationService } from 'ng2-alfresco-core/dist/ng2-alfresco-core';
import { TranslationMock } from '../assets/translation.service.mock';
describe('FileUploadDialog', () => {
setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);
beforeEachProviders(() => {
return [
provide(AlfrescoTranslationService, {useClass: TranslationMock})
];
});
it('should render dialog box with css class show', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb
.createAsync(FileUploadingDialogComponent)
.then((fixture) => {
let fileFake = {
id: 'fake-id',
name: 'fake-name'
};
let file = new FileModel(fileFake);
let component = fixture.componentInstance;
fixture.detectChanges();
component.filesUploadingList = [file];
let compiled = fixture.debugElement.nativeElement;
component._showDialog();
component.showDialog();
fixture.detectChanges();
expect(compiled.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog show');

View File

@@ -15,21 +15,21 @@
* limitations under the License.
*/
import { TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS } from 'angular2/platform/testing/browser';
import { it, describe, expect, injectAsync, beforeEachProviders, TestComponentBuilder, setBaseTestProviders } from 'angular2/testing';
import { it, describe, expect, injectAsync, beforeEachProviders, TestComponentBuilder } from 'angular2/testing';
import { provide } from 'angular2/core';
import { UploadButtonComponent } from './upload-button.component';
import { AlfrescoTranslationService } from 'ng2-alfresco-core/dist/ng2-alfresco-core';
import { TranslationMock } from '../assets/translation.service.mock';
import { UploadServiceMock } from '../assets/upload.service.mock';
import { UploadService } from '../services/upload.service';
describe('AlfrescoUploadButton', () => {
setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);
beforeEachProviders(() => {
return [
provide(AlfrescoTranslationService, {useClass: TranslationMock})
provide(AlfrescoTranslationService, {useClass: TranslationMock}),
provide(UploadService, {useClass: UploadServiceMock})
];
});
@@ -38,6 +38,8 @@ describe('AlfrescoUploadButton', () => {
return tcb
.createAsync(UploadButtonComponent)
.then((fixture) => {
let component = fixture.componentInstance;
component.multipleFiles = false;
let compiled = fixture.debugElement.nativeElement;
fixture.detectChanges();
expect(compiled.querySelector('#upload-single-file')).toBeDefined();

View File

@@ -68,7 +68,6 @@ export class UploadDragAreaComponent {
let container = this.getContainerId();
this._uploaderService.setOptions({
fieldName: 'filedata',
formFields: {
siteid: site,
containerid: container

View File

@@ -44,20 +44,18 @@ describe('AlfrescoUploadService', () => {
let service: MockUploadService,
options: any;
options = {
host: 'fakehost',
url: '/some/cool/url',
baseUrlPath: 'fakebasepath',
formFields: {
siteid: 'fakeSite',
containerid: 'fakeFolder'
}
};
beforeEach(() => {
jasmine.Ajax.install();
options = {
url: '/some/cool/url',
withCredentials: true,
authToken: btoa('fakeadmin:fakeadmin'),
authTokenPrefix: 'Basic',
fieldName: 'fakeFileData',
formFields: {
siteid: 'fakeSite',
containerid: 'fakeFolder'
}
};
service = new MockUploadService(options);
});
@@ -65,44 +63,56 @@ describe('AlfrescoUploadService', () => {
jasmine.Ajax.uninstall();
});
it('should show the default option if no method setOption is called', () => {
let empty = {};
service.setOptions(empty);
expect(service.getUrl()).toEqual('/alfresco/service/api/upload');
expect(service.getBaseUrl()).toEqual('/alfresco/api/-default-/public/alfresco/versions/1');
let formFields: Object = {};
expect(service.getFormFileds()).toEqual(formFields);
});
it('should set the basic option an empty queue if no elements are added', () => {
service.setOptions(options);
expect(service.getUrl()).toEqual('/some/cool/url');
expect(service.getBaseUrl()).toEqual('fakebasepath');
expect(service.getFormFileds()).toEqual({
siteid: 'fakeSite',
containerid: 'fakeFolder'
});
});
it('should return an empty queue if no elements are added', () => {
service.setOptions(options);
expect(service.getQueue().length).toEqual(0);
});
it('should add an element in the queue and returns it', () => {
service.setOptions(options);
let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake);
expect(service.getQueue().length).toEqual(1);
});
it('should add two elements in the queue and returns them', () => {
service.setOptions(options);
let filesFake = [{name: 'fake-name', size: 10}, {name: 'fake-name2', size: 20}];
service.addToQueue(filesFake);
expect(service.getQueue().length).toEqual(2);
});
it('should make XHR done request after the file is added in the queue', () => {
let mockUploadSuccessResponses = {
upload: {
success: {
status: 200,
responseText: '{"nodeRef":"workspace://SpacesStore/fake","fileName": "fake-name.png","status":' +
'{"code": 200,"name": "OK","description": "fake file uploaded successfully"}}'
}
}
};
service.setOptions(options);
let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake);
service.uploadFilesInTheQueue('', null);
let request = jasmine.Ajax.requests.mostRecent();
// request.respondWith(mockUploadSuccessResponses.upload.success);
expect(request.url).toBe('/some/cool/url');
expect(request.url).toBe('fakehost/some/cool/url');
expect(request.method).toBe('POST');
// expect(request.data()).toEqual({fileName: 'fake-name.png'});
expect(doneFn).not.toHaveBeenCalled();
console.log(mockUploadSuccessResponses);
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'text/plain',
@@ -112,10 +122,11 @@ describe('AlfrescoUploadService', () => {
});
it('should make XHR error request after an error occur', () => {
service.setOptions(options);
let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake);
service.uploadFilesInTheQueue('', null);
expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url');
expect(jasmine.Ajax.requests.mostRecent().url).toBe('fakehost/some/cool/url');
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 404,
contentType: 'text/plain',
@@ -125,6 +136,7 @@ describe('AlfrescoUploadService', () => {
});
it('should make XHR abort request after the xhr abort is called', () => {
service.setOptions(options);
let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake);
service.uploadFilesInTheQueue('', null);
@@ -134,12 +146,13 @@ describe('AlfrescoUploadService', () => {
});
it('should make XHR done request after the file is upload', () => {
service.setOptions(options);
let filesFake = {name: 'fake-name', size: 10};
let uploadingFileModel = new FileModel(filesFake);
service.uploadFile(uploadingFileModel, '', null);
expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url');
expect(jasmine.Ajax.requests.mostRecent().url).toBe('fakehost/some/cool/url');
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'text/plain',
@@ -147,5 +160,4 @@ describe('AlfrescoUploadService', () => {
});
expect(doneFn).toHaveBeenCalledWith('Single File uploaded');
});
});

View File

@@ -60,6 +60,7 @@ export class UploadService {
*
*/
public setOptions(options: any): void {
this._url = options.url || this._url;
this._baseUrlPath = options.baseUrlPath || this._baseUrlPath;
this._formFields = options.formFields != null ? options.formFields : this._formFields;
}
@@ -68,24 +69,32 @@ export class UploadService {
* Get the host
* @returns {string}
*/
public get host(): string {
public getHost(): string {
return this._host;
}
/**
* Set the host
* @param value
* Get the url
* @returns {string}
*/
public set host(value: string) {
this._host = value;
public getUrl(): string {
return this._url;
}
/**
* Get the base url
* @returns {string}
*/
private getBaseUrl(): string {
return this._host + this._baseUrlPath;
public getBaseUrl(): string {
return this._baseUrlPath;
}
/**
* Get the form fields
* @returns {Object}
*/
public getFormFileds(): Object {
return this._formFields;
}
/**
@@ -102,7 +111,6 @@ export class UploadService {
*/
private getAlfrescoClient() {
let defaultClient = new AlfrescoApi.ApiClient();
defaultClient.basePath = this.getBaseUrl();
// Configure HTTP basic authorization: basicAuth
let basicAuth = defaultClient.authentications['basicAuth'];
@@ -206,6 +214,7 @@ export class UploadService {
form.append('uploaddirectory', directory);
let xmlHttpRequest = this.createXMLHttpRequestInstance(uploadingFileModel, elementEmit);
uploadingFileModel._xmlHttpRequest = xmlHttpRequest;
xmlHttpRequest.open(this._method, this._host + this._url, true);
let authToken = btoa(basicAuth.username + ':' + basicAuth.password);