From 362e2e32eac9742a449a842e8f91fe99fd49554f Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Thu, 6 Oct 2016 16:18:03 +0100 Subject: [PATCH] fix test and coverage Angular2 Final release uploader component #749 --- .../tag-node-list.component.spec.ts | 1 - .../src/services/tag.service.spec.ts | 1 + ng2-components/ng2-alfresco-upload/index.ts | 4 +- .../ng2-alfresco-upload/karma.conf.js | 15 +- .../ng2-alfresco-upload/package.json | 9 +- .../file-uploading-dialog.component.spec.ts | 162 +++----- .../file-uploading-dialog.component.ts | 2 +- .../upload-button.component.spec.ts | 103 +++--- .../src/components/upload-button.component.ts | 2 +- .../upload-drag-area.component.spec.ts | 86 ++--- .../components/upload-drag-area.component.ts | 10 +- .../file-draggable.directive.spec.ts | 7 +- .../src/services/upload.service.spec.ts | 348 +++++++++--------- 13 files changed, 351 insertions(+), 399 deletions(-) diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts index 84bca5aea9..0cd1ce2654 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts @@ -30,7 +30,6 @@ declare let jasmine: any; describe('Test ng2-alfresco-tag Tag relative node list', () => { - let dataTag = { 'list': { 'pagination': { diff --git a/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts b/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts index 637d568e98..d7f4b9c8be 100644 --- a/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts @@ -24,6 +24,7 @@ import { import { TagService } from '../services/tag.service'; declare let jasmine: any; +declare var AlfrescoApi: any; describe('Tag service', () => { diff --git a/ng2-components/ng2-alfresco-upload/index.ts b/ng2-components/ng2-alfresco-upload/index.ts index 30228bdda3..7209322ad7 100644 --- a/ng2-components/ng2-alfresco-upload/index.ts +++ b/ng2-components/ng2-alfresco-upload/index.ts @@ -24,6 +24,7 @@ import { UploadButtonComponent } from './src/components/upload-button.component' import { FileUploadingDialogComponent } from './src/components/file-uploading-dialog.component'; import { FileUploadingListComponent } from './src/components/file-uploading-list.component'; import { UploadService } from './src/services/upload.service'; +import { TranslateModule } from 'ng2-translate'; /** * ng2-alfresco-upload, provide components to upload files to alfresco repository. @@ -62,7 +63,8 @@ export const UPLOAD_PROVIDERS: any[] = [ @NgModule({ imports: [ - CoreModule + CoreModule, + TranslateModule.forRoot() ], declarations: [ ...UPLOAD_DIRECTIVES diff --git a/ng2-components/ng2-alfresco-upload/karma.conf.js b/ng2-components/ng2-alfresco-upload/karma.conf.js index 8f3adc54b4..7d567ca9db 100644 --- a/ng2-components/ng2-alfresco-upload/karma.conf.js +++ b/ng2-components/ng2-alfresco-upload/karma.conf.js @@ -24,8 +24,8 @@ module.exports = function (config) { 'node_modules/zone.js/dist/fake-async-test.js', // RxJs - { pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false }, - { pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false }, + {pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false}, + {pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false}, // Paths loaded via module imports: // Angular itself @@ -33,6 +33,7 @@ module.exports = function (config) { {pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false}, 'node_modules/alfresco-js-api/dist/alfresco-js-api.js', + {pattern: 'node_modules/ng2-translate/**/*.js', included: false, watched: false}, {pattern: 'node_modules/ng2-translate/**/*.js.map', included: false, watched: false}, @@ -44,11 +45,14 @@ module.exports = function (config) { {pattern: 'dist/**/*.css', included: true, served: true, watched: true}, // ng2-components - { pattern: 'node_modules/ng2-alfresco-core/dist/**/*.js', included: false, served: true, watched: false }, + {pattern: 'node_modules/ng2-alfresco-core/dist/**/*.js', included: false, served: true, watched: false}, + {pattern: 'node_modules/ng2-alfresco-core/dist/**/*.map', included: false, served: true, watched: false}, // paths to support debugging with source maps in dev tools {pattern: 'src/**/*.ts', included: false, watched: false}, - {pattern: 'dist/**/*.js.map', included: false, watched: false} + {pattern: 'dist/**/*.js.map', included: false, watched: false}, + + {pattern: 'src/i18n/*.json', included: false, watched: true, served: true} ], exclude: [ @@ -96,11 +100,10 @@ module.exports = function (config) { // Source files that you wanna generate coverage for. // Do not include tests or libraries (these files will be instrumented by Istanbul) preprocessors: { - 'dist/**/!(*spec|index|*mock|*model).js': 'coverage' + 'dist/**/!(*spec).js': ['coverage'] }, coverageReporter: { - includeAllSources: true, dir: 'coverage/', subdir: 'report', reporters: [ diff --git a/ng2-components/ng2-alfresco-upload/package.json b/ng2-components/ng2-alfresco-upload/package.json index 093fd0e4fd..2096d20763 100644 --- a/ng2-components/ng2-alfresco-upload/package.json +++ b/ng2-components/ng2-alfresco-upload/package.json @@ -63,15 +63,14 @@ "@angular/router": "3.0.0", "@angular/upgrade": "2.0.0", "@types/node": "^6.0.42", + "alfresco-js-api": "^0.3.0", "core-js": "^2.4.1", + "ng2-alfresco-core": "0.3.2", + "ng2-translate": "2.5.0", "reflect-metadata": "^0.1.3", "rxjs": "5.0.0-beta.12", "systemjs": "0.19.27", - "zone.js": "^0.6.23", - - "ng2-translate": "2.5.0", - "alfresco-js-api": "^0.3.0", - "ng2-alfresco-core": "0.3.2" + "zone.js": "^0.6.23" }, "devDependencies": { "@types/core-js": "^0.9.32", diff --git a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.spec.ts b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.spec.ts index a613517b59..c1d11ca8ac 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.spec.ts +++ b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.spec.ts @@ -15,153 +15,107 @@ * limitations under the License. */ -/* -import { PLATFORM_PIPES } from '@angular/core'; -import { describe, expect, it, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; -import { TestComponentBuilder } from '@angular/compiler/testing'; -import { Observable } from 'rxjs/Observable'; -import { HTTP_PROVIDERS } from '@angular/http'; - -import { - AlfrescoTranslationService, - AlfrescoSettingsService, - AlfrescoAuthenticationService, - AlfrescoApiService, - AlfrescoPipeTranslate -} from 'ng2-alfresco-core'; +import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { FileUploadingDialogComponent } from './file-uploading-dialog.component'; -import { FileModel } from '../models/file.model'; -import { TranslationMock } from '../assets/translation.service.mock'; +import { FileUploadingListComponent } from './file-uploading-list.component'; +import { DebugElement, ReflectiveInjector } from '@angular/core'; +import { + AlfrescoAuthenticationService, + AlfrescoSettingsService, + AlfrescoApiService, + CoreModule +} from 'ng2-alfresco-core'; import { UploadService } from '../services/upload.service'; +import { FileModel } from '../models/file.model'; -describe('FileUploadDialog', () => { +describe('Test ng2-alfresco-upload FileUploadDialog', () => { - let componentFixture; - let uploadService; + let injector; + let component: any; + let fixture: ComponentFixture; + let debug: DebugElement; + let element: any; + let file: FileModel; - beforeEachProviders(() => { - return [ - HTTP_PROVIDERS, - AlfrescoApiService, - AlfrescoSettingsService, - AlfrescoAuthenticationService, - { provide: PLATFORM_PIPES, useValue: AlfrescoPipeTranslate, multi: true }, - { provide: AlfrescoTranslationService, useClass: TranslationMock }, + beforeEach(async(() => { + injector = ReflectiveInjector.resolveAndCreate([ UploadService - ]; - }); + ]); - beforeEach( - inject( - [TestComponentBuilder, UploadService], - (tcb: TestComponentBuilder, service: UploadService) => { - return tcb - .createAsync(FileUploadingDialogComponent) - .then(fixture => { - componentFixture = fixture; - uploadService = service; - }); - } - ) - ); + TestBed.configureTestingModule({ + imports: [ + CoreModule + ], + declarations: [FileUploadingDialogComponent, FileUploadingListComponent], + providers: [ + AlfrescoSettingsService, + AlfrescoAuthenticationService, + AlfrescoApiService, + UploadService + ] + }).compileComponents(); + })); + + beforeEach(() => { + window['componentHandler'] = null; - it('should render completed upload 1 when an element is added to Observer', () => { let fileFake = { id: 'fake-id', name: 'fake-name' }; - let file = new FileModel(fileFake); - file.progress = {'percent': 50}; + file = new FileModel(fileFake); - uploadService.totalCompleted$ = new Observable(observer => { - observer.next(1); - }); + fixture = TestBed.createComponent(FileUploadingDialogComponent); + + debug = fixture.debugElement; + element = fixture.nativeElement; + component = fixture.componentInstance; - let component = componentFixture.componentInstance; - componentFixture.detectChanges(); component.filesUploadingList = [file]; + fixture.detectChanges(); + }); - let compiled = componentFixture.debugElement.nativeElement; - componentFixture.detectChanges(); + it('should render completed upload 1 when an element is added to Observer', () => { + component._uploaderService.updateFileCounterStream(1); + fixture.detectChanges(); - expect(compiled.querySelector('#total-upload-completed').innerText).toEqual('1'); + expect(element.querySelector('#total-upload-completed').innerText).toEqual('1'); }); it('should render dialog box with css class show when an element is added to Observer', () => { - let fileFake = { - id: 'fake-id', - name: 'fake-name' - }; - let file = new FileModel(fileFake); - file.progress = {'percent': 50}; - - let component = componentFixture.componentInstance; - componentFixture.detectChanges(); - uploadService.addToQueue([file]); + component._uploaderService.addToQueue([file]); component.filesUploadingList = [file]; - let compiled = componentFixture.debugElement.nativeElement; + fixture.detectChanges(); - componentFixture.detectChanges(); - - expect(compiled.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog show'); + expect(element.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog show'); }); it('should render dialog box with css class show when the toggleShowDialog is called', () => { - let fileFake = { - id: 'fake-id', - name: 'fake-name' - }; - let file = new FileModel(fileFake); - - let component = componentFixture.componentInstance; - componentFixture.detectChanges(); - component.filesUploadingList = [file]; - - let compiled = componentFixture.debugElement.nativeElement; - component.toggleShowDialog(); - componentFixture.detectChanges(); + fixture.detectChanges(); - expect(compiled.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog show'); + expect(element.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog show'); }); it('should render dialog box with css class hide', () => { - let fileFake = { - id: 'fake-id', - name: 'fake-name' - }; - let file = new FileModel(fileFake); - let component = componentFixture.componentInstance; - component.filesUploadingList = [file]; component.isDialogActive = true; - let compiled = componentFixture.debugElement.nativeElement; - component.toggleShowDialog(); - componentFixture.detectChanges(); + fixture.detectChanges(); - expect(compiled.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog'); + expect(element.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog'); }); it('should render minimize dialog as default', () => { - let fileFake = { - id: 'fake-id', - name: 'fake-name' - }; - let file = new FileModel(fileFake); - let component = componentFixture.componentInstance; - component.filesUploadingList = [file]; component.isDialogActive = true; - let compiled = componentFixture.debugElement.nativeElement; - component.toggleDialogMinimize(); - componentFixture.detectChanges(); + fixture.detectChanges(); - expect(compiled.querySelector('.minimize-button').getAttribute('class')).toEqual('minimize-button active'); + expect(element.querySelector('.minimize-button').getAttribute('class')).toEqual('minimize-button active'); }); }); -*/ + diff --git a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.ts b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.ts index 71e3a814a0..561f51bb43 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.ts +++ b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.ts @@ -54,7 +54,7 @@ export class FileUploadingDialogComponent implements OnInit, OnDestroy { constructor(private cd: ChangeDetectorRef, translate: AlfrescoTranslationService, private _uploaderService: UploadService) { - translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src'); + translate.addTranslationFolder('./src'); } ngOnInit() { diff --git a/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.spec.ts b/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.spec.ts index 2d9fcfcefa..5466709cf6 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.spec.ts +++ b/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.spec.ts @@ -15,29 +15,20 @@ * limitations under the License. */ -/* -import { PLATFORM_PIPES } from '@angular/core'; -import { beforeEachProviders } from '@angular/core/testing'; -import { TestComponentBuilder } from '@angular/compiler/testing'; -import { HTTP_PROVIDERS } from '@angular/http'; - -import { - AlfrescoTranslationService, - AlfrescoSettingsService, - AlfrescoAuthenticationService, - AlfrescoApiService, - AlfrescoPipeTranslate -} from 'ng2-alfresco-core'; - +import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { UploadButtonComponent } from './upload-button.component'; +import { DebugElement } from '@angular/core'; +import { + AlfrescoAuthenticationService, + AlfrescoSettingsService, + AlfrescoApiService, + CoreModule, + AlfrescoTranslationService +} from 'ng2-alfresco-core'; import { TranslationMock } from '../assets/translation.service.mock'; import { UploadService } from '../services/upload.service'; -declare var AlfrescoApi: any; - -describe('AlfrescoUploadButton', () => { - - let uploadButtonFixture; +describe('Test ng2-alfresco-upload UploadButton', () => { let file = {name: 'fake-name-1', size: 10, webkitRelativePath: 'fake-folder1/fake-name-1.json'}; let fakeEvent = { @@ -72,68 +63,73 @@ describe('AlfrescoUploadButton', () => { reject(fakeRejectRest); }); - beforeEach( () => { - window['componentHandler'] = null; - }); + let component: any; + let fixture: ComponentFixture; + let debug: DebugElement; + let element: HTMLElement; - beforeEachProviders(() => { - return [ - HTTP_PROVIDERS, - AlfrescoSettingsService, - AlfrescoAuthenticationService, - AlfrescoApiService, - { provide: PLATFORM_PIPES, useValue: AlfrescoPipeTranslate, multi: true }, - { provide: AlfrescoTranslationService, useClass: TranslationMock }, - UploadService - ]; - }); - - beforeEach( inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { - return tcb - .createAsync(UploadButtonComponent) - .then(fixture => uploadButtonFixture = fixture); + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [ + CoreModule + ], + declarations: [UploadButtonComponent], + providers: [ + AlfrescoSettingsService, + AlfrescoAuthenticationService, + AlfrescoApiService, + UploadService, + { provide: AlfrescoTranslationService, useClass: TranslationMock } + ] + }).compileComponents(); })); + beforeEach(() => { + window['componentHandler'] = null; + + fixture = TestBed.createComponent(UploadButtonComponent); + + debug = fixture.debugElement; + element = fixture.nativeElement; + component = fixture.componentInstance; + fixture.detectChanges(); + }); + it('should render upload-single-file button as default', () => { - let component = uploadButtonFixture.componentInstance; component.multipleFiles = false; - let compiled = uploadButtonFixture.debugElement.nativeElement; - uploadButtonFixture.detectChanges(); + 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', () => { - let component = uploadButtonFixture.componentInstance; component.multipleFiles = true; - let compiled = uploadButtonFixture.debugElement.nativeElement; - uploadButtonFixture.detectChanges(); + let compiled = fixture.debugElement.nativeElement; + fixture.detectChanges(); expect(compiled.querySelector('#upload-multiple-files')).toBeDefined(); }); it('should render an uploadFolder button if uploadFolder is true', () => { - let component = uploadButtonFixture.componentInstance; component.uploadFolder = true; - let compiled = uploadButtonFixture.debugElement.nativeElement; - uploadButtonFixture.detectChanges(); + let compiled = fixture.debugElement.nativeElement; + fixture.detectChanges(); expect(compiled.querySelector('#uploadFolder')).toBeDefined(); }); it('should call uploadFile with the default folder', () => { - let component = uploadButtonFixture.componentInstance; component.currentFolderPath = '/root-fake-/sites-fake/folder-fake'; component.onSuccess = null; component._uploaderService.uploadFilesInTheQueue = jasmine.createSpy('uploadFilesInTheQueue'); - uploadButtonFixture.detectChanges(); + fixture.detectChanges(); component.onFilesAdded(fakeEvent); expect(component._uploaderService.uploadFilesInTheQueue).toHaveBeenCalledWith('/root-fake-/sites-fake/folder-fake', null); }); it('should create a folder and emit an File uploaded event', (done) => { - let component = uploadButtonFixture.componentInstance; component.currentFolderPath = '/fake-root-path'; - uploadButtonFixture.detectChanges(); + fixture.detectChanges(); spyOn(component._uploaderService, 'callApiCreateFolder').and.returnValue(fakeResolvePromise); @@ -152,9 +148,6 @@ describe('AlfrescoUploadButton', () => { }); it('should emit an onError event when the folder already exist', (done) => { - let component = uploadButtonFixture.componentInstance; - uploadButtonFixture.detectChanges(); - spyOn(component._uploaderService, 'callApiCreateFolder').and.returnValue(fakeRejectPromise); component.onError.subscribe(e => { expect(e.value).toEqual('FILE_UPLOAD.MESSAGES.FOLDER_ALREADY_EXIST'); @@ -164,4 +157,4 @@ describe('AlfrescoUploadButton', () => { component.onDirectoryAdded(fakeEvent); }); }); -*/ + diff --git a/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.ts b/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.ts index d994298144..5b4b357a14 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.ts +++ b/ng2-components/ng2-alfresco-upload/src/components/upload-button.component.ts @@ -89,7 +89,7 @@ export class UploadButtonComponent { constructor(public el: ElementRef, private _uploaderService: UploadService, translate: AlfrescoTranslationService) { this.translate = translate; - this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src'); + translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src'); } ngOnChanges(changes) { diff --git a/ng2-components/ng2-alfresco-upload/src/components/upload-drag-area.component.spec.ts b/ng2-components/ng2-alfresco-upload/src/components/upload-drag-area.component.spec.ts index ceccbf5aac..b82e3465b2 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/upload-drag-area.component.spec.ts +++ b/ng2-components/ng2-alfresco-upload/src/components/upload-drag-area.component.spec.ts @@ -15,47 +15,53 @@ * limitations under the License. */ -/* -import { PLATFORM_PIPES } from '@angular/core'; -import { describe, expect, it, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; -import { TestComponentBuilder } from '@angular/compiler/testing'; +import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { UploadDragAreaComponent } from './upload-drag-area.component'; -import { AlfrescoTranslationService, AlfrescoSettingsService, AlfrescoAuthenticationService, AlfrescoApiService, AlfrescoPipeTranslate } from 'ng2-alfresco-core'; +import { DebugElement } from '@angular/core'; +import { + AlfrescoAuthenticationService, + AlfrescoSettingsService, + AlfrescoApiService, + AlfrescoTranslationService, + CoreModule +} from 'ng2-alfresco-core'; import { TranslationMock } from '../assets/translation.service.mock'; import { UploadService } from '../services/upload.service'; -import { HTTP_PROVIDERS } from '@angular/http'; import { EventEmitter } from '@angular/core'; -declare var AlfrescoApi: any; +describe('Test ng2-alfresco-upload UploadDragArea', () => { -describe('AlfrescoUploadDragArea', () => { + let component: any; + let fixture: ComponentFixture; + let debug: DebugElement; + let element: HTMLElement; - let componentFixture; - - beforeEach( () => { - - }); - - beforeEachProviders(() => { - return [ - HTTP_PROVIDERS, - AlfrescoSettingsService, - AlfrescoAuthenticationService, - AlfrescoApiService, - { provide: PLATFORM_PIPES, useValue: AlfrescoPipeTranslate, multi: true }, - { provide: AlfrescoTranslationService, useClass: TranslationMock }, - UploadService - ]; - }); - - beforeEach( inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { - return tcb - .createAsync(UploadDragAreaComponent) - .then(fixture => componentFixture = fixture); + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [ + CoreModule + ], + declarations: [UploadDragAreaComponent], + providers: [ + AlfrescoSettingsService, + AlfrescoAuthenticationService, + AlfrescoApiService, + UploadService, + { provide: AlfrescoTranslationService, useClass: TranslationMock } + ] + }).compileComponents(); })); + beforeEach(() => { + fixture = TestBed.createComponent(UploadDragAreaComponent); + + debug = fixture.debugElement; + element = fixture.nativeElement; + component = fixture.componentInstance; + fixture.detectChanges(); + }); + it('should show an folder non supported error in console when the file type is empty', () => { - let component = componentFixture.componentInstance; component.showUdoNotificationBar = false; spyOn(console, 'error'); @@ -66,7 +72,6 @@ describe('AlfrescoUploadDragArea', () => { }); it('should show an folder non supported error in the notification bar when the file type is empty', () => { - let component = componentFixture.componentInstance; component._showErrorNotificationBar = jasmine.createSpy('_showErrorNotificationBar'); component.showUdoNotificationBar = true; @@ -77,14 +82,13 @@ describe('AlfrescoUploadDragArea', () => { }); it('should upload the list of files dropped', () => { - let component = componentFixture.componentInstance; component.currentFolderPath = '/root-fake-/sites-fake/folder-fake'; component.onSuccess = null; component.showUdoNotificationBar = false; component._uploaderService.addToQueue = jasmine.createSpy('addToQueue'); component._uploaderService.uploadFilesInTheQueue = jasmine.createSpy('uploadFilesInTheQueue'); - componentFixture.detectChanges(); + fixture.detectChanges(); let fileFake = {name: 'fake-name-1', size: 10, webkitRelativePath: 'fake-folder1/fake-name-1.json'}; let filesList = [fileFake]; @@ -94,14 +98,13 @@ describe('AlfrescoUploadDragArea', () => { }); it('should show the loading messages in the notification bar when the files are dropped', () => { - let component = componentFixture.componentInstance; component.currentFolderPath = '/root-fake-/sites-fake/folder-fake'; component.onSuccess = null; component.showUdoNotificationBar = true; component._uploaderService.uploadFilesInTheQueue = jasmine.createSpy('uploadFilesInTheQueue'); component._showUndoNotificationBar = jasmine.createSpy('_showUndoNotificationBar'); - componentFixture.detectChanges(); + fixture.detectChanges(); let fileFake = {name: 'fake-name-1', size: 10, webkitRelativePath: 'fake-folder1/fake-name-1.json'}; let filesList = [fileFake]; @@ -111,11 +114,10 @@ describe('AlfrescoUploadDragArea', () => { }); it('should upload a file when dropped', done => { - let component = componentFixture.componentInstance; component.currentFolderPath = '/root-fake-/sites-fake/document-library-fake'; component.onSuccess = null; - componentFixture.detectChanges(); + fixture.detectChanges(); spyOn(component._uploaderService, 'uploadFilesInTheQueue'); spyOn(component, '_showUndoNotificationBar').and.callFake( () => { expect(component._showUndoNotificationBar).toHaveBeenCalled(); @@ -139,11 +141,10 @@ describe('AlfrescoUploadDragArea', () => { }); it('should throws an exception and show it in the notification bar when the folder already exist', done => { - let component = componentFixture.componentInstance; component.currentFolderPath = '/root-fake-/sites-fake/folder-fake'; component.showUdoNotificationBar = true; - componentFixture.detectChanges(); + fixture.detectChanges(); let fakeRest = { response: { body: { @@ -173,11 +174,10 @@ describe('AlfrescoUploadDragArea', () => { }); it('should create a folder and call onFilesEntityDropped with the file inside the folder', done => { - let component = componentFixture.componentInstance; component.currentFolderPath = '/root-fake-/sites-fake/document-library-fake'; component.onSuccess = new EventEmitter(); - componentFixture.detectChanges(); + fixture.detectChanges(); let itemEntity = { fullPath: '/folder-fake/file-fake.png', @@ -224,4 +224,4 @@ describe('AlfrescoUploadDragArea', () => { component.onFolderEntityDropped(folderEntry); }); }); -*/ + diff --git a/ng2-components/ng2-alfresco-upload/src/components/upload-drag-area.component.ts b/ng2-components/ng2-alfresco-upload/src/components/upload-drag-area.component.ts index e947a86451..73c743e0b5 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/upload-drag-area.component.ts +++ b/ng2-components/ng2-alfresco-upload/src/components/upload-drag-area.component.ts @@ -60,7 +60,7 @@ export class UploadDragAreaComponent { constructor(private _uploaderService: UploadService, translate: AlfrescoTranslationService) { this.translate = translate; - this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src'); + translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src'); } ngOnChanges(changes) { @@ -240,9 +240,11 @@ export class UploadDragAreaComponent { * @returns {string} - The message without placeholder */ private formatString(message: string, keys: any []) { - let i = keys.length; - while (i--) { - message = message.replace(new RegExp('\\{' + i + '\\}', 'gm'), keys[i]); + if (message) { + let i = keys.length; + while (i--) { + message = message.replace(new RegExp('\\{' + i + '\\}', 'gm'), keys[i]); + } } return message; } diff --git a/ng2-components/ng2-alfresco-upload/src/directives/file-draggable.directive.spec.ts b/ng2-components/ng2-alfresco-upload/src/directives/file-draggable.directive.spec.ts index 1148e14935..3a2776f635 100644 --- a/ng2-components/ng2-alfresco-upload/src/directives/file-draggable.directive.spec.ts +++ b/ng2-components/ng2-alfresco-upload/src/directives/file-draggable.directive.spec.ts @@ -17,7 +17,7 @@ import { FileDraggableDirective } from '../directives/file-draggable.directive'; -describe('AlfrescoDirectiveFileDraggable', () => { +describe('Test ng2-alfresco-upload FileDraggableDirective', () => { let component; @@ -101,9 +101,4 @@ describe('AlfrescoDirectiveFileDraggable', () => { component._onDragEnter(mockEvent); expect(component.getInputFocus()).toBe(true); }); - - - - - }); diff --git a/ng2-components/ng2-alfresco-upload/src/services/upload.service.spec.ts b/ng2-components/ng2-alfresco-upload/src/services/upload.service.spec.ts index 8e20677a02..50e8c15e21 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/upload.service.spec.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/upload.service.spec.ts @@ -15,17 +15,20 @@ * limitations under the License. */ -/* -import { it, describe, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; -import { EventEmitter } from '@angular/core'; +import { ReflectiveInjector } from '@angular/core'; +import { + AlfrescoAuthenticationService, + AlfrescoSettingsService, + AlfrescoApiService +} from 'ng2-alfresco-core'; import { UploadService } from './upload.service'; -import { AlfrescoSettingsService, AlfrescoApiService, AlfrescoAuthenticationService } from 'ng2-alfresco-core'; +import { EventEmitter } from '@angular/core'; -declare let AlfrescoApi: any; declare let jasmine: any; +declare let AlfrescoApi: any; -describe('AlfrescoUploadService', () => { - let service, options: any; +describe('Test ng2-alfresco-upload', () => { + let service, injector, options; options = { host: 'fakehost', @@ -37,202 +40,203 @@ describe('AlfrescoUploadService', () => { } }; - beforeEachProviders(() => { - return [ + beforeEach(() => { + injector = ReflectiveInjector.resolveAndCreate([ AlfrescoSettingsService, AlfrescoApiService, AlfrescoAuthenticationService, UploadService - ]; + ]); }); - - beforeEach( inject([UploadService, AlfrescoApiService], (uploadService: UploadService, apiService: AlfrescoApiService) => { - jasmine.Ajax.install(); - service = uploadService; - apiService.setInstance(new AlfrescoApi({})); - })); - - afterEach(() => { - jasmine.Ajax.uninstall(); - }); - - 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', (done) => { - let emitter = new EventEmitter(); - - emitter.subscribe(e => { - expect(e.value).toBe('File uploaded'); - done(); + describe('UploadService ', () => { + beforeEach(() => { + service = injector.get(UploadService); + service.apiService.setInstance(new AlfrescoApi({})); + jasmine.Ajax.install(); }); - service.setOptions(options); - let filesFake = [{name: 'fake-name', size: 10}]; - service.addToQueue(filesFake); - service.uploadFilesInTheQueue('fake-dir', emitter); - let request = jasmine.Ajax.requests.mostRecent(); - expect(request.url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?autoRename=true'); - expect(request.method).toBe('POST'); - - jasmine.Ajax.requests.mostRecent().respondWith({ - 'status': 200, - contentType: 'text/plain', - responseText: 'File uploaded' + afterEach(() => { + jasmine.Ajax.uninstall(); }); - }); - it('should make XHR error request after an error occur', (done) => { - let emitter = new EventEmitter(); - - emitter.subscribe(e => { - expect(e.value).toBe('Error file uploaded'); - done(); + it('should return an empty queue if no elements are added', () => { + service.setOptions(options, false); + expect(service.getQueue().length).toEqual(0); }); - service.setOptions(options); - let filesFake = [{name: 'fake-name', size: 10}]; - service.addToQueue(filesFake); - service.uploadFilesInTheQueue('', emitter); - expect(jasmine.Ajax.requests.mostRecent().url) - .toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?autoRename=true'); - jasmine.Ajax.requests.mostRecent().respondWith({ - 'status': 404, - contentType: 'text/plain', - responseText: 'Error file uploaded' + it('should add an element in the queue and returns it', () => { + service.setOptions(options, false); + let filesFake = [{name: 'fake-name', size: 10}]; + service.addToQueue(filesFake); + expect(service.getQueue().length).toEqual(1); }); - }); - it('should make XHR abort request after the xhr abort is called', (done) => { - let emitter = new EventEmitter(); - - emitter.subscribe(e => { - expect(e.value).toEqual('File aborted'); - done(); + it('should add two elements in the queue and returns them', () => { + service.setOptions(options, false); + let filesFake = [{name: 'fake-name', size: 10}, {name: 'fake-name2', size: 20}]; + service.addToQueue(filesFake); + expect(service.getQueue().length).toEqual(2); }); - service.setOptions(options); - let filesFake = [{name: 'fake-name', size: 10}]; - service.addToQueue(filesFake); - service.uploadFilesInTheQueue('', emitter); - let file = service.getQueue(); - file[0].emitAbort(); - }); + it('should make XHR done request after the file is added in the queue', (done) => { + let emitter = new EventEmitter(); - it('should make XHR error request after the xhr error is called', (done) => { - let emitter = new EventEmitter(); + emitter.subscribe(e => { + expect(e.value).toBe('File uploaded'); + done(); + }); + service.setOptions(options, false); + let filesFake = [{name: 'fake-name', size: 10}]; + service.addToQueue(filesFake); + service.uploadFilesInTheQueue('fake-dir', emitter); - emitter.subscribe(e => { - expect(e.value).toBe('Error file uploaded'); - done(); + let request = jasmine.Ajax.requests.mostRecent(); + expect(request.url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?autoRename=true'); + expect(request.method).toBe('POST'); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'text/plain', + responseText: 'File uploaded' + }); }); - service.setOptions(options); - let filesFake = [{name: 'fake-name', size: 10}]; - service.addToQueue(filesFake); - service.uploadFilesInTheQueue('', emitter); - let file = service.getQueue(); - file[0].emitError(); - }); + it('should make XHR error request after an error occur', (done) => { + let emitter = new EventEmitter(); - it('should make XHR progress request after the onprogress is called', (done) => { - service.setOptions(options); - let fakeProgress = { - loaded: 500, - total: 1234, - percent: 44 - }; - let filesFake = [{name: 'fake-name', size: 10}]; - service.addToQueue(filesFake); - service.filesUpload$.subscribe((file) => { - expect(file).toBeDefined(); - expect(file[0]).toBeDefined(); - expect(file[0].progress).toEqual(fakeProgress); - done(); + emitter.subscribe(e => { + expect(e.value).toBe('Error file uploaded'); + done(); + }); + service.setOptions(options, false); + let filesFake = [{name: 'fake-name', size: 10}]; + service.addToQueue(filesFake); + service.uploadFilesInTheQueue('', emitter); + expect(jasmine.Ajax.requests.mostRecent().url) + .toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?autoRename=true'); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 404, + contentType: 'text/plain', + responseText: 'Error file uploaded' + }); }); - service.uploadFilesInTheQueue('', null); - let file = service.getQueue(); + it('should make XHR abort request after the xhr abort is called', (done) => { + let emitter = new EventEmitter(); - file[0].emitProgres(fakeProgress); - }); + emitter.subscribe(e => { + expect(e.value).toEqual('File aborted'); + done(); + }); + service.setOptions(options, false); + let filesFake = [{name: 'fake-name', size: 10}]; + service.addToQueue(filesFake); + service.uploadFilesInTheQueue('', emitter); - it('should make XHR done request after the folder is created', (done) => { - let fakeRest = { - entry: { - isFile: false, - isFolder: true, - name: 'fake-folder' - } - }; - let fakePromise = new Promise(function (resolve, reject) { - resolve(fakeRest); + let file = service.getQueue(); + file[0].emitAbort(); }); - spyOn(service, 'callApiCreateFolder').and.returnValue(fakePromise); - service.setOptions(options); - let defaultPath = ''; - let folderName = 'fake-folder'; - service.createFolder(defaultPath, folderName).subscribe(res => { - expect(res).toEqual(fakeRest); - done(); - }); - }); - it('should throws an exception when a folder already exist', (done) => { - let fakeRest = { - response: { - body: { - error: { - statusCode: 409 + it('should make XHR error request after the xhr error is called', (done) => { + let emitter = new EventEmitter(); + + emitter.subscribe(e => { + expect(e.value).toBe('Error file uploaded'); + done(); + }); + service.setOptions(options, false); + let filesFake = [{name: 'fake-name', size: 10}]; + service.addToQueue(filesFake); + service.uploadFilesInTheQueue('', emitter); + + let file = service.getQueue(); + file[0].emitError(); + }); + + it('should make XHR progress request after the onprogress is called', (done) => { + service.setOptions(options, false); + let fakeProgress = { + loaded: 500, + total: 1234, + percent: 44 + }; + let filesFake = [{name: 'fake-name', size: 10}]; + service.addToQueue(filesFake); + service.filesUpload$.subscribe((file) => { + expect(file).toBeDefined(); + expect(file[0]).toBeDefined(); + expect(file[0].progress).toEqual(fakeProgress); + done(); + }); + service.uploadFilesInTheQueue('', null); + + let file = service.getQueue(); + + file[0].emitProgres(fakeProgress); + }); + + it('should make XHR done request after the folder is created', (done) => { + let fakeRest = { + entry: { + isFile: false, + isFolder: true, + name: 'fake-folder' + } + }; + let fakePromise = new Promise(function (resolve, reject) { + resolve(fakeRest); + }); + spyOn(service, 'callApiCreateFolder').and.returnValue(fakePromise); + service.setOptions(options, false); + let defaultPath = ''; + let folderName = 'fake-folder'; + service.createFolder(defaultPath, folderName).subscribe(res => { + expect(res).toEqual(fakeRest); + done(); + }); + }); + + it('should throws an exception when a folder already exist', (done) => { + let fakeRest = { + response: { + body: { + error: { + statusCode: 409 + } } } - } - }; - let fakePromise = new Promise(function (resolve, reject) { - reject(fakeRest); + }; + let fakePromise = new Promise(function (resolve, reject) { + reject(fakeRest); + }); + spyOn(service, 'callApiCreateFolder').and.returnValue(fakePromise); + service.setOptions(options, false); + let defaultPath = ''; + let folderName = 'folder-duplicate-fake'; + service.createFolder(defaultPath, folderName).subscribe( + res => { + }, + error => { + expect(error).toEqual(fakeRest); + done(); + } + ); }); - spyOn(service, 'callApiCreateFolder').and.returnValue(fakePromise); - service.setOptions(options); - let defaultPath = ''; - let folderName = 'folder-duplicate-fake'; - service.createFolder(defaultPath, folderName).subscribe( - res => { - }, - error => { - expect(error).toEqual(fakeRest); - done(); - } - ); - }); - it('If versioning is true autoRename should not be present and majorVersion should be a param', () => { - let emitter = new EventEmitter(); + it('If versioning is true autoRename should not be present and majorVersion should be a param', () => { + let emitter = new EventEmitter(); - let enableVersioning = true; - service.setOptions(options, enableVersioning); - let filesFake = [{name: 'fake-name', size: 10}]; - service.addToQueue(filesFake); - service.uploadFilesInTheQueue('', emitter); + let enableVersioning = true; + service.setOptions(options, enableVersioning); + let filesFake = [{name: 'fake-name', size: 10}]; + service.addToQueue(filesFake); + service.uploadFilesInTheQueue('', emitter); - console.log(jasmine.Ajax.requests.mostRecent().url); - expect(jasmine.Ajax.requests.mostRecent().url.endsWith('autoRename=true')).toBe(false); - expect(jasmine.Ajax.requests.mostRecent().params.has('majorVersion')).toBe(true); + console.log(jasmine.Ajax.requests.mostRecent().url); + expect(jasmine.Ajax.requests.mostRecent().url.endsWith('autoRename=true')).toBe(false); + expect(jasmine.Ajax.requests.mostRecent().params.has('majorVersion')).toBe(true); + }); }); }); -*/ +