mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
[ADF-3282] Refactor Start Task Component (#4012)
* [ADF-3282] Refactor Start Task Component * [ADF-3282] Fix e2e test * [ADF-3282] Remove maxTaskNameLength from start task doc
This commit is contained in:
committed by
Eugenio Romano
parent
ca5543c48d
commit
49738ad555
@@ -16,33 +16,36 @@
|
||||
*/
|
||||
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { setupTestBed } from '@alfresco/adf-core';
|
||||
import { Observable, of, throwError } from 'rxjs';
|
||||
import { startTaskMock } from '../../mock';
|
||||
import { StartTaskModel } from '../models/start-task.model';
|
||||
import { setupTestBed, LogService } from '@alfresco/adf-core';
|
||||
import { of, throwError, Observable } from 'rxjs';
|
||||
import { TaskListService } from '../services/tasklist.service';
|
||||
import { StartTaskComponent } from './start-task.component';
|
||||
import { ProcessTestingModule } from '../../testing/process.testing.module';
|
||||
import { taskDetailsMock } from '../../mock/task/task-details.mock';
|
||||
import { TaskDetailsModel } from '../models/task-details.model';
|
||||
|
||||
describe('StartTaskComponent', () => {
|
||||
|
||||
let component: StartTaskComponent;
|
||||
let fixture: ComponentFixture<StartTaskComponent>;
|
||||
let service: TaskListService;
|
||||
let logService: LogService;
|
||||
let element: HTMLElement;
|
||||
let getFormListSpy: jasmine.Spy;
|
||||
let createNewTaskSpy: jasmine.Spy;
|
||||
let fakeForms = [
|
||||
let logSpy: jasmine.Spy;
|
||||
let fakeForms$ = [
|
||||
{
|
||||
id: 123,
|
||||
name: 'Display Data'
|
||||
name: 'Display Data'
|
||||
},
|
||||
{
|
||||
{
|
||||
id: 1111,
|
||||
name: 'Employee Info'
|
||||
name: 'Employee Info'
|
||||
}
|
||||
];
|
||||
let testUser = {id: 1001, firstName: 'fakeName', email: 'fake@app.activiti.com'};
|
||||
|
||||
let testUser = { id: 1001, firstName: 'fakeName', email: 'fake@app.activiti.com' };
|
||||
|
||||
setupTestBed({
|
||||
imports: [ProcessTestingModule]
|
||||
@@ -54,21 +57,28 @@ describe('StartTaskComponent', () => {
|
||||
element = fixture.nativeElement;
|
||||
|
||||
service = TestBed.get(TaskListService);
|
||||
getFormListSpy = spyOn(service, 'getFormList').and.returnValue(of(fakeForms));
|
||||
logService = TestBed.get(LogService);
|
||||
getFormListSpy = spyOn(service, 'getFormList').and.returnValue(new Observable((observer) => {
|
||||
observer.next(fakeForms$);
|
||||
observer.complete();
|
||||
}));
|
||||
|
||||
fixture.detectChanges();
|
||||
}));
|
||||
|
||||
afterEach(() => {
|
||||
fixture.destroy();
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
it('should create instance of StartTaskComponent', () => {
|
||||
expect(fixture.componentInstance instanceof StartTaskComponent).toBe(true, 'should create StartTaskComponent');
|
||||
expect(component instanceof StartTaskComponent).toBe(true, 'should create StartTaskComponent');
|
||||
});
|
||||
|
||||
it('should fetch fake form on init', () => {
|
||||
component.ngOnInit();
|
||||
expect(component.forms).toEqual(fakeForms);
|
||||
expect(component.forms[0].name).toEqual('Display Data');
|
||||
expect(component.forms[1].name).toEqual('Employee Info');
|
||||
expect(component.forms[1].id).toEqual(1111);
|
||||
fixture.detectChanges();
|
||||
expect(component.forms$).toBeDefined();
|
||||
expect(getFormListSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -87,8 +97,7 @@ describe('StartTaskComponent', () => {
|
||||
|
||||
it('should create new task when start is clicked', () => {
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.appId = 42;
|
||||
component.startTaskModel = new StartTaskModel(startTaskMock);
|
||||
component.taskForm.controls['name'].setValue('task');
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
createTaskButton.click();
|
||||
@@ -97,8 +106,8 @@ describe('StartTaskComponent', () => {
|
||||
|
||||
it('should send on success event when the task is started', () => {
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.appId = 42;
|
||||
component.startTaskModel = new StartTaskModel(startTaskMock);
|
||||
component.taskDetailsModel = new TaskDetailsModel(taskDetailsMock);
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
createTaskButton.click();
|
||||
@@ -113,7 +122,7 @@ describe('StartTaskComponent', () => {
|
||||
it('should send on success event when only name is given', () => {
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.appId = 42;
|
||||
component.startTaskModel.name = 'fakeName';
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
createTaskButton.click();
|
||||
@@ -122,7 +131,7 @@ describe('StartTaskComponent', () => {
|
||||
|
||||
it('should not emit success event when data not present', () => {
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.startTaskModel = new StartTaskModel(null);
|
||||
component.taskDetailsModel = new TaskDetailsModel(null);
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
createTaskButton.click();
|
||||
@@ -141,6 +150,9 @@ describe('StartTaskComponent', () => {
|
||||
assignee: null
|
||||
}
|
||||
));
|
||||
});
|
||||
|
||||
it('should attach form to the task when a form is selected', () => {
|
||||
spyOn(service, 'attachFormToATask').and.returnValue(of(
|
||||
{
|
||||
id: 91,
|
||||
@@ -149,13 +161,11 @@ describe('StartTaskComponent', () => {
|
||||
assignee: null
|
||||
}
|
||||
));
|
||||
});
|
||||
|
||||
it('should attach form to the task when a form is selected', () => {
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
component.taskForm.controls['formKey'].setValue(1204);
|
||||
component.appId = 42;
|
||||
component.startTaskModel = new StartTaskModel(startTaskMock);
|
||||
component.formKey = '1204';
|
||||
component.taskDetailsModel = new TaskDetailsModel(taskDetailsMock);
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
createTaskButton.click();
|
||||
@@ -168,10 +178,19 @@ describe('StartTaskComponent', () => {
|
||||
});
|
||||
|
||||
it('should not attach form to the task when a no form is selected', () => {
|
||||
spyOn(service, 'attachFormToATask').and.returnValue(of(
|
||||
{
|
||||
id: 91,
|
||||
name: 'fakeName',
|
||||
formKey: null,
|
||||
assignee: null
|
||||
}
|
||||
));
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
component.taskForm.controls['formKey'].setValue(null);
|
||||
component.appId = 42;
|
||||
component.startTaskModel = new StartTaskModel(startTaskMock);
|
||||
component.formKey = null;
|
||||
component.taskDetailsModel = new TaskDetailsModel(taskDetailsMock);
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
createTaskButton.click();
|
||||
@@ -214,9 +233,9 @@ describe('StartTaskComponent', () => {
|
||||
|
||||
it('should assign task when an assignee is selected', () => {
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
component.taskForm.controls['formKey'].setValue(1204);
|
||||
component.appId = 42;
|
||||
component.startTaskModel = new StartTaskModel(startTaskMock);
|
||||
component.formKey = '1204';
|
||||
component.assigneeId = testUser.id;
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
@@ -229,12 +248,31 @@ describe('StartTaskComponent', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should assign task with id of selected user assigned', () => {
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.taskDetailsModel = new TaskDetailsModel(taskDetailsMock);
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
component.taskForm.controls['formKey'].setValue(1204);
|
||||
component.appId = 42;
|
||||
component.getAssigneeId(testUser.id);
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
createTaskButton.click();
|
||||
expect(successSpy).toHaveBeenCalledWith({
|
||||
id: 91,
|
||||
name: 'fakeName',
|
||||
formKey: 1204,
|
||||
assignee: testUser
|
||||
});
|
||||
});
|
||||
|
||||
it('should not assign task when no assignee is selected', () => {
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
component.taskForm.controls['formKey'].setValue(1204);
|
||||
component.appId = 42;
|
||||
component.formKey = '1204';
|
||||
component.assigneeId = null;
|
||||
component.startTaskModel = new StartTaskModel(startTaskMock);
|
||||
component.taskDetailsModel = new TaskDetailsModel(taskDetailsMock);
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
createTaskButton.click();
|
||||
@@ -245,27 +283,10 @@ describe('StartTaskComponent', () => {
|
||||
assignee: null
|
||||
});
|
||||
});
|
||||
|
||||
it('should assign task with id of selected user assigned', () => {
|
||||
let successSpy = spyOn(component.success, 'emit');
|
||||
component.appId = 42;
|
||||
component.startTaskModel = new StartTaskModel(startTaskMock);
|
||||
component.formKey = '1204';
|
||||
component.getAssigneeId(testUser.id);
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
createTaskButton.click();
|
||||
expect(successSpy).toHaveBeenCalledWith({
|
||||
id: 91,
|
||||
name: 'fakeName',
|
||||
formKey: 1204,
|
||||
assignee: testUser
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should not attach a form when a form id is not selected', () => {
|
||||
let attachFormToATask = spyOn(service, 'attachFormToATask').and.returnValue(of());
|
||||
let attachFormToATask = spyOn(service, 'attachFormToATask').and.returnValue([]);
|
||||
spyOn(service, 'createNewTask').and.callFake(
|
||||
function() {
|
||||
return new Observable((observer) => {
|
||||
@@ -273,14 +294,16 @@ describe('StartTaskComponent', () => {
|
||||
observer.complete();
|
||||
});
|
||||
});
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
component.startTaskModel.name = 'fake-name';
|
||||
fixture.detectChanges();
|
||||
createTaskButton.click();
|
||||
expect(attachFormToATask).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should show start task button', () => {
|
||||
fixture.detectChanges();
|
||||
expect(element.querySelector('#button-start')).toBeDefined();
|
||||
expect(element.querySelector('#button-start')).not.toBeNull();
|
||||
expect(element.querySelector('#button-start').textContent).toContain('ADF_TASK_LIST.START_TASK.FORM.ACTION.START');
|
||||
@@ -294,16 +317,15 @@ describe('StartTaskComponent', () => {
|
||||
});
|
||||
|
||||
it('should disable start button if name is empty', () => {
|
||||
component.startTaskModel.name = '';
|
||||
component.taskForm.controls['name'].setValue('');
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = fixture.nativeElement.querySelector('#button-start');
|
||||
let createTaskButton = fixture.nativeElement.querySelector('#button-start');
|
||||
expect(createTaskButton.disabled).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should cancel start task on cancel button click', () => {
|
||||
let emitSpy = spyOn(component.cancel, 'emit');
|
||||
let cancelTaskButton = fixture.nativeElement.querySelector('#button-cancel');
|
||||
component.startTaskModel.name = '';
|
||||
let cancelTaskButton = <HTMLElement> element.querySelector('#button-cancel');
|
||||
fixture.detectChanges();
|
||||
cancelTaskButton.click();
|
||||
expect(emitSpy).not.toBeNull();
|
||||
@@ -311,24 +333,24 @@ describe('StartTaskComponent', () => {
|
||||
});
|
||||
|
||||
it('should enable start button if name is filled out', () => {
|
||||
component.startTaskModel.name = 'fakeName';
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
fixture.detectChanges();
|
||||
let createTaskButton = fixture.nativeElement.querySelector('#button-start');
|
||||
expect(createTaskButton.disabled).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should define the select option for Forms', () => {
|
||||
component.forms = fakeForms;
|
||||
it('should define the select options for Forms', () => {
|
||||
component.forms$ = service.getFormList();
|
||||
fixture.detectChanges();
|
||||
let selectElement = fixture.nativeElement.querySelector('#form_id');
|
||||
expect(selectElement.attributes['aria-label'].value).toContain('ADF_TASK_LIST.START_TASK.FORM.LABEL.FORM');
|
||||
let selectElement = fixture.nativeElement.querySelector('#form_label');
|
||||
expect(selectElement.innerHTML).toContain('ADF_TASK_LIST.START_TASK.FORM.LABEL.FORM');
|
||||
});
|
||||
|
||||
it('should get formatted full name', () => {
|
||||
let testUser1 = {'id': 1001, 'firstName': 'Wilbur', 'lastName': 'Adams', 'email': 'wilbur@app.activiti.com'};
|
||||
let testUser2 = {'id': 1002, 'firstName': '', 'lastName': 'Adams', 'email': 'adams@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'};
|
||||
it('should get formatted fullname', () => {
|
||||
let testUser1 = { 'id': 1001, 'firstName': 'Wilbur', 'lastName': 'Adams', 'email': 'wilbur@app.activiti.com' };
|
||||
let testUser2 = { 'id': 1002, 'firstName': '', 'lastName': 'Adams', 'email': 'adams@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 testFullName1 = component.getDisplayUser(testUser1.firstName, testUser1.lastName, ' ');
|
||||
let testFullName2 = component.getDisplayUser(testUser2.firstName, testUser2.lastName, ' ');
|
||||
@@ -342,12 +364,44 @@ describe('StartTaskComponent', () => {
|
||||
});
|
||||
|
||||
it('should emit error when there is an error while creating task', () => {
|
||||
component.taskForm.controls['name'].setValue('fakeName');
|
||||
let errorSpy = spyOn(component.error, 'emit');
|
||||
spyOn(service, 'createNewTask').and.returnValue(throwError({}));
|
||||
let createTaskButton = <HTMLElement> element.querySelector('#button-start');
|
||||
component.startTaskModel.name = 'fake-name';
|
||||
fixture.detectChanges();
|
||||
createTaskButton.click();
|
||||
expect(errorSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should emit error when task name exceeds maximum length', () => {
|
||||
component.maxTaskNameLength = 2;
|
||||
component.ngOnInit();
|
||||
fixture.detectChanges();
|
||||
let name = component.taskForm.controls['name'];
|
||||
name.setValue('task');
|
||||
fixture.detectChanges();
|
||||
expect(name.valid).toBeFalsy();
|
||||
name.setValue('ta');
|
||||
fixture.detectChanges();
|
||||
expect(name.valid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should emit error when task name field is empty', () => {
|
||||
fixture.detectChanges();
|
||||
let name = component.taskForm.controls['name'];
|
||||
name.setValue('');
|
||||
fixture.detectChanges();
|
||||
expect(name.valid).toBeFalsy();
|
||||
name.setValue('task');
|
||||
fixture.detectChanges();
|
||||
expect(name.valid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should call logService when task name exceeds maximum length', () => {
|
||||
logSpy = spyOn(logService, 'log').and.callThrough();
|
||||
component.maxTaskNameLength = 300;
|
||||
component.ngOnInit();
|
||||
fixture.detectChanges();
|
||||
expect(logSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user