From d85ff5f621155e4d4b866a68ad9a41b054628d41 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Mon, 9 May 2016 16:00:27 +0100 Subject: [PATCH] #9 Unit tests --- .../src/components/content-action.js | 10 +- .../src/components/content-action.js.map | 2 +- .../src/components/content-action.ts | 34 +++- .../test/components/content-action.spec.ts | 179 ++++++++++++++++++ .../test/components/document-list.spec.ts | 18 +- .../ng2-alfresco-documentlist/test/index.html | 1 + .../test/tsconfig.json | 1 + 7 files changed, 225 insertions(+), 20 deletions(-) create mode 100644 ng2-components/ng2-alfresco-documentlist/test/components/content-action.spec.ts diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.js b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.js index ae5f4cd340..aed977affa 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.js +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.js @@ -77,10 +77,16 @@ System.register(['angular2/core', './../models/content-action.model', './content if (target) { var ltarget = target.toLowerCase(); if (ltarget === 'document') { - return this.documentActions.getHandler(name); + if (this.documentActions) { + return this.documentActions.getHandler(name); + } + return null; } if (ltarget === 'folder') { - return this.folderActions.getHandler(name); + if (this.folderActions) { + return this.folderActions.getHandler(name); + } + return null; } } return null; diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.js.map b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.js.map index 63be1348a9..538cc3cdc4 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.js.map +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.js.map @@ -1 +1 @@ -{"version":3,"file":"content-action.js","sourceRoot":"","sources":["content-action.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YAYH;gBAQI,uBACY,IAAuB,EACvB,eAAuC,EACvC,aAAmC;oBAFnC,SAAI,GAAJ,IAAI,CAAmB;oBACvB,oBAAe,GAAf,e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AM,CAAC,IAAI,CAAC;gBAChB,CAAC;gBA/DD;oBAAC,YAAK,EAAE;;4DAAA;gBAGR;oBAAC,YAAK,EAAE;;2DAAA;gBAGR;oBAAC,YAAK,EAAE;;8DAAA;gBAGR;oBAAC,YAAK,EAAE;;2DAAA;gBAGR;oBAAC,YAAK,EAAE;;6DAAA;gBAGR;oBAAC,aAAM,EAAE;;8DAAA;gBArBb;oBAAC,gBAAS,CAAC;wBACP,QAAQ,EAAE,gBAAgB;wBAC1B,QAAQ,EAAE,EAAE;qBACf,CAAC;;iCAAA;gBAmEF,oBAAC;YAAD,CAAC,AAlED,IAkEC;YAlED,yCAkEC,CAAA"} \ No newline at end of file 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 efe2c873b3..3eb70f8485 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-action.ts @@ -26,12 +26,24 @@ import {FolderActionsService} from '../services/folder-actions.service'; template: '' }) export class ContentAction implements OnInit { - @Input() title: string = 'Action'; - @Input() icon: string; - @Input() handler: string; - @Input() type: string; - @Input() target: string; - @Output() execute = new EventEmitter(); + + @Input() + title: string = 'Action'; + + @Input() + icon: string; + + @Input() + handler: string; + + @Input() + type: string; + + @Input() + target: string; + + @Output() + execute = new EventEmitter(); constructor( private list: ContentActionList, @@ -64,11 +76,17 @@ export class ContentAction implements OnInit { let ltarget = target.toLowerCase(); if (ltarget === 'document') { - return this.documentActions.getHandler(name); + if (this.documentActions) { + return this.documentActions.getHandler(name); + } + return null; } if (ltarget === 'folder') { - return this.folderActions.getHandler(name); + if (this.folderActions) { + return this.folderActions.getHandler(name); + } + return null; } } return null; diff --git a/ng2-components/ng2-alfresco-documentlist/test/components/content-action.spec.ts b/ng2-components/ng2-alfresco-documentlist/test/components/content-action.spec.ts new file mode 100644 index 0000000000..48932490fd --- /dev/null +++ b/ng2-components/ng2-alfresco-documentlist/test/components/content-action.spec.ts @@ -0,0 +1,179 @@ +/** + * @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 { + it, + describe, + expect, + beforeEach +} from 'angular2/testing'; +import {EventEmitter} from 'angular2/core'; +import {DocumentList} from '../../src/components/document-list'; +import {AlfrescoServiceMock} from '../assets/alfresco.service.mock'; +import {ContentActionList} from '../../src/components/content-action-list'; +import {ContentAction} from '../../src/components/content-action'; +import {DocumentActionsService} from '../../src/services/document-actions.service'; +import {FolderActionsService} from '../../src/services/folder-actions.service'; + +describe('ContentAction', () => { + + let documentList: DocumentList; + let actionList: ContentActionList; + + beforeEach(() => { + let alfrescoServiceMock = new AlfrescoServiceMock(); + documentList = new DocumentList(alfrescoServiceMock); + actionList = new ContentActionList(documentList); + }); + + it('should register within parent actions list', () => { + spyOn(actionList, 'registerAction').and.stub(); + + let action = new ContentAction(actionList, null, null); + action.ngOnInit(); + + expect(actionList.registerAction).toHaveBeenCalled(); + }); + + it('should setup and register model', () => { + let action = new ContentAction(actionList, null, null); + action.type = 'button'; + action.target = 'document'; + action.title = ''; + action.icon = '<icon>'; + + expect(documentList.actions.length).toBe(0); + action.ngOnInit(); + + expect(documentList.actions.length).toBe(1); + + let model = documentList.actions[0]; + expect(model.type).toBe(action.type); + expect(model.target).toBe(action.target); + expect(model.title).toBe(action.title); + expect(model.icon).toBe(action.icon); + }); + + 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); + action.type = 'button'; + action.target = 'document'; + action.handler = '<handler>'; + action.ngOnInit(); + + expect(documentActions.getHandler).toHaveBeenCalledWith(action.handler); + expect(documentList.actions.length).toBe(1); + + let model = documentList.actions[0]; + expect(model.handler).toBe(handler); + }); + + 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); + action.type = 'button'; + action.target = 'folder'; + action.handler = '<handler>'; + action.ngOnInit(); + + expect(folderActions.getHandler).toHaveBeenCalledWith(action.handler); + expect(documentList.actions.length).toBe(1); + + let model = documentList.actions[0]; + expect(model.handler).toBe(handler); + }); + + 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); + action.type = 'button'; + action.handler = '<handler>'; + + action.ngOnInit(); + expect(documentList.actions.length).toBe(1); + expect(folderActions.getHandler).not.toHaveBeenCalled(); + expect(documentActions.getHandler).not.toHaveBeenCalled(); + + action.target = 'document'; + action.ngOnInit(); + expect(documentActions.getHandler).toHaveBeenCalled(); + + action.target = 'folder'; + action.ngOnInit(); + expect(folderActions.getHandler).toHaveBeenCalled(); + }); + + 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); + action.target = 'DoCuMeNt'; + action.type = 'button'; + action.handler = '<handler>'; + + action.ngOnInit(); + expect(documentActions.getHandler).toHaveBeenCalledWith(action.handler); + }); + + it('should be case insensitive for folder target', () => { + let folderActions = new FolderActionsService(); + spyOn(folderActions, 'getHandler').and.stub(); + + let action = new ContentAction(actionList, null, folderActions); + action.target = 'FoLdEr'; + action.type = 'button'; + action.handler = '<handler>'; + + action.ngOnInit(); + expect(folderActions.getHandler).toHaveBeenCalledWith(action.handler); + }); + + it('should use custom "execute" emitter', (done) => { + let emitter = new EventEmitter(); + + emitter.subscribe(e => { + expect(e.value).toBe('<obj>'); + done(); + }); + + let action = new ContentAction(actionList, null, null); + action.target = 'document'; + action.type = 'button'; + action.execute = emitter; + + action.ngOnInit(); + expect(documentList.actions.length).toBe(1); + + let model = documentList.actions[0]; + model.handler('<obj>'); + }); + +}); diff --git a/ng2-components/ng2-alfresco-documentlist/test/components/document-list.spec.ts b/ng2-components/ng2-alfresco-documentlist/test/components/document-list.spec.ts index 3298376ead..0627524030 100644 --- a/ng2-components/ng2-alfresco-documentlist/test/components/document-list.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/test/components/document-list.spec.ts @@ -24,7 +24,7 @@ import { import {DocumentList} from '../../src/components/document-list'; import {ContentColumnModel} from '../../src/models/content-column.model'; import {AlfrescoServiceMock} from '../assets/alfresco.service.mock'; -import {DocumentEntity} from '../../src/models/document-library.model'; +import {DocumentEntity, LocationEntity} from '../../src/models/document-library.model'; import {ContentActionModel} from '../../src/models/content-action.model'; describe('DocumentList', () => { @@ -304,14 +304,14 @@ describe('DocumentList', () => { }); it('should get node path', () => { - let node = { - fileName: 'fileName', - location: { - site: 'swsdp', - container: 'documentLibrary', - path: '\/' - } - }; + let location = new LocationEntity(); + location.site = 'swsdp'; + location.container = 'documentLibrary'; + location.path = '\/'; + + let node = new DocumentEntity(); + node.fileName = 'fileName'; + node.location = location; expect(documentList.getNodePath(node)).toBe('swsdp/documentLibrary/fileName'); }); diff --git a/ng2-components/ng2-alfresco-documentlist/test/index.html b/ng2-components/ng2-alfresco-documentlist/test/index.html index 5f5baff899..bb9b2155b3 100644 --- a/ng2-components/ng2-alfresco-documentlist/test/index.html +++ b/ng2-components/ng2-alfresco-documentlist/test/index.html @@ -37,6 +37,7 @@ // #3. Import the spec file explicitly Promise.all([ + System.import('test/components/content-action.spec'), System.import('test/components/document-list.spec'), System.import('test/components/content-column-list.spec'), System.import('test/components/content-column.spec'), diff --git a/ng2-components/ng2-alfresco-documentlist/test/tsconfig.json b/ng2-components/ng2-alfresco-documentlist/test/tsconfig.json index 288a503eb7..ea9b9a42dc 100644 --- a/ng2-components/ng2-alfresco-documentlist/test/tsconfig.json +++ b/ng2-components/ng2-alfresco-documentlist/test/tsconfig.json @@ -14,6 +14,7 @@ "../node_modules/angular2/typings/browser.d.ts", "../typings/main/ambient/jasmine/index.d.ts", "assets/alfresco.service.mock.ts", + "components/content-action.spec.ts", "components/document-list.spec.ts", "components/content-column-list.spec.ts", "components/content-column.spec.ts",