[ADF-1432] Can not create a task (#2247)

* Changed assignee  to assigneeId to avoid script error
* Modified unit tests
This commit is contained in:
Deepak Paul
2017-08-25 20:59:14 +05:30
committed by Maurizio Vitale
parent d71c9089d2
commit 261149f4c5
3 changed files with 163 additions and 130 deletions

View File

@@ -43,10 +43,10 @@
</md-grid-tile> </md-grid-tile>
<md-grid-tile> <md-grid-tile>
<md-select placeholder="{{'START_TASK.FORM.LABEL.ASSIGNEE'|translate}}" id="assignee_id" <md-select placeholder="{{'START_TASK.FORM.LABEL.ASSIGNEE'|translate}}" id="assignee_id"
class="adf-start-task-input-container" [(ngModel)]="assignee"> class="adf-start-task-input-container" [(ngModel)]="assigneeId">
<md-option>{{'START_TASK.FORM.LABEL.NONE'|translate}}</md-option> <md-option>{{'START_TASK.FORM.LABEL.NONE'|translate}}</md-option>
<span *ngFor="let user of people"> <span *ngFor="let user of people">
<md-option [value]="user" *ngIf="!isUserNameEmpty(user)">{{ getDisplayUser(user.firstName, <md-option [value]="user.id" *ngIf="!isUserNameEmpty(user)">{{ getDisplayUser(user.firstName,
user.lastName, ' ')}} user.lastName, ' ')}}
</md-option> </md-option>
</span> </span>

View File

@@ -20,7 +20,6 @@ import { MdButtonModule, MdDatepickerModule, MdGridListModule, MdIconModule, MdI
import { AlfrescoTranslationService, CoreModule } from 'ng2-alfresco-core'; import { AlfrescoTranslationService, CoreModule } from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import { StartTaskModel } from '../models/start-task.model'; import { StartTaskModel } from '../models/start-task.model';
import { User } from '../models/user.model';
import { PeopleService } from '../services/people.service'; import { PeopleService } from '../services/people.service';
import { TaskListService } from '../services/tasklist.service'; import { TaskListService } from '../services/tasklist.service';
import { startTaskMock } from './../assets/start-task.mock'; import { startTaskMock } from './../assets/start-task.mock';
@@ -28,7 +27,7 @@ import { StartTaskComponent } from './start-task.component';
describe('StartTaskComponent', () => { describe('StartTaskComponent', () => {
let activitiStartTaskComponent: StartTaskComponent; let component: StartTaskComponent;
let fixture: ComponentFixture<StartTaskComponent>; let fixture: ComponentFixture<StartTaskComponent>;
let service: TaskListService; let service: TaskListService;
let peopleService: PeopleService; let peopleService: PeopleService;
@@ -74,7 +73,7 @@ describe('StartTaskComponent', () => {
spyOn(translateService.translate, 'get').and.callFake((key) => { return Observable.of(key); }); spyOn(translateService.translate, 'get').and.callFake((key) => { return Observable.of(key); });
fixture = TestBed.createComponent(StartTaskComponent); fixture = TestBed.createComponent(StartTaskComponent);
activitiStartTaskComponent = fixture.componentInstance; component = fixture.componentInstance;
element = fixture.nativeElement; element = fixture.nativeElement;
service = fixture.debugElement.injector.get(TaskListService); service = fixture.debugElement.injector.get(TaskListService);
@@ -107,11 +106,11 @@ describe('StartTaskComponent', () => {
}); });
it('should fetch fakeform on ngonint', () => { it('should fetch fakeform on ngonint', () => {
activitiStartTaskComponent.ngOnInit(); component.ngOnInit();
expect(activitiStartTaskComponent.forms).toEqual(fakeForms); expect(component.forms).toEqual(fakeForms);
expect(activitiStartTaskComponent.forms[0].name).toEqual('Display Data'); expect(component.forms[0].name).toEqual('Display Data');
expect(activitiStartTaskComponent.forms[1].name).toEqual('Employee Info'); expect(component.forms[1].name).toEqual('Employee Info');
expect(activitiStartTaskComponent.forms[1].id).toEqual(1111); expect(component.forms[1].id).toEqual(1111);
expect(getFormlistSpy).toHaveBeenCalled(); expect(getFormlistSpy).toHaveBeenCalled();
}); });
@@ -128,53 +127,62 @@ describe('StartTaskComponent', () => {
)); ));
}); });
it('should create new task when start is clicked', async(() => { it('should create new task when start is clicked', () => {
activitiStartTaskComponent.success.subscribe((res) => { let successSpy = spyOn(component.success, 'emit');
expect(res).toBeDefined(); component.appId = 'fakeAppId';
}); component.startTaskmodel = new StartTaskModel(startTaskMock);
activitiStartTaskComponent.appId = 'fakeAppId';
activitiStartTaskComponent.startTaskmodel = new StartTaskModel(startTaskMock);
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click();
}));
it('should send on success event when the task is started', async(() => {
activitiStartTaskComponent.success.subscribe((res) => {
expect(res).toBeDefined();
expect(res.id).toBe(91);
expect(res.name).toBe('fakeName');
expect(res.formKey).toBe(null);
expect(res.assignee).toBe(null);
});
activitiStartTaskComponent.appId = 'fakeAppId';
activitiStartTaskComponent.startTaskmodel = new StartTaskModel(startTaskMock);
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click();
}));
it('should send on success event when only name is given', async(() => {
activitiStartTaskComponent.success.subscribe((res) => {
expect(res).toBeDefined();
});
activitiStartTaskComponent.appId = 'fakeAppId';
activitiStartTaskComponent.startTaskmodel.name = 'fakeName';
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click();
}));
it('should not emit success event when data not present', async(() => {
let successSpy: jasmine.Spy = spyOn(activitiStartTaskComponent.success, 'emit');
activitiStartTaskComponent.startTaskmodel = new StartTaskModel(null);
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click();
fixture.detectChanges(); fixture.detectChanges();
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click();
expect(successSpy).toHaveBeenCalled();
});
it('should send on success event when the task is started', () => {
let successSpy = spyOn(component.success, 'emit');
component.appId = 'fakeAppId';
component.startTaskmodel = new StartTaskModel(startTaskMock);
fixture.detectChanges();
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click();
expect(successSpy).toHaveBeenCalledWith({
id: 91,
name: 'fakeName',
formKey: null,
assignee: null
});
});
it('should send on success event when only name is given', () => {
let successSpy = spyOn(component.success, 'emit');
component.appId = 'fakeAppId';
component.startTaskmodel.name = 'fakeName';
fixture.detectChanges();
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click();
expect(successSpy).toHaveBeenCalled();
});
it('should not emit success event when data not present', () => {
let successSpy = spyOn(component.success, 'emit');
component.startTaskmodel = new StartTaskModel(null);
fixture.detectChanges();
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click();
expect(createNewTaskSpy).not.toHaveBeenCalled(); expect(createNewTaskSpy).not.toHaveBeenCalled();
expect(successSpy).not.toHaveBeenCalled(); expect(successSpy).not.toHaveBeenCalled();
})); });
}); });
describe('assign form', () => { describe('attach form', () => {
beforeEach(() => { beforeEach(() => {
spyOn(service, 'createNewTask').and.returnValue(Observable.of(
{
id: 91,
name: 'fakeName',
formKey: null,
assignee: null
}
));
attachFormSpy = spyOn(service, 'attachFormToATask').and.returnValue(Observable.of( attachFormSpy = spyOn(service, 'attachFormToATask').and.returnValue(Observable.of(
{ {
id: 91, id: 91,
@@ -186,38 +194,57 @@ describe('StartTaskComponent', () => {
}); });
it('should attach form to the task when a form is selected', () => { it('should attach form to the task when a form is selected', () => {
activitiStartTaskComponent.success.subscribe((res) => { let successSpy = spyOn(component.success, 'emit');
expect(res).toBeDefined(); component.appId = 'fakeAppId';
expect(res.id).toBe(91); component.startTaskmodel = new StartTaskModel(startTaskMock);
expect(res.name).toBe('fakeName'); component.formKey = 1204;
expect(res.formKey).toBe(1204); fixture.detectChanges();
expect(res.assignee).toBe(null);
});
activitiStartTaskComponent.appId = 'fakeAppId';
activitiStartTaskComponent.formKey = 1204;
activitiStartTaskComponent.startTaskmodel = new StartTaskModel(startTaskMock);
let createTaskButton = <HTMLElement> element.querySelector('#button-start'); let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click(); createTaskButton.click();
expect(successSpy).toHaveBeenCalledWith({
id: 91,
name: 'fakeName',
formKey: 1204,
assignee: null
});
}); });
it('should not attach form to the task when a no form is selected', () => { it('should not attach form to the task when a no form is selected', () => {
activitiStartTaskComponent.success.subscribe((res) => { let successSpy = spyOn(component.success, 'emit');
expect(res).toBeDefined(); component.appId = 'fakeAppId';
expect(res.id).toBe(91); component.startTaskmodel = new StartTaskModel(startTaskMock);
expect(res.name).toBe('fakeName'); component.formKey = null;
expect(res.formKey).toBe(null); fixture.detectChanges();
});
activitiStartTaskComponent.appId = 'fakeAppId';
activitiStartTaskComponent.formKey = null;
activitiStartTaskComponent.startTaskmodel = new StartTaskModel(startTaskMock);
let createTaskButton = <HTMLElement> element.querySelector('#button-start'); let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click(); createTaskButton.click();
expect(successSpy).toHaveBeenCalledWith({
id: 91,
name: 'fakeName',
formKey: null,
assignee: null
});
}); });
}); });
describe('assign task', () => { describe('assign user', () => {
beforeEach(() => { beforeEach(() => {
assignUserSpy = spyOn(service, 'assignTask').and.returnValue(Observable.of( spyOn(service, 'createNewTask').and.returnValue(Observable.of(
{
id: 91,
name: 'fakeName',
formKey: null,
assignee: null
}
));
spyOn(service, 'attachFormToATask').and.returnValue(Observable.of(
{
id: 91,
name: 'fakeName',
formKey: 1204,
assignee: null
}
));
assignUserSpy = spyOn(service, 'assignTaskByUserId').and.returnValue(Observable.of(
{ {
id: 91, id: 91,
name: 'fakeName', name: 'fakeName',
@@ -228,39 +255,41 @@ describe('StartTaskComponent', () => {
}); });
it('should assign task when an assignee is selected', () => { it('should assign task when an assignee is selected', () => {
activitiStartTaskComponent.success.subscribe((res) => { let successSpy = spyOn(component.success, 'emit');
expect(res).toBeDefined(); component.appId = 'fakeAppId';
expect(res.id).toBe(91); component.startTaskmodel = new StartTaskModel(startTaskMock);
expect(res.name).toBe('fakeName'); component.formKey = 1204;
expect(res.formKey).toBe(1204); component.assigneeId = testUser.id;
expect(res.assignee).toBe(testUser); fixture.detectChanges();
});
activitiStartTaskComponent.appId = 'fakeAppId';
activitiStartTaskComponent.formKey = 1204;
activitiStartTaskComponent.assignee = new User(testUser);
activitiStartTaskComponent.startTaskmodel = new StartTaskModel(startTaskMock);
let createTaskButton = <HTMLElement> element.querySelector('#button-start'); let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click(); createTaskButton.click();
expect(successSpy).toHaveBeenCalledWith({
id: 91,
name: 'fakeName',
formKey: 1204,
assignee: testUser
});
}); });
it('should not assign task when no assignee is selected', () => { it('should not assign task when no assignee is selected', () => {
activitiStartTaskComponent.success.subscribe((res) => { let successSpy = spyOn(component.success, 'emit');
expect(res).toBeDefined(); component.appId = 'fakeAppId';
expect(res.id).toBe(91); component.formKey = 1204;
expect(res.name).toBe('fakeName'); component.assigneeId = null;
expect(res.formKey).toBe(1204); component.startTaskmodel = new StartTaskModel(startTaskMock);
expect(res.assignee).toBe(null); fixture.detectChanges();
});
activitiStartTaskComponent.appId = 'fakeAppId';
activitiStartTaskComponent.formKey = 1204;
activitiStartTaskComponent.assignee = null;
activitiStartTaskComponent.startTaskmodel = new StartTaskModel(startTaskMock);
let createTaskButton = <HTMLElement> element.querySelector('#button-start'); let createTaskButton = <HTMLElement> element.querySelector('#button-start');
createTaskButton.click(); createTaskButton.click();
expect(successSpy).toHaveBeenCalledWith({
id: 91,
name: 'fakeName',
formKey: 1204,
assignee: null
});
}); });
}); });
it('should not attach a task when a form id is not slected', () => { it('should not attach a form when a form id is not slected', () => {
let attachFormToATask = spyOn(service, 'attachFormToATask').and.returnValue(Observable.of()); let attachFormToATask = spyOn(service, 'attachFormToATask').and.returnValue(Observable.of());
spyOn(service, 'createNewTask').and.callFake( spyOn(service, 'createNewTask').and.callFake(
function() { function() {
@@ -270,7 +299,8 @@ describe('StartTaskComponent', () => {
}); });
}); });
let createTaskButton = <HTMLElement> element.querySelector('#button-start'); let createTaskButton = <HTMLElement> element.querySelector('#button-start');
activitiStartTaskComponent.startTaskmodel.name = 'fake-name'; component.startTaskmodel.name = 'fake-name';
fixture.detectChanges();
createTaskButton.click(); createTaskButton.click();
expect(attachFormToATask).not.toHaveBeenCalled(); expect(attachFormToATask).not.toHaveBeenCalled();
}); });
@@ -281,34 +311,34 @@ describe('StartTaskComponent', () => {
expect(element.querySelector('#button-start').textContent).toContain('START_TASK.FORM.ACTION.START'); expect(element.querySelector('#button-start').textContent).toContain('START_TASK.FORM.ACTION.START');
}); });
it('should fetch all users on ngonint', async(() => { it('should fetch all users on ngonint', () => {
activitiStartTaskComponent.ngOnInit(); component.ngOnInit();
expect(activitiStartTaskComponent.people).toBeDefined(); expect(component.people).toBeDefined();
expect(activitiStartTaskComponent.people[0].firstName).toEqual('fakeName'); expect(component.people[0].firstName).toEqual('fakeName');
expect(activitiStartTaskComponent.people[1].firstName).toEqual('fake-name'); expect(component.people[1].firstName).toEqual('fake-name');
expect(activitiStartTaskComponent.people[0].id).toEqual(1); expect(component.people[0].id).toEqual(1);
expect(activitiStartTaskComponent.people[1].id).toEqual(1001); expect(component.people[1].id).toEqual(1001);
expect(getWorkflowUsersSpy).toHaveBeenCalled(); expect(getWorkflowUsersSpy).toHaveBeenCalled();
})); });
it('should not emit TaskDetails OnCancle', () => { it('should not emit TaskDetails OnCancel', () => {
let emitSpy = spyOn(activitiStartTaskComponent.cancel, 'emit'); let emitSpy = spyOn(component.cancel, 'emit');
activitiStartTaskComponent.onCancel(); component.onCancel();
expect(emitSpy).not.toBeNull(); expect(emitSpy).not.toBeNull();
expect(emitSpy).toHaveBeenCalled(); expect(emitSpy).toHaveBeenCalled();
}); });
it('should start button disable if name is empty', () => { it('should disable start button if name is empty', () => {
let createTaskButton = fixture.nativeElement.querySelector('#button-start'); component.startTaskmodel.name = '';
activitiStartTaskComponent.startTaskmodel.name = '';
fixture.detectChanges(); fixture.detectChanges();
let createTaskButton = fixture.nativeElement.querySelector('#button-start');
expect(createTaskButton.disabled).toBeTruthy(); expect(createTaskButton.disabled).toBeTruthy();
}); });
it('should cancle start task on cancle button clicked', () => { it('should cancel start task on cancel button click', () => {
let emitSpy = spyOn(activitiStartTaskComponent.cancel, 'emit'); let emitSpy = spyOn(component.cancel, 'emit');
let cancleTaskButton = fixture.nativeElement.querySelector('#button-cancle'); let cancleTaskButton = fixture.nativeElement.querySelector('#button-cancle');
activitiStartTaskComponent.startTaskmodel.name = ''; component.startTaskmodel.name = '';
fixture.detectChanges(); fixture.detectChanges();
cancleTaskButton.click(); cancleTaskButton.click();
expect(emitSpy).not.toBeNull(); expect(emitSpy).not.toBeNull();
@@ -316,24 +346,23 @@ describe('StartTaskComponent', () => {
}); });
it('should enable start button if name is filled out', () => { it('should enable start button if name is filled out', () => {
activitiStartTaskComponent.startTaskmodel.name = 'fakeName'; component.startTaskmodel.name = 'fakeName';
fixture.detectChanges(); fixture.detectChanges();
let createTaskButton = fixture.nativeElement.querySelector('#button-start'); let createTaskButton = fixture.nativeElement.querySelector('#button-start');
expect(createTaskButton.enable).toBeFalsy(); expect(createTaskButton.disabled).toBeFalsy();
}); });
it('should defined the select option for Assignee', () => { it('should define the select option for Assignee', async(() => {
fixture.detectChanges();
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
let selectElement = fixture.nativeElement.querySelector('#assignee_id'); let selectElement = fixture.nativeElement.querySelector('#assignee_id');
expect(selectElement).not.toBeNull(); expect(selectElement).not.toBeNull();
expect(selectElement).toBeDefined(); expect(selectElement).toBeDefined();
expect(selectElement.innerText.trim()).toBe('START_TASK.FORM.LABEL.ASSIGNEE'); expect(selectElement.innerText.trim()).toBe('START_TASK.FORM.LABEL.ASSIGNEE');
}); });
}); }));
it('should defined the select option for Forms', () => { it('should define the select option for Forms', () => {
activitiStartTaskComponent.forms = fakeForms; component.forms = fakeForms;
fixture.detectChanges(); fixture.detectChanges();
let selectElement = fixture.nativeElement.querySelector('#form_id'); let selectElement = fixture.nativeElement.querySelector('#form_id');
expect(selectElement.innerText.trim()).toBe('START_TASK.FORM.LABEL.FORM'); expect(selectElement.innerText.trim()).toBe('START_TASK.FORM.LABEL.FORM');
@@ -345,10 +374,10 @@ describe('StartTaskComponent', () => {
let testUser3 = {'id': 1003, 'firstName': 'Wilbur', 'lastName': '', 'email': 'wilbur@app.activiti.com'}; let testUser3 = {'id': 1003, 'firstName': 'Wilbur', 'lastName': '', 'email': 'wilbur@app.activiti.com'};
let testUser4 = {'id': 1004, 'firstName': '', 'lastName': '', 'email': 'test@app.activiti.com'}; let testUser4 = {'id': 1004, 'firstName': '', 'lastName': '', 'email': 'test@app.activiti.com'};
let testFullname1 = activitiStartTaskComponent.getDisplayUser(testUser1.firstName, testUser1.lastName, ' '); let testFullname1 = component.getDisplayUser(testUser1.firstName, testUser1.lastName, ' ');
let testFullname2 = activitiStartTaskComponent.getDisplayUser(testUser2.firstName, testUser2.lastName, ' '); let testFullname2 = component.getDisplayUser(testUser2.firstName, testUser2.lastName, ' ');
let testFullname3 = activitiStartTaskComponent.getDisplayUser(testUser3.firstName, testUser3.lastName, ' '); let testFullname3 = component.getDisplayUser(testUser3.firstName, testUser3.lastName, ' ');
let testFullname4 = activitiStartTaskComponent.getDisplayUser(testUser4.firstName, testUser4.lastName, ' '); let testFullname4 = component.getDisplayUser(testUser4.firstName, testUser4.lastName, ' ');
expect(testFullname1.trim()).toBe('Wilbur Adams'); expect(testFullname1.trim()).toBe('Wilbur Adams');
expect(testFullname2.trim()).toBe('Adams'); expect(testFullname2.trim()).toBe('Adams');
@@ -356,9 +385,13 @@ describe('StartTaskComponent', () => {
expect(testFullname4.trim()).toBe(''); expect(testFullname4.trim()).toBe('');
}); });
it('should not show the name if it is empty', () => { it('should emit error when there is an error while creating task', () => {
let testUser2 = {'id': 1001, 'firstName': '', 'lastName': '', 'email': 'wilbur@app.activiti.com'}; let errorSpy = spyOn(component.error, 'emit');
let isUserNameEmpty2 = activitiStartTaskComponent.isUserNameEmpty(testUser2); spyOn(service, 'createNewTask').and.returnValue(Observable.throw({}));
expect(isUserNameEmpty2).toBe(true); let createTaskButton = <HTMLElement> element.querySelector('#button-start');
component.startTaskmodel.name = 'fake-name';
fixture.detectChanges();
createTaskButton.click();
expect(errorSpy).toHaveBeenCalled();
}); });
}); });

View File

@@ -59,7 +59,7 @@ export class StartTaskComponent implements OnInit {
forms: Form[]; forms: Form[];
assignee: any; assigneeId: number;
formKey: number; formKey: number;
@@ -90,7 +90,7 @@ export class StartTaskComponent implements OnInit {
.switchMap((createRes: any) => .switchMap((createRes: any) =>
this.attachForm(createRes.id, this.formKey).defaultIfEmpty(createRes) this.attachForm(createRes.id, this.formKey).defaultIfEmpty(createRes)
.switchMap((attachRes: any) => .switchMap((attachRes: any) =>
this.assignTaskByUserId(createRes.id, this.assignee.id).defaultIfEmpty(attachRes ? attachRes : createRes) this.assignTaskByUserId(createRes.id, this.assigneeId).defaultIfEmpty(attachRes ? attachRes : createRes)
) )
) )
.subscribe( .subscribe(