From 1c6ee6e7bf81b6c4253d0b7fcb2c4c91c5f503c3 Mon Sep 17 00:00:00 2001 From: Cilibiu Bogdan Date: Wed, 26 Jul 2017 12:01:00 +0300 Subject: [PATCH] [ADF-924] upload dialog unit tests (#2132) --- .../file-uploading-dialog.component.spec.ts | 169 ++++++++---------- .../file-uploading-list-row.component.spec.ts | 55 ++++++ .../file-uploading-list.component.spec.ts | 152 ++++++++++++++++ .../services/file-uploading.service.spec.ts | 43 +++++ 4 files changed, 325 insertions(+), 94 deletions(-) create mode 100644 ng2-components/ng2-alfresco-upload/src/components/file-uploading-list-row.component.spec.ts create mode 100644 ng2-components/ng2-alfresco-upload/src/components/file-uploading-list.component.spec.ts create mode 100644 ng2-components/ng2-alfresco-upload/src/services/file-uploading.service.spec.ts 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 e0a0d7a446..16f46ea380 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,52 +15,37 @@ * limitations under the License. */ -import { DebugElement } from '@angular/core'; +import { EventEmitter } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { MdProgressSpinnerModule } from '@angular/material'; -import { CoreModule } from 'ng2-alfresco-core'; -import { FileModel, FileUploadStatus } from 'ng2-alfresco-core'; -import { FileUploadCompleteEvent, FileUploadEvent, UploadService } from 'ng2-alfresco-core'; +import { FileModel, FileUploadCompleteEvent, UploadService } from 'ng2-alfresco-core'; +import { UploadModule } from '../../index'; import { FileUploadingDialogComponent } from './file-uploading-dialog.component'; -import { FileUploadingListComponent } from './file-uploading-list.component'; -xdescribe('FileUploadingDialogComponent', () => { - - let component: FileUploadingDialogComponent; +describe('FileUploadingDialogComponent', () => { let fixture: ComponentFixture; - let debug: DebugElement; - let element: any; - let file: FileModel; let uploadService: UploadService; + let component: FileUploadingDialogComponent; + let emitter: EventEmitter; + let filelist: FileModel[]; beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - CoreModule.forRoot(), - MdProgressSpinnerModule - ], - declarations: [ - FileUploadingDialogComponent, - FileUploadingListComponent - ], - providers: [ - UploadService + UploadModule ] }).compileComponents(); })); beforeEach(() => { - const fileFake = new File([''], 'fake-name'); - file = new FileModel(fileFake); - fixture = TestBed.createComponent(FileUploadingDialogComponent); - uploadService = TestBed.get(UploadService); - - debug = fixture.debugElement; - element = fixture.nativeElement; component = fixture.componentInstance; + uploadService = TestBed.get(UploadService); + emitter = new EventEmitter(); + filelist = [ + new FileModel( { name: 'fake-name', size: 10 }), + new FileModel( { name: 'fake-name2', size: 10 }) + ]; - component.filesUploadingList = [file]; fixture.detectChanges(); }); @@ -69,78 +54,74 @@ xdescribe('FileUploadingDialogComponent', () => { TestBed.resetTestingModule(); }); - it('should render completed upload 1 when an element is added to Observer', () => { - uploadService.fileUploadComplete.next(new FileUploadCompleteEvent(null, 1)); - fixture.detectChanges(); + describe('upload service subscribers', () => { + it('does not render when uploading list is empty', () => { + uploadService.addToQueue(); + uploadService.uploadFilesInTheQueue(emitter); - 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', () => { - uploadService.addToQueue(new FileModel( { name: 'file' })); - component.filesUploadingList = [file]; - - fixture.detectChanges(); - - expect(element.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog show'); - }); - - it('should render dialog box with css class show when the toggleVisible is called', () => { - component.close(); - fixture.detectChanges(); - - expect(element.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog show'); - }); - - it('should render dialog box with css class hide', () => { - component.isDialogActive = true; - - component.close(); - fixture.detectChanges(); - - expect(element.querySelector('.file-dialog').getAttribute('class')).toEqual('file-dialog'); - }); - - it('should render minimize dialog as default', () => { - component.isDialogActive = true; - - component.toggleMinimized(); - fixture.detectChanges(); - - expect(element.querySelector('.minimize-button').getAttribute('class')).toEqual('minimize-button active'); - }); - - it('should show the close button when the file upload is completed', async(() => { - component.isDialogActive = true; - uploadService.addToQueue(new FileModel( { name: 'file' })); - fixture.whenStable().then(() => { - fixture.detectChanges(); - let closeButton = element.querySelector('#button-close-upload-list'); - expect(closeButton).not.toBeNull(); + expect(component.isDialogActive).toBe(false); }); - uploadService.fileUpload.next(new FileUploadCompleteEvent(file, 1, { status: FileUploadStatus.Complete }, 0)); - })); + it('opens when uploading list is not empty', () => { + uploadService.addToQueue(...filelist); + uploadService.uploadFilesInTheQueue(emitter); - it('should show the close button when the file upload is in error', async(() => { - component.isDialogActive = true; - fixture.whenStable().then(() => { - fixture.detectChanges(); - let closeButton = element.querySelector('#button-close-upload-list'); - expect(closeButton).not.toBeNull(); + expect(component.isDialogActive).toBe(true); }); - uploadService.fileUpload.next(new FileUploadEvent(file, FileUploadStatus.Error)); - })); + it('updates uploading file list', () => { + uploadService.addToQueue(...filelist); + uploadService.uploadFilesInTheQueue(emitter); - it('should show the close button when the file upload is cancelled', async(() => { - component.isDialogActive = true; - fixture.whenStable().then(() => { - fixture.detectChanges(); - let closeButton = element.querySelector('#button-close-upload-list'); - expect(closeButton).not.toBeNull(); + expect(component.filesUploadingList.length).toBe(2); }); - uploadService.fileUpload.next(new FileUploadEvent(file, FileUploadStatus.Cancelled)); - })); + it('updates completed uploaded files', () => { + const completedFiles = 2; + const completeEvent = new FileUploadCompleteEvent(null, completedFiles, null, null); + uploadService.fileUploadComplete.next(completeEvent); + + expect(component.totalCompleted).toEqual(completedFiles); + }); + }); + + describe('toggleMinimized()', () => { + it('minimzes the dialog', () => { + component.isDialogMinimized = true; + component.toggleMinimized(); + + expect(component.isDialogMinimized).toBe(false); + }); + + it('maximizes the dialog', () => { + component.isDialogMinimized = false; + component.toggleMinimized(); + + expect(component.isDialogMinimized).toBe(true); + }); + }); + + describe('close()', () => { + it('closes the dialog', () => { + component.isDialogActive = true; + component.close(); + + expect(component.isDialogActive).toBe(false); + }); + + it('resets dialog minimize state', () => { + component.isDialogMinimized = true; + component.close(); + + expect(component.isDialogMinimized).toBe(false); + }); + + it('resets upload queue', () => { + uploadService.addToQueue(...filelist); + uploadService.uploadFilesInTheQueue(emitter); + component.close(); + + expect(uploadService.getQueue().length).toBe(0); + }); + }); }); diff --git a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-list-row.component.spec.ts b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-list-row.component.spec.ts new file mode 100644 index 0000000000..00ca4ea96e --- /dev/null +++ b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-list-row.component.spec.ts @@ -0,0 +1,55 @@ +/*! + * @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 { ComponentFixture, TestBed } from '@angular/core/testing'; +import { FileModel } from 'ng2-alfresco-core'; +import { UploadModule } from '../../index'; +import { FileUploadingListRowComponent } from './file-uploading-list-row.component'; + +describe('FileUploadingListRowComponent', () => { + let fixture: ComponentFixture; + let component: FileUploadingListRowComponent; + let file = new FileModel( { name: 'fake-name' }); + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + UploadModule + ] + }).compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(FileUploadingListRowComponent); + component = fixture.componentInstance; + component.file = file; + }); + + it('emits cancel event', () => { + spyOn(component.cancel, 'emit'); + component.onCancel(component.file); + + expect(component.cancel.emit).toHaveBeenCalledWith(file); + }); + + it('emits remove event', () => { + spyOn(component.remove, 'emit'); + component.onRemove(component.file); + + expect(component.remove.emit).toHaveBeenCalledWith(file); + }); +}); diff --git a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-list.component.spec.ts b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-list.component.spec.ts new file mode 100644 index 0000000000..a2ae832855 --- /dev/null +++ b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-list.component.spec.ts @@ -0,0 +1,152 @@ +/*! + * @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 { ComponentFixture, TestBed } from '@angular/core/testing'; +import { AlfrescoTranslationService, FileModel, NodesApiService, NotificationService, UploadService } from 'ng2-alfresco-core'; +import { Observable } from 'rxjs/Rx'; +import { UploadModule } from '../../index'; +import { FileUploadService } from '../services/file-uploading.service'; +import { FileUploadingListComponent } from './file-uploading-list.component'; + +describe('FileUploadingListComponent', () => { + let fixture: ComponentFixture; + let component: FileUploadingListComponent; + let uploadService: UploadService; + let nodesApiService: NodesApiService; + let fileUploadService: FileUploadService; + let notificationService: NotificationService; + let translateService: AlfrescoTranslationService; + let file = new FileModel( { name: 'fake-name' }); + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + UploadModule + ] + }).compileComponents(); + }); + + beforeEach(() => { + nodesApiService = TestBed.get(NodesApiService); + uploadService = TestBed.get(UploadService); + fileUploadService = TestBed.get(FileUploadService); + notificationService = TestBed.get(NotificationService); + translateService = TestBed.get(AlfrescoTranslationService); + fixture = TestBed.createComponent(FileUploadingListComponent); + component = fixture.componentInstance; + component.files = [ file ]; + file.data = { entry: { id: 'x' } }; + + spyOn(translateService, 'get').and.returnValue(Observable.of('some error message')); + }); + + describe('cancelFileUpload()', () => { + it('calls cancelUpload()', () => { + spyOn(uploadService, 'cancelUpload'); + component.cancelFileUpload(file); + + expect(uploadService.cancelUpload).toHaveBeenCalledWith(file); + }); + }); + + describe('removeFile()', () => { + it('removes file successfully', () => { + spyOn(nodesApiService, 'deleteNode').and.returnValue(Observable.of('success')); + spyOn(fileUploadService, 'emitFileRemoved'); + + component.removeFile(file); + fixture.detectChanges(); + + expect(fileUploadService.emitFileRemoved).toHaveBeenCalledWith(file); + }); + + it('notify on remove file fail', () => { + spyOn(nodesApiService, 'deleteNode').and.returnValue(Observable.throw({})); + spyOn(notificationService, 'openSnackMessage'); + + component.removeFile(file); + fixture.detectChanges(); + + expect(notificationService.openSnackMessage).toHaveBeenCalled(); + }); + }); + + describe('cancelAllFiles()', () => { + beforeEach(() => { + spyOn(component, 'removeFile'); + spyOn(component, 'cancelFileUpload'); + }); + + it('calls remove method if file was uploaded', () => { + file.status = 1; + component.cancelAllFiles(null); + + expect(component.removeFile).toHaveBeenCalledWith(file); + }); + + it('calls cancel method if file is in progress', () => { + file.status = 3; + component.cancelAllFiles(null); + + expect(component.cancelFileUpload).toHaveBeenCalledWith(file); + }); + }); + + describe('isUploadCompleted()', () => { + it('returns true', () => { + file.status = 1; + + expect(component.isUploadCompleted()).toBe(true); + }); + + it('returns false', () => { + file.status = 3; + + expect(component.isUploadCompleted()).toBe(false); + }); + }); + + describe('isUploadCancelled()', () => { + it('return true', () => { + file.status = 4; + + expect(component.isUploadCancelled()).toBe(true); + }); + + it('return false', () => { + file.status = 1; + + expect(component.isUploadCancelled()).toBe(false); + }); + }); + + describe('uploadErrorFiles()', () => { + it('returns the error files', () => { + file.status = 6; + + expect(component.uploadErrorFiles()).toEqual([file]); + }); + }); + + describe('totalErrorFiles()', () => { + it('returns the number of error files', () => { + file.status = 6; + + expect(component.totalErrorFiles()).toEqual(1); + }); + }); +}); diff --git a/ng2-components/ng2-alfresco-upload/src/services/file-uploading.service.spec.ts b/ng2-components/ng2-alfresco-upload/src/services/file-uploading.service.spec.ts new file mode 100644 index 0000000000..c44b442eba --- /dev/null +++ b/ng2-components/ng2-alfresco-upload/src/services/file-uploading.service.spec.ts @@ -0,0 +1,43 @@ +/*! + * @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 { FileModel } from 'ng2-alfresco-core'; +import { FileUploadService } from './file-uploading.service'; + +describe('FileUploadService', () => { + let service: FileUploadService; + let file = new FileModel( { name: 'fake-name' }); + + beforeEach(() => { + service = new FileUploadService(); + }); + + it('emits file remove event', () => { + spyOn(service.remove, 'next'); + service.emitFileRemoved(file); + + expect(service.remove.next).toHaveBeenCalledWith(file); + }); + + it('passes removed file data', () => { + service.emitFileRemoved(file); + + service.remove.subscribe((data) => { + expect(data).toEqual(file); + }); + }); +});