[ADF-924] upload dialog unit tests (#2132)

This commit is contained in:
Cilibiu Bogdan
2017-07-26 12:01:00 +03:00
committed by Eugenio Romano
parent 896149acd8
commit 1c6ee6e7bf
4 changed files with 325 additions and 94 deletions

View File

@@ -15,52 +15,37 @@
* limitations under the License. * limitations under the License.
*/ */
import { DebugElement } from '@angular/core'; import { EventEmitter } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MdProgressSpinnerModule } from '@angular/material'; import { FileModel, FileUploadCompleteEvent, UploadService } from 'ng2-alfresco-core';
import { CoreModule } from 'ng2-alfresco-core'; import { UploadModule } from '../../index';
import { FileModel, FileUploadStatus } from 'ng2-alfresco-core';
import { FileUploadCompleteEvent, FileUploadEvent, UploadService } from 'ng2-alfresco-core';
import { FileUploadingDialogComponent } from './file-uploading-dialog.component'; import { FileUploadingDialogComponent } from './file-uploading-dialog.component';
import { FileUploadingListComponent } from './file-uploading-list.component';
xdescribe('FileUploadingDialogComponent', () => { describe('FileUploadingDialogComponent', () => {
let component: FileUploadingDialogComponent;
let fixture: ComponentFixture<FileUploadingDialogComponent>; let fixture: ComponentFixture<FileUploadingDialogComponent>;
let debug: DebugElement;
let element: any;
let file: FileModel;
let uploadService: UploadService; let uploadService: UploadService;
let component: FileUploadingDialogComponent;
let emitter: EventEmitter<any>;
let filelist: FileModel[];
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
CoreModule.forRoot(), UploadModule
MdProgressSpinnerModule
],
declarations: [
FileUploadingDialogComponent,
FileUploadingListComponent
],
providers: [
UploadService
] ]
}).compileComponents(); }).compileComponents();
})); }));
beforeEach(() => { beforeEach(() => {
const fileFake = new File([''], 'fake-name');
file = new FileModel(fileFake);
fixture = TestBed.createComponent(FileUploadingDialogComponent); fixture = TestBed.createComponent(FileUploadingDialogComponent);
uploadService = TestBed.get(UploadService);
debug = fixture.debugElement;
element = fixture.nativeElement;
component = fixture.componentInstance; component = fixture.componentInstance;
uploadService = TestBed.get(UploadService);
emitter = new EventEmitter();
filelist = [
new FileModel(<File> { name: 'fake-name', size: 10 }),
new FileModel(<File> { name: 'fake-name2', size: 10 })
];
component.filesUploadingList = [file];
fixture.detectChanges(); fixture.detectChanges();
}); });
@@ -69,78 +54,74 @@ xdescribe('FileUploadingDialogComponent', () => {
TestBed.resetTestingModule(); TestBed.resetTestingModule();
}); });
it('should render completed upload 1 when an element is added to Observer', () => { describe('upload service subscribers', () => {
uploadService.fileUploadComplete.next(new FileUploadCompleteEvent(null, 1)); it('does not render when uploading list is empty', () => {
fixture.detectChanges(); uploadService.addToQueue();
uploadService.uploadFilesInTheQueue(emitter);
expect(element.querySelector('#total-upload-completed').innerText).toEqual('1'); expect(component.isDialogActive).toBe(false);
});
it('should render dialog box with css class show when an element is added to Observer', () => {
uploadService.addToQueue(new FileModel(<File> { 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(<File> { name: 'file' }));
fixture.whenStable().then(() => {
fixture.detectChanges();
let closeButton = element.querySelector('#button-close-upload-list');
expect(closeButton).not.toBeNull();
}); });
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(() => { expect(component.isDialogActive).toBe(true);
component.isDialogActive = true;
fixture.whenStable().then(() => {
fixture.detectChanges();
let closeButton = element.querySelector('#button-close-upload-list');
expect(closeButton).not.toBeNull();
}); });
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(() => { expect(component.filesUploadingList.length).toBe(2);
component.isDialogActive = true;
fixture.whenStable().then(() => {
fixture.detectChanges();
let closeButton = element.querySelector('#button-close-upload-list');
expect(closeButton).not.toBeNull();
}); });
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);
});
});
}); });

View File

@@ -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<FileUploadingListRowComponent>;
let component: FileUploadingListRowComponent;
let file = new FileModel(<File> { 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);
});
});

View File

@@ -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<FileUploadingListComponent>;
let component: FileUploadingListComponent;
let uploadService: UploadService;
let nodesApiService: NodesApiService;
let fileUploadService: FileUploadService;
let notificationService: NotificationService;
let translateService: AlfrescoTranslationService;
let file = new FileModel(<File> { 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);
});
});
});

View File

@@ -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(<File> { 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);
});
});
});