diff --git a/ng2-components/ng2-alfresco-upload/package.json b/ng2-components/ng2-alfresco-upload/package.json index cff25c8146..29cd07eeb6 100644 --- a/ng2-components/ng2-alfresco-upload/package.json +++ b/ng2-components/ng2-alfresco-upload/package.json @@ -45,6 +45,7 @@ }, "devDependencies": { "coveralls": "^2.11.9", + "jasmine-ajax": "^3.2.0", "jasmine-core": "2.4.1", "license-check": "^1.0.4", "live-server": "^0.9.2", diff --git a/ng2-components/ng2-alfresco-upload/src/services/upload.service.d.ts b/ng2-components/ng2-alfresco-upload/src/services/upload.service.d.ts index 740ad32935..58a028abbc 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/upload.service.d.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/upload.service.d.ts @@ -30,6 +30,7 @@ export declare class UploadService { private _fieldName; private _formFields; private _withCredentials; + private _xmlHttpRequest; private _queue; constructor(options: any); /** @@ -44,6 +45,10 @@ export declare class UploadService { * Pick all the files in the queue that are not been uploaded yet and upload it. */ private _uploadFilesInTheQueue(); + /** + * The method create a new XMLHttpRequest instance if doesn't exist + */ + private _configureXMLHttpRequest(); /** * Upload a file, and enrich it with the xhr. * @@ -63,4 +68,9 @@ export declare class UploadService { * @return {boolean} */ private _isFile(file); + /** + * Set XMLHttpRequest method + * @param xhr + */ + setXMLHttpRequest(xhr: XMLHttpRequest): void; } diff --git a/ng2-components/ng2-alfresco-upload/src/services/upload.service.js b/ng2-components/ng2-alfresco-upload/src/services/upload.service.js index 9e7fbed6de..b9a2284715 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/upload.service.js +++ b/ng2-components/ng2-alfresco-upload/src/services/upload.service.js @@ -82,6 +82,36 @@ System.register(['../models/file.model'], function(exports_1, context_1) { }); }; ; + /** + * The method create a new XMLHttpRequest instance if doesn't exist + */ + UploadService.prototype._configureXMLHttpRequest = function () { + var _this = this; + if (this._xmlHttpRequest == undefined) { + this._xmlHttpRequest = new XMLHttpRequest(); + this._xmlHttpRequest.upload.onprogress = function (e) { + if (e.lengthComputable) { + var percent = Math.round(e.loaded / e.total * 100); + uploadingFileModel.setProgres({ + total: e.total, + loaded: e.loaded, + percent: percent + }); + } + }; + this._xmlHttpRequest.upload.onabort = function (e) { + uploadingFileModel.setAbort(); + }; + this._xmlHttpRequest.upload.onerror = function (e) { + uploadingFileModel.setError(); + }; + this._xmlHttpRequest.onreadystatechange = function () { + if (_this._xmlHttpRequest.readyState === XMLHttpRequest.DONE) { + uploadingFileModel.onFinished(_this._xmlHttpRequest.status, _this._xmlHttpRequest.statusText, _this._xmlHttpRequest.response); + } + }; + } + }; /** * Upload a file, and enrich it with the xhr. * @@ -95,35 +125,14 @@ System.register(['../models/file.model'], function(exports_1, context_1) { Object.keys(this._formFields).forEach(function (key) { form.append(key, _this._formFields[key]); }); - var xmlHttpRequest = new XMLHttpRequest(); - uploadingFileModel.setXMLHttpRequest(xmlHttpRequest); - xmlHttpRequest.upload.onprogress = function (e) { - if (e.lengthComputable) { - var percent = Math.round(e.loaded / e.total * 100); - uploadingFileModel.setProgres({ - total: e.total, - loaded: e.loaded, - percent: percent - }); - } - }; - xmlHttpRequest.upload.onabort = function (e) { - uploadingFileModel.setAbort(); - }; - xmlHttpRequest.upload.onerror = function (e) { - uploadingFileModel.setError(); - }; - xmlHttpRequest.onreadystatechange = function () { - if (xmlHttpRequest.readyState === XMLHttpRequest.DONE) { - uploadingFileModel.onFinished(xmlHttpRequest.status, xmlHttpRequest.statusText, xmlHttpRequest.response); - } - }; - xmlHttpRequest.open(this._method, this._url, true); - xmlHttpRequest.withCredentials = this._withCredentials; + this._configureXMLHttpRequest(); + uploadingFileModel.setXMLHttpRequest(this._xmlHttpRequest); + this._xmlHttpRequest.open(this._method, this._url, true); + this._xmlHttpRequest.withCredentials = this._withCredentials; if (this._authToken) { - xmlHttpRequest.setRequestHeader('Authorization', this._authTokenPrefix + " " + this._authToken); + this._xmlHttpRequest.setRequestHeader('Authorization', this._authTokenPrefix + " " + this._authToken); } - xmlHttpRequest.send(form); + this._xmlHttpRequest.send(form); }; /** * Return all the files in the uploading queue. @@ -141,6 +150,13 @@ System.register(['../models/file.model'], function(exports_1, context_1) { UploadService.prototype._isFile = function (file) { return file !== null && (file instanceof Blob || (file.name && file.size)); }; + /** + * Set XMLHttpRequest method + * @param xhr + */ + UploadService.prototype.setXMLHttpRequest = function (xhr) { + this._xmlHttpRequest = xhr; + }; return UploadService; }()); exports_1("UploadService", UploadService); diff --git a/ng2-components/ng2-alfresco-upload/src/services/upload.service.js.map b/ng2-components/ng2-alfresco-upload/src/services/upload.service.js.map index ee714fbe2a..6da509662e 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/upload.service.js.map +++ b/ng2-components/ng2-alfresco-upload/src/services/upload.service.js.map @@ -1 +1 @@ -{"version":3,"file":"upload.service.js","sourceRoot":"","sources":["upload.service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;;;;;;;;;;;;YAMH;;;;;eAKG;YACH;gBAWI,uBAAoB,OAAY;oBAAZ,YAAO,GAAP,OAAO,CAAK;oBATxB,YAAO,GAAW,MAAM,CAAC;oBACzB,qBAAgB,GAAW,OAAO,CAAC;oBACnC,eAAU,GAAW,SAAS,CAAC;oBAC/B,eAAU,GAAW,MAAM,CAAC;oBAC5B,gBAAW,GAAW,EAAE,CAAC;oBAGzB,WAAM,GAAgB,EAAE,CAAC;oBAG7B,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;oBAEzC,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,eAAe,IAAI,IAAI,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,gBAAgB,CAAC;oBAC1G,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,GAAG,IAAI,IAAI,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC;oBAC1D,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,eAAe,IAAI,IAAI,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,C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AC,MAAM,CAAC,GAAG,EAAE,KAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;oBAC5C,CAAC,CAAC,CAAC;oBAEH,IAAI,CAAC,wBAAwB,EAAE,CAAC;oBAChC,kBAAkB,CAAC,iBAAiB,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;oBAE3D,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;oBACzD,IAAI,CAAC,eAAe,CAAC,eAAe,GAAG,IAAI,CAAC,gBAAgB,CAAC;oBAE7D,EAAE,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;wBAClB,IAAI,CAAC,eAAe,CAAC,gBAAgB,CAAC,eAAe,EAAK,IAAI,CAAC,gBAAgB,SAAI,IAAI,CAAC,UAAY,CAAC,CAAC;oBAC1G,CAAC;oBAED,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACpC,CAAC;gBAED;;;;mBAIG;gBACH,gCAAQ,GAAR;oBACI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;gBACvB,CAAC;gBAED;;;;mBAIG;gBACK,+BAAO,GAAf,UAAgB,IAAS;oBACrB,MAAM,CAAC,IAAI,KAAK,IAAI,IAAI,CAAC,IAAI,YAAY,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;gBAC/E,CAAC;gBAED;;;mBAGG;gBACI,yCAAiB,GAAxB,UAAyB,GAAmB;oBACxC,IAAI,CAAC,eAAe,GAAG,GAAG,CAAC;gBAC/B,CAAC;gBACL,oBAAC;YAAD,CAAC,AAlJD,IAkJC;YAlJD,yCAkJC,CAAA"} \ No newline at end of file diff --git a/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts b/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts index 6798840f39..07a4301386 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts @@ -33,6 +33,7 @@ export class UploadService { private _fieldName: string = 'file'; private _formFields: Object = {}; private _withCredentials: boolean; + private _xmlHttpRequest: XMLHttpRequest; private _queue: FileModel[] = []; @@ -82,6 +83,43 @@ export class UploadService { }); }; + /** + * The method create a new XMLHttpRequest instance if doesn't exist + */ + private _configureXMLHttpRequest() { + if (this._xmlHttpRequest == undefined) { + this._xmlHttpRequest = new XMLHttpRequest(); + this._xmlHttpRequest.upload.onprogress = (e) => { + if (e.lengthComputable) { + let percent = Math.round(e.loaded / e.total * 100); + uploadingFileModel.setProgres({ + total: e.total, + loaded: e.loaded, + percent: percent + }); + } + }; + + this._xmlHttpRequest.upload.onabort = (e) => { + uploadingFileModel.setAbort(); + }; + + this._xmlHttpRequest.upload.onerror = (e) => { + uploadingFileModel.setError(); + }; + + this._xmlHttpRequest.onreadystatechange = () => { + if (this._xmlHttpRequest.readyState === XMLHttpRequest.DONE) { + uploadingFileModel.onFinished( + this._xmlHttpRequest.status, + this._xmlHttpRequest.statusText, + this._xmlHttpRequest.response + ); + } + }; + } + } + /** * Upload a file, and enrich it with the xhr. * @@ -95,46 +133,17 @@ export class UploadService { form.append(key, this._formFields[key]); }); - let xmlHttpRequest = new XMLHttpRequest(); - uploadingFileModel.setXMLHttpRequest(xmlHttpRequest); + this._configureXMLHttpRequest(); + uploadingFileModel.setXMLHttpRequest(this._xmlHttpRequest); - xmlHttpRequest.upload.onprogress = (e) => { - if (e.lengthComputable) { - let percent = Math.round(e.loaded / e.total * 100); - uploadingFileModel.setProgres({ - total: e.total, - loaded: e.loaded, - percent: percent - }); - } - }; - - xmlHttpRequest.upload.onabort = (e) => { - uploadingFileModel.setAbort(); - }; - - xmlHttpRequest.upload.onerror = (e) => { - uploadingFileModel.setError(); - }; - - xmlHttpRequest.onreadystatechange = () => { - if (xmlHttpRequest.readyState === XMLHttpRequest.DONE) { - uploadingFileModel.onFinished( - xmlHttpRequest.status, - xmlHttpRequest.statusText, - xmlHttpRequest.response - ); - } - }; - - xmlHttpRequest.open(this._method, this._url, true); - xmlHttpRequest.withCredentials = this._withCredentials; + this._xmlHttpRequest.open(this._method, this._url, true); + this._xmlHttpRequest.withCredentials = this._withCredentials; if (this._authToken) { - xmlHttpRequest.setRequestHeader('Authorization', `${this._authTokenPrefix} ${this._authToken}`); + this._xmlHttpRequest.setRequestHeader('Authorization', `${this._authTokenPrefix} ${this._authToken}`); } - xmlHttpRequest.send(form); + this._xmlHttpRequest.send(form); } /** @@ -154,4 +163,12 @@ export class UploadService { private _isFile(file: any): boolean { return file !== null && (file instanceof Blob || (file.name && file.size)); } + + /** + * Set XMLHttpRequest method + * @param xhr + */ + public setXMLHttpRequest(xhr: XMLHttpRequest) { + this._xmlHttpRequest = xhr; + } } diff --git a/ng2-components/ng2-alfresco-upload/test/components/upload-button.component.spec.ts b/ng2-components/ng2-alfresco-upload/test/components/upload-button.component.spec.ts new file mode 100644 index 0000000000..2f7d9057ee --- /dev/null +++ b/ng2-components/ng2-alfresco-upload/test/components/upload-button.component.spec.ts @@ -0,0 +1,94 @@ +/** + * @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 {TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS} from 'angular2/platform/testing/browser'; +import {it, describe, expect, injectAsync, TestComponentBuilder, setBaseTestProviders} from 'angular2/testing'; +import {Component, provide, Injector} from 'angular2/core'; +import {UploadButtonComponent} from '../../../src/components/upload-button.component'; + +describe('AlfrescoUploadButton', () => { + + setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS); + + it('should render upload-single-file button as default', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => { + return tcb + .createAsync(UploadButtonComponent) + .then((fixture) => { + let compiled = fixture.debugElement.nativeElement; + fixture.detectChanges(); + expect(compiled.querySelector('#upload-single-file')).toBeDefined(); + }); + })); + + it('should render upload-multiple-file button if multipleFiles is true', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => { + return tcb + .createAsync(UploadButtonComponent) + .then((fixture) => { + let component = fixture.componentInstance; + component.multipleFiles = true; + let compiled = fixture.debugElement.nativeElement; + fixture.detectChanges(); + expect(compiled.querySelector('#upload-multiple-files')).toBeDefined(); + }); + })); + + it('should render an uploadFolder button if uploadFolder is true', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => { + return tcb + .createAsync(UploadButtonComponent) + .then((fixture) => { + let component = fixture.componentInstance; + component.uploadFolder = true; + let compiled = fixture.debugElement.nativeElement; + fixture.detectChanges(); + expect(compiled.querySelector('#uploadFolder')).toBeDefined(); + }); + })); + + it('should call onFilesAdded method', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => { + return tcb + .createAsync(UploadButtonComponent) + .then((fixture) => { + let component = fixture.componentInstance; + component.onFilesAdded = jasmine.createSpy('onFilesAdded'); + + fixture.detectChanges(); + + let fakeEvent = { + currentTarget: {files: [{name: 'fake-name', size: 10}]} + }; + + component.onFilesAdded(fakeEvent); + expect(component.onFilesAdded).toHaveBeenCalledWith(fakeEvent); + }); + })); + + it('should render dialog box with css class show ', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => { + return tcb + .createAsync(UploadButtonComponent) + .then((fixture) => { + let component = fixture.componentInstance; + fixture.detectChanges(); + let compiled = fixture.debugElement.nativeElement; + + component._showDialog(); + fixture.detectChanges(); + expect(compiled.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog show'); + }); + })); +}); + diff --git a/ng2-components/ng2-alfresco-upload/test/index.html b/ng2-components/ng2-alfresco-upload/test/index.html index c6030e01cb..33f43a9724 100644 --- a/ng2-components/ng2-alfresco-upload/test/index.html +++ b/ng2-components/ng2-alfresco-upload/test/index.html @@ -8,6 +8,7 @@ + @@ -34,7 +35,8 @@ // #3. Import the spec file explicitly Promise.all([ - System.import('test/services/upload.service.spec') + System.import('test/services/upload.service.spec'), + System.import('test/components/upload-button.component.spec') ]) // #4. wait for all imports to load ... // then re-execute `window.onload` which diff --git a/ng2-components/ng2-alfresco-upload/test/services/upload.service.spec.ts b/ng2-components/ng2-alfresco-upload/test/services/upload.service.spec.ts index 8fb588b12f..bd24df0ffa 100644 --- a/ng2-components/ng2-alfresco-upload/test/services/upload.service.spec.ts +++ b/ng2-components/ng2-alfresco-upload/test/services/upload.service.spec.ts @@ -20,28 +20,32 @@ import {provide, Injector} from 'angular2/core'; import {Http, HTTP_PROVIDERS, XHRBackend} from 'angular2/http'; import {MockBackend} from 'angular2/http/testing'; import {UploadService} from '../../src/services/upload.service'; +import {FileModel} from '../../src/models/file.model'; describe('AlfrescoUploadService', () => { - let injector, - backend, - mockBackend, - httpService, - service, - options; + let service, + options, + xhr, + doneFn, + errorFn; beforeEach(() => { - injector = Injector.resolveAndCreate([ - HTTP_PROVIDERS, - MockBackend, - provide(XHRBackend, {useClass: MockBackend}) - ]); + jasmine.Ajax.install(); - mockBackend = injector.get(MockBackend); - backend = injector.get(XHRBackend); - httpService = injector.get(Http); + doneFn = jasmine.createSpy("success"); + errorFn = jasmine.createSpy("error"); + xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function () { + if (this.readyState == this.DONE && this.status == 200) { + doneFn(this.responseText); + } else if (this.readyState == this.DONE && this.status == 404) { + errorFn(this.responseText); + } + }; + xhr.abort = jasmine.createSpy('abort'); options = { - url: 'http://mockUpload', + url: '/some/cool/url', withCredentials: true, authToken: btoa('fakeadmin:fakeadmin'), authTokenPrefix: 'Basic', @@ -54,27 +58,82 @@ describe('AlfrescoUploadService', () => { service = new UploadService(options); }); - it('should make XHR request', () => { + afterEach(() => { + jasmine.Ajax.uninstall(); + }); - let xhr = { - open: jasmine.createSpy('open'), - upload: jasmine.createSpy('upload'), - send: jasmine.createSpy('send'), - setRequestHeader: jasmine.createSpy('setRequestHeader'), - onprogress : jasmine.createSpy('onprogress') - }; + it('should return an empty queue if no elements are added', () => { + expect(service.getQueue().length).toEqual(0); + }); - XMLHttpRequest = jasmine.createSpy('XMLHttpRequest'); - XMLHttpRequest.and.callFake(function () { - return xhr; - }); + it('should add an element in the queue and returns it', () => { + service.setXMLHttpRequest(xhr); + 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.setXMLHttpRequest(xhr); + 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', () => { + service.setXMLHttpRequest(xhr); let filesFake = [{name: 'fake-name', size: 10}]; service.addToQueue(filesFake); - expect(xhr.open).toHaveBeenCalled(); + expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url'); + expect(doneFn).not.toHaveBeenCalled(); + jasmine.Ajax.requests.mostRecent().respondWith({ + "status": 200, + contentType: 'text/plain', + responseText: 'File uploaded' + }); + expect(doneFn).toHaveBeenCalledWith('File uploaded'); }); + it('should make XHR error request after an error occur', () => { + service.setXMLHttpRequest(xhr); + let filesFake = [{name: 'fake-name', size: 10}]; + service.addToQueue(filesFake); + expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url'); + expect(doneFn).not.toHaveBeenCalled(); + jasmine.Ajax.requests.mostRecent().respondWith({ + "status": 404, + contentType: 'text/plain', + responseText: 'Error file uploaded' + }); + expect(errorFn).toHaveBeenCalledWith('Error file uploaded'); + }); + + it('should make XHR abort request after the xhr abort is called', () => { + service.setXMLHttpRequest(xhr); + let filesFake = [{name: 'fake-name', size: 10}]; + service.addToQueue(filesFake); + let file = service.getQueue(); + file[0].setAbort(); + expect(xhr.abort).toHaveBeenCalled(); + }); + + it('should make XHR done request after the file is upload', () => { + service.setXMLHttpRequest(xhr); + let filesFake = {name: 'fake-name', size: 10}; + + let uploadingFileModel = new FileModel(filesFake) + service.uploadFile(uploadingFileModel); + + expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url'); + expect(doneFn).not.toHaveBeenCalled(); + jasmine.Ajax.requests.mostRecent().respondWith({ + "status": 200, + contentType: 'text/plain', + responseText: 'File uploaded' + }); + expect(doneFn).toHaveBeenCalledWith('File uploaded'); + }); });