Fixed unit test

This commit is contained in:
mauriziovitale84
2016-06-21 15:20:35 +01:00
parent 75a533bcde
commit 98d6b5405f
3 changed files with 60 additions and 34 deletions

View File

@@ -0,0 +1,39 @@
/*!
* @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 AlfrescoSettingsServiceMock {
static DEFAULT_HOST_ADDRESS: string = 'fakehost';
static DEFAULT_CONTEXT_PATH: string = '/fake-path-alfresco';
static DEFAULT_BASE_API_PATH: string = '/fake-api/fake-public/fake-alfresco/fake-versions/1';
private _host: string = AlfrescoSettingsServiceMock.DEFAULT_HOST_ADDRESS;
private _contextPath = AlfrescoSettingsServiceMock.DEFAULT_CONTEXT_PATH;
private _apiBasePath: string = AlfrescoSettingsServiceMock.DEFAULT_BASE_API_PATH;
public get host(): string {
return this._host;
}
public set host(value: string) {
this._host = value;
}
getApiBaseUrl(): string {
return this._host + this._contextPath + this._apiBasePath;
}
}

View File

@@ -15,9 +15,12 @@
* limitations under the License.
*/
import { it, describe, beforeEach, expect } from '@angular/core/testing';
import { it, describe, inject, beforeEach, beforeEachProviders } from '@angular/core/testing';
import { UploadService } from './upload.service';
import { provide } from '@angular/core';
import { FileModel } from './../models/file.model';
import { AlfrescoSettingsService } from 'ng2-alfresco-core';
import { AlfrescoSettingsServiceMock } from '../assets/AlfrescoSettingsService.service.mock';
import { AlfrescoApiMock } from '../assets/AlfrescoApi.mock';
@@ -30,6 +33,10 @@ let errorFn = jasmine.createSpy('error');
class MockUploadService extends UploadService {
constructor(settings: AlfrescoSettingsService) {
super(settings);
}
createXMLHttpRequestInstance() {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
@@ -45,9 +52,7 @@ class MockUploadService extends UploadService {
}
describe('AlfrescoUploadService', () => {
let service: MockUploadService,
serviceUpload: UploadService,
options: any;
let service, options: any;
options = {
host: 'fakehost',
@@ -59,12 +64,17 @@ describe('AlfrescoUploadService', () => {
}
};
beforeEach(() => {
window['AlfrescoApi'] = AlfrescoApiMock;
beforeEachProviders(() => [
provide(AlfrescoSettingsService, {useClass: AlfrescoSettingsServiceMock}),
provide(UploadService, {useClass: MockUploadService})
]);
beforeEach( inject([UploadService], (uploadService: UploadService) => {
jasmine.Ajax.install();
window['AlfrescoApi'] = AlfrescoApiMock;
service = new MockUploadService(options);
serviceUpload = new UploadService(options);
});
service = uploadService;
}));
afterEach(() => {
jasmine.Ajax.uninstall();
@@ -74,15 +84,13 @@ describe('AlfrescoUploadService', () => {
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', () => {
it('should show the option passed as input', () => {
service.setOptions(options);
expect(service.getUrl()).toEqual('/some/cool/url');
expect(service.getBaseUrl()).toEqual('fakebasepath');
expect(service.getFormFileds()).toEqual({
siteid: 'fakeSite',
containerid: 'fakeFolder'
@@ -207,15 +215,4 @@ describe('AlfrescoUploadService', () => {
);
});
it('should create an XHR object ', () => {
service.setOptions(options);
let filesFake = {name: 'fake-name', size: 10};
let uploadingFileModel = new FileModel(filesFake);
let xhrRequest = serviceUpload.createXMLHttpRequestInstance(uploadingFileModel, null);
expect(xhrRequest.upload).toBeDefined();
expect(xhrRequest.upload.onabort).toBeDefined();
expect(xhrRequest.upload.onprogress).toBeDefined();
expect(xhrRequest.upload.onerror).toBeDefined();
expect(xhrRequest.onreadystatechange).toBeDefined();
});
});

View File

@@ -33,7 +33,6 @@ declare let AlfrescoApi: any;
*/
@Injectable()
export class UploadService {
private _baseUrlPath: string = '/alfresco/api/-default-/public/alfresco/versions/1';
private _url: string = '/alfresco/service/api/upload';
private _method: string = 'POST';
@@ -66,7 +65,6 @@ 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;
}
@@ -86,14 +84,6 @@ export class UploadService {
return this._url;
}
/**
* Get the base url
* @returns {string}
*/
public getBaseUrl(): string {
return this._baseUrlPath;
}
/**
* Get the form fields
* @returns {Object}
@@ -224,7 +214,7 @@ export class UploadService {
let xmlHttpRequest = this.createXMLHttpRequestInstance(uploadingFileModel, elementEmit);
uploadingFileModel._xmlHttpRequest = xmlHttpRequest;
xmlHttpRequest.open(this._method, this.getHost() + this._url, true);
xmlHttpRequest.open(this._method, this.getHost() + this.getUrl(), true);
let authToken = btoa(basicAuth.username + ':' + basicAuth.password);
if (authToken) {
xmlHttpRequest.setRequestHeader('Authorization', `${basicAuth.type} ${authToken}`);