diff --git a/ng2-components/ng2-activiti-processlist/karma-test-shim.js b/ng2-components/ng2-activiti-processlist/karma-test-shim.js index fa7b29a843..b931cf3867 100644 --- a/ng2-components/ng2-activiti-processlist/karma-test-shim.js +++ b/ng2-components/ng2-activiti-processlist/karma-test-shim.js @@ -54,6 +54,7 @@ var map = { 'ng2-translate': 'npm:ng2-translate', 'alfresco-js-api': 'npm:alfresco-js-api/dist', + 'ng2-activiti-form': 'npm:ng2-activiti-form/dist', 'ng2-activiti-tasklist': 'npm:ng2-activiti-tasklist/dist', 'ng2-alfresco-core': 'npm:ng2-alfresco-core/dist', 'ng2-alfresco-datatable': 'npm:ng2-alfresco-datatable/dist' @@ -65,6 +66,7 @@ var packages = { 'ng2-translate': { defaultExtension: 'js' }, 'alfresco-js-api': { main: './alfresco-js-api.js', defaultExtension: 'js'}, + 'ng2-activiti-form': { main: './index.js', defaultExtension: 'js'}, 'ng2-activiti-tasklist': { main: './index.js', defaultExtension: 'js'}, 'ng2-alfresco-core': { main: './index.js', defaultExtension: 'js'}, 'ng2-alfresco-datatable': { main: './index.js', defaultExtension: 'js'} diff --git a/ng2-components/ng2-activiti-processlist/karma.conf.js b/ng2-components/ng2-activiti-processlist/karma.conf.js index 1cdd488ff6..3eb9845474 100644 --- a/ng2-components/ng2-activiti-processlist/karma.conf.js +++ b/ng2-components/ng2-activiti-processlist/karma.conf.js @@ -46,7 +46,7 @@ module.exports = function (config) { { pattern: 'node_modules/ng2-alfresco-core/dist/**/*.*', included: false, served: true, watched: false }, { pattern: 'node_modules/ng2-alfresco-datatable/dist/**/*.*', included: false, served: true, watched: false }, { pattern: 'node_modules/ng2-activiti-tasklist/dist/**/*.js', included: false, served: true, watched: false }, - { pattern: 'node_modules/ng2-activiti-form/dist/**/*.js', included: false, served: true, watched: false }, + { pattern: 'node_modules/ng2-activiti-form/dist/**/*.*', included: false, served: true, watched: false }, // paths to support debugging with source maps in dev tools {pattern: 'src/**/*.ts', included: false, watched: false}, diff --git a/ng2-components/ng2-activiti-processlist/src/assets/translation.service.mock.ts b/ng2-components/ng2-activiti-processlist/src/assets/translation.service.mock.ts index 023104944a..539b2af021 100644 --- a/ng2-components/ng2-activiti-processlist/src/assets/translation.service.mock.ts +++ b/ng2-components/ng2-activiti-processlist/src/assets/translation.service.mock.ts @@ -16,22 +16,13 @@ */ import { Observable } from 'rxjs/Rx'; -import { EventEmitter } from '@angular/core'; - -export interface LangChangeEvent { - lang: string; - translations: any; -} export class TranslationMock { - public onLangChange: EventEmitter = new EventEmitter(); - public get(key: string|Array, interpolateParams?: Object): Observable { return Observable.of(key); } addTranslationFolder() { - } } diff --git a/ng2-components/ng2-activiti-processlist/src/components/activiti-start-process.component.spec.ts b/ng2-components/ng2-activiti-processlist/src/components/activiti-start-process.component.spec.ts new file mode 100644 index 0000000000..1c8becbf26 --- /dev/null +++ b/ng2-components/ng2-activiti-processlist/src/components/activiti-start-process.component.spec.ts @@ -0,0 +1,143 @@ +/*! + * @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, async } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; +import { DebugElement } from '@angular/core'; +import { Observable } from 'rxjs/Rx'; +import { ActivitiFormModule } from 'ng2-activiti-form'; +import { AlfrescoTranslationService, CoreModule } from 'ng2-alfresco-core'; +import { FormService } from 'ng2-activiti-form'; +import { TranslationMock } from './../assets/translation.service.mock'; +import { ActivitiStartProcessButton } from './activiti-start-process.component'; +import { ActivitiProcessService } from '../services/activiti-process.service'; + +describe('ActivitiStartProcessButton', () => { + + let componentHandler: any; + let component: ActivitiStartProcessButton; + let fixture: ComponentFixture; + let processService: ActivitiProcessService; + let formService: FormService; + let getDefinitionsSpy: jasmine.Spy; + let startProcessSpy: jasmine.Spy; + let de: DebugElement; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [ CoreModule, ActivitiFormModule ], + declarations: [ + ActivitiStartProcessButton + ], + providers: [ + { provide: AlfrescoTranslationService, useClass: TranslationMock }, + ActivitiProcessService, + FormService + ] + }).compileComponents(); + })); + + beforeEach(() => { + + fixture = TestBed.createComponent(ActivitiStartProcessButton); + component = fixture.componentInstance; + processService = fixture.debugElement.injector.get(ActivitiProcessService); + formService = fixture.debugElement.injector.get(FormService); + + getDefinitionsSpy = spyOn(processService, 'getProcessDefinitions').and.returnValue(Observable.of([{ + id: 'my:process1', + name: 'My Process 1' + }, { + id: 'my:process2', + name: 'My Process 2' + }])); + startProcessSpy = spyOn(processService, 'startProcess').and.returnValue(Observable.of({ + id: '32323', + name: 'Process' + })); + + componentHandler = jasmine.createSpyObj('componentHandler', [ + 'upgradeAllRegistered', + 'upgradeElement' + ]); + window['componentHandler'] = componentHandler; + }); + + it('should display the correct number of processes in the select list', () => { + fixture.detectChanges(); + de = fixture.debugElement.query(By.css('select')); + expect(de.children.length).toBe(2); + }); + + it('should display the correct process def details', (done) => { + fixture.detectChanges(); + fixture.whenStable().then(() => { + de = fixture.debugElement.query(By.css('select option')); + let optionEl: HTMLOptionElement = de.nativeElement; + expect(optionEl.value).toBe('my:process1'); + expect(optionEl.textContent.trim()).toBe('My Process 1'); + done(); + }); + }); + + it('should call service to start process if required fields provided', (done) => { + component.name = 'My new process'; + component.processDefinitionId = 'my:process1'; + component.showDialog(); + fixture.detectChanges(); + component.startProcess(); + fixture.whenStable().then(() => { + expect(startProcessSpy).toHaveBeenCalled(); + done(); + }); + }); + + it('should avoid calling service to start process if required fields NOT provided', (done) => { + component.showDialog(); + fixture.detectChanges(); + component.startProcess(); + fixture.whenStable().then(() => { + expect(startProcessSpy).not.toHaveBeenCalled(); + done(); + }); + }); + + it('should call service to start process with the correct parameters', (done) => { + component.name = 'My new process'; + component.processDefinitionId = 'my:process1'; + component.showDialog(); + fixture.detectChanges(); + component.startProcess(); + fixture.whenStable().then(() => { + expect(startProcessSpy).toHaveBeenCalledWith('my:process1', 'My new process', undefined); + done(); + }); + }); + + it('should indicate start form is missing when process does not have a start form', (done) => { + component.name = 'My new process'; + component.processDefinitionId = 'my:process1'; + component.showDialog(); + fixture.detectChanges(); + component.startProcess(); + fixture.whenStable().then(() => { + expect(component.isStartFormMissingOrValid()).toBe(true); + done(); + }); + }); + +}); diff --git a/ng2-components/ng2-activiti-processlist/src/components/activiti-start-process.component.ts b/ng2-components/ng2-activiti-processlist/src/components/activiti-start-process.component.ts index ec840ae3aa..a2dec7d863 100644 --- a/ng2-components/ng2-activiti-processlist/src/components/activiti-start-process.component.ts +++ b/ng2-components/ng2-activiti-processlist/src/components/activiti-start-process.component.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { Component, Input, OnInit, ViewChild } from '@angular/core'; +import { Component, Input, OnInit, ViewChild, DebugElement } from '@angular/core'; import { AlfrescoTranslationService } from 'ng2-alfresco-core'; import { ActivitiStartForm } from 'ng2-activiti-form'; import { ActivitiProcessService } from './../services/activiti-process.service'; @@ -34,7 +34,7 @@ export class ActivitiStartProcessButton implements OnInit { appId: string; @ViewChild('dialog') - dialog: any; + dialog: DebugElement; @ViewChild('startForm') startForm: ActivitiStartForm; @@ -68,9 +68,7 @@ export class ActivitiStartProcessButton implements OnInit { } public showDialog() { - if (this.dialog) { - this.dialog.nativeElement.showModal(); - } + this.dialog.nativeElement.showModal(); } public startProcess() { @@ -90,9 +88,7 @@ export class ActivitiStartProcessButton implements OnInit { } public cancel() { - if (this.dialog) { - this.dialog.nativeElement.close(); - } + this.dialog.nativeElement.close(); } private getSelectedProcess(): any {