From 6feb6edd02ce349fcdf05df3c5566bf232fb7f25 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Tue, 28 Jun 2016 15:40:29 +0100 Subject: [PATCH] Unit tests --- .../src/components/content-action.spec.ts | 78 +++++++++++++++---- .../src/components/content-action.ts | 4 +- .../src/components/content-column.spec.ts | 16 ++++ 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.spec.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.spec.ts index 010bf39984..08d260980c 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.spec.ts @@ -21,23 +21,28 @@ import { expect, beforeEach } from '@angular/core/testing'; -import {EventEmitter} from '@angular/core'; +import { EventEmitter } from '@angular/core'; -import {DocumentList} from './document-list'; -import {AlfrescoServiceMock} from '../assets/alfresco.service.mock'; - -import {ContentActionList} from './content-action-list'; -import {ContentAction} from './content-action'; -import {DocumentActionsService} from '../services/document-actions.service'; -import {FolderActionsService} from '../services/folder-actions.service'; +import { DocumentList } from './document-list'; +import { AlfrescoServiceMock } from '../assets/alfresco.service.mock'; +import { ContentActionList } from './content-action-list'; +import { ContentAction } from './content-action'; +import { DocumentActionsService } from '../services/document-actions.service'; +import { FolderActionsService } from '../services/folder-actions.service'; +import { ContentActionHandler } from '../models/content-action.model'; describe('ContentAction', () => { let documentList: DocumentList; let actionList: ContentActionList; + let documentActions: DocumentActionsService; + let folderActions: FolderActionsService; beforeEach(() => { let alfrescoServiceMock = new AlfrescoServiceMock(); + documentActions = new DocumentActionsService(null, null); + folderActions = new FolderActionsService(null); + documentList = new DocumentList(alfrescoServiceMock, null); actionList = new ContentActionList(documentList); }); @@ -73,7 +78,6 @@ describe('ContentAction', () => { it('should get action handler from document actions service', () => { let handler = function() {}; - let documentActions = new DocumentActionsService(null); spyOn(documentActions, 'getHandler').and.returnValue(handler); let action = new ContentAction(actionList, documentActions, null); @@ -91,7 +95,6 @@ describe('ContentAction', () => { it('should get action handler from folder actions service', () => { let handler = function() {}; - let folderActions = new FolderActionsService(); spyOn(folderActions, 'getHandler').and.returnValue(handler); let action = new ContentAction(actionList, null, folderActions); @@ -108,10 +111,7 @@ describe('ContentAction', () => { }); it('should require target to get system handler', () => { - let folderActions = new FolderActionsService(); spyOn(folderActions, 'getHandler').and.stub(); - - let documentActions = new DocumentActionsService(null); spyOn(documentActions, 'getHandler').and.stub(); let action = new ContentAction(actionList, documentActions, folderActions); @@ -133,7 +133,6 @@ describe('ContentAction', () => { }); it('should be case insensitive for document target', () => { - let documentActions = new DocumentActionsService(null); spyOn(documentActions, 'getHandler').and.stub(); let action = new ContentAction(actionList, documentActions, null); @@ -146,7 +145,6 @@ describe('ContentAction', () => { }); it('should be case insensitive for folder target', () => { - let folderActions = new FolderActionsService(); spyOn(folderActions, 'getHandler').and.stub(); let action = new ContentAction(actionList, null, folderActions); @@ -177,4 +175,54 @@ describe('ContentAction', () => { let model = documentList.actions[0]; model.handler(''); }); + + it('should sync localizable fields with model', () => { + + let action = new ContentAction(actionList, null, null); + action.title = 'title1'; + action.ngOnInit(); + + expect(action.model.title).toBe(action.title); + + action.title = 'title2'; + action.ngOnChanges(null); + + expect(action.model.title).toBe('title2'); + }); + + it('should not find document action handler with missing service', () => { + let action = new ContentAction(actionList, null, null); + expect(action.getSystemHandler('document', 'name')).toBeNull(); + }); + + it('should not find folder action handler with missing service', () => { + let action = new ContentAction(actionList, null, null); + expect(action.getSystemHandler('folder', 'name')).toBeNull(); + }); + + it('should find document action handler via service', () => { + let handler = function (obj: any, target?: any) {}; + let action = new ContentAction(actionList, documentActions, null); + spyOn(documentActions, 'getHandler').and.returnValue(handler); + expect(action.getSystemHandler('document', 'name')).toBe(handler); + }); + + it('should find folder action handler via service', () => { + let handler = function (obj: any, target?: any) {}; + let action = new ContentAction(actionList, null, folderActions); + spyOn(folderActions, 'getHandler').and.returnValue(handler); + expect(action.getSystemHandler('folder', 'name')).toBe(handler); + }); + + it('should not find actions for unknown target type', () => { + spyOn(folderActions, 'getHandler').and.stub(); + spyOn(documentActions, 'getHandler').and.stub(); + + let action = new ContentAction(actionList, documentActions, folderActions); + + expect(action.getSystemHandler('unknown', 'name')).toBeNull(); + expect(folderActions.getHandler).not.toHaveBeenCalled(); + expect(documentActions.getHandler).not.toHaveBeenCalled(); + + }); }); diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts index 1bbff3ee6c..33164fdea5 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts @@ -76,12 +76,12 @@ export class ContentAction implements OnInit, OnChanges { this.list.registerAction(this.model); } - ngOnChanges() { + ngOnChanges(changes) { // update localizable properties this.model.title = this.title; } - private getSystemHandler(target: string, name: string): ContentActionHandler { + getSystemHandler(target: string, name: string): ContentActionHandler { if (target) { let ltarget = target.toLowerCase(); diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-column.spec.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-column.spec.ts index b3aa603c31..9f335ec6a3 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-column.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-column.spec.ts @@ -77,4 +77,20 @@ describe('ContentColumn', () => { expect(model.srTitle).toBe('Thumbnail'); }); + it('should sync localizable fields with model', () => { + + let column = new ContentColumn(columnList); + column.title = 'title1'; + column.srTitle = 'srTitle1'; + column.ngOnInit(); + + expect(column.model.title).toBe(column.title); + expect(column.model.srTitle).toBe(column.srTitle); + + column.title = 'title2'; + column.ngOnChanges(null); + + expect(column.model.title).toBe('title2'); + }); + });