Unit tests for document list component

This commit is contained in:
Denys Vuika
2016-06-29 16:37:16 +01:00
parent 1334f52e20
commit 01c63f9574
6 changed files with 273 additions and 18 deletions

View File

@@ -21,17 +21,26 @@ import {
expect,
beforeEach
} from '@angular/core/testing';
import {ContentActionHandler} from '../models/content-action.model';
import {DocumentActionsService} from './document-actions.service';
import {AlfrescoServiceMock} from '../assets/alfresco.service.mock';
import { AlfrescoContentService } from 'ng2-alfresco-core';
import { ContentActionHandler } from '../models/content-action.model';
import { DocumentActionsService } from './document-actions.service';
import { AlfrescoServiceMock } from '../assets/alfresco.service.mock';
import { AlfrescoService } from './alfresco.service';
import {
FileNode,
FolderNode
} from '../assets/document-library.model.mock';
describe('DocumentActionsService', () => {
let service: DocumentActionsService;
let alfrescoService: AlfrescoService;
let contentService: AlfrescoContentService;
beforeEach(() => {
let alfrescoServiceMock = new AlfrescoServiceMock();
service = new DocumentActionsService(alfrescoServiceMock);
alfrescoService = new AlfrescoServiceMock();
contentService = new AlfrescoContentService(null, null);
service = new DocumentActionsService(alfrescoService, contentService);
});
it('should register default download action', () => {
@@ -52,6 +61,142 @@ describe('DocumentActionsService', () => {
let handler: ContentActionHandler = function (obj: any) {};
service.setHandler('<key>', handler);
expect(service.getHandler('<KEY>')).toBe(handler);
});
it('should not find handler with invalid key', () => {
expect(service.getHandler(null)).toBeNull();
expect(service.getHandler('')).toBeNull();
});
it('should allow action execution only when service available', () => {
let file = new FileNode();
expect(service.canExecuteAction(file)).toBeTruthy();
service = new DocumentActionsService(null);
expect(service.canExecuteAction(file)).toBeFalsy();
});
it('should allow action execution only for file nodes', () => {
expect(service.canExecuteAction(null)).toBeFalsy();
expect(service.canExecuteAction(new FileNode())).toBeTruthy();
expect(service.canExecuteAction(new FolderNode())).toBeFalsy();
});
it('should set new handler only by key', () => {
let handler: ContentActionHandler = function (obj: any) {};
expect(service.setHandler(null, handler)).toBeFalsy();
expect(service.setHandler('', handler)).toBeFalsy();
expect(service.setHandler('my-handler', handler)).toBeTruthy();
});
// TODO: to be removed once demo handlers are removed
it('should execute demo actions', () => {
spyOn(window, 'alert').and.stub();
service.getHandler('system1')(null);
expect(window.alert).toHaveBeenCalledWith('standard document action 1');
service.getHandler('system2')(null);
expect(window.alert).toHaveBeenCalledWith('standard document action 2');
});
// TODO: to be removed once demo handlers are removed
it('should register demo handlers', () => {
expect(service.getHandler('system1')).toBeDefined();
expect(service.getHandler('system2')).toBeDefined();
});
it('should register delete action', () => {
expect(service.getHandler('delete')).toBeDefined();
});
it('should register download action', () => {
expect(service.getHandler('download')).toBeDefined();
});
it('should execute download action and cleanup', () => {
let file = new FileNode();
let url = 'http://<address>';
spyOn(contentService, 'getContentUrl').and.returnValue(url);
let link = jasmine.createSpyObj('a', [
'setAttribute',
'click'
]);
spyOn(document, 'createElement').and.returnValue(link);
spyOn(document.body, 'appendChild').and.stub();
spyOn(document.body, 'removeChild').and.stub();
service.getHandler('download')(file);
expect(contentService.getContentUrl).toHaveBeenCalledWith(file);
expect(document.createElement).toHaveBeenCalledWith('a');
expect(link.setAttribute).toHaveBeenCalledWith('download', 'download');
expect(document.body.appendChild).toHaveBeenCalledWith(link);
expect(link.click).toHaveBeenCalled();
expect(document.body.removeChild).toHaveBeenCalledWith(link);
});
it('should require internal service for download action', () => {
let actionService = new DocumentActionsService(null, contentService);
let file = new FileNode();
let result = actionService.getHandler('download')(file);
expect(result).toBeFalsy();
});
it('should require content service for download action', () => {
let actionService = new DocumentActionsService(alfrescoService, null);
let file = new FileNode();
let result = actionService.getHandler('download')(file);
expect(result).toBeFalsy();
});
it('should require file node for download action', () => {
let folder = new FolderNode();
expect(service.getHandler('download')(folder)).toBeFalsy();
});
it('should delete file node', () => {
spyOn(alfrescoService, 'deleteNode').and.callThrough();
let file = new FileNode();
service.getHandler('delete')(file);
expect(alfrescoService.deleteNode).toHaveBeenCalledWith(file.entry.id);
});
it('should support deletion only file node', () => {
spyOn(alfrescoService, 'deleteNode').and.callThrough();
let folder = new FolderNode();
service.getHandler('delete')(folder);
expect(alfrescoService.deleteNode).not.toHaveBeenCalled();
let file = new FileNode();
service.getHandler('delete')(file);
expect(alfrescoService.deleteNode).toHaveBeenCalled();
});
it('should require node id to delete', () => {
spyOn(alfrescoService, 'deleteNode').and.callThrough();
let file = new FileNode();
file.entry.id = null;
service.getHandler('delete')(file);
expect(alfrescoService.deleteNode).not.toHaveBeenCalled();
});
it('should reload target upon node deletion', () => {
spyOn(alfrescoService, 'deleteNode').and.callThrough();
let target = jasmine.createSpyObj('obj', ['reload']);
let file = new FileNode();
service.getHandler('delete')(file, target);
expect(alfrescoService.deleteNode).toHaveBeenCalled();
expect(target.reload).toHaveBeenCalled();
});
});