#62 uploader test and coverage

This commit is contained in:
Mario Romano
2016-05-13 12:13:01 +01:00
parent 42b8e4e207
commit e0978c2a13
19 changed files with 442 additions and 107 deletions

View File

@@ -0,0 +1,40 @@
/*!
* @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 { Observable } from 'rxjs/Observable';
import { EventEmitter } from 'angular2/core';
import { LangChangeEvent } from 'ng2-translate/ng2-translate';
export class TranslationMock {
public onLangChange: EventEmitter<LangChangeEvent> = new EventEmitter<LangChangeEvent>();
setDefaultLang() {
console.log('mock');
}
use() {
console.log('mock');
}
public get(key: string|Array<string>, interpolateParams?: Object): Observable<string|any> {
if (!key) {
throw new Error('Parameter "key" required');
}
return Observable.of(key);
}
}

View File

@@ -0,0 +1,38 @@
/*!
* @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 { it, describe, expect, injectAsync, TestComponentBuilder } from 'angular2/testing';
import { FileUploadingDialogComponent } from '../../src/components/file-uploading-dialog.component';
describe('FileUploadDialog', () => {
it('should render dialog box with css class show', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb
.createAsync(FileUploadingDialogComponent)
.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');
});
}));
});

View File

@@ -0,0 +1,105 @@
/*!
* @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, beforeEachProviders, TestComponentBuilder, setBaseTestProviders } from 'angular2/testing';
import { provide } from 'angular2/core';
import { UploadButtonComponent } from '../../src/components/upload-button.component';
import { TranslateService } from 'ng2-translate/ng2-translate';
import { TranslationMock } from '../assets/translation.service.mock';
describe('AlfrescoUploadButton', () => {
setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);
beforeEachProviders(() => {
return [
provide(TranslateService, {useClass: TranslationMock})
];
});
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');
});
}));
});

View File

@@ -0,0 +1,137 @@
/*!
* @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 { it, describe, beforeEach, expect } from 'angular2/testing';
import { UploadService } from '../../src/services/upload.service';
import { FileModel } from '../../src/models/file.model';
declare let jasmine: any;
describe('AlfrescoUploadService', () => {
let service: UploadService,
options: any,
xhr: XMLHttpRequest,
doneFn: any,
errorFn: any;
beforeEach(() => {
jasmine.Ajax.install();
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: '/some/cool/url',
withCredentials: true,
authToken: btoa('fakeadmin:fakeadmin'),
authTokenPrefix: 'Basic',
fieldName: 'fakeFileData',
formFields: {
siteid: 'fakeSite',
containerid: 'fakeFolder'
}
};
service = new UploadService(options);
});
afterEach(() => {
jasmine.Ajax.uninstall();
});
it('should return an empty queue if no elements are added', () => {
expect(service.getQueue().length).toEqual(0);
});
it('should add an element in the queue and returns it', () => {
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', () => {
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);
service.uploadFilesInTheQueue('', null);
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);
service.uploadFilesInTheQueue('', null);
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);
service.uploadFilesInTheQueue('', null);
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, '', null);
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');
});
});