From 5ce59c99549fc86379405e420140b4b4eede18d7 Mon Sep 17 00:00:00 2001 From: Vito Albano Date: Thu, 17 Nov 2016 14:30:54 +0000 Subject: [PATCH] #1080 - added test for dropdown editor of dynamic table --- .../editors/dropdown/dropdown.editor.html | 3 +- .../editors/dropdown/dropdown.editor.spec.ts | 159 +++++++++++++++++- .../editors/dropdown/dropdown.editor.ts | 6 +- 3 files changed, 161 insertions(+), 7 deletions(-) diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.html b/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.html index 026f9cd555..93afc1d666 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.html +++ b/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.html @@ -3,12 +3,13 @@
diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.spec.ts index 1ab3605eec..4db043d2c3 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.spec.ts @@ -17,9 +17,17 @@ import { Observable } from 'rxjs/Rx'; import { DropdownEditorComponent } from './dropdown.editor'; -import { DynamicTableModel, DynamicTableRow, DynamicTableColumn, DynamicTableColumnOption } from './../../dynamic-table.widget.model'; +import { + DynamicTableModel, + DynamicTableRow, + DynamicTableColumn, + DynamicTableColumnOption +} from './../../dynamic-table.widget.model'; import { FormFieldModel, FormModel } from './../../../core/index'; import { FormService } from './../../../../../services/form.service'; +import { CoreModule } from 'ng2-alfresco-core'; +import { ComponentFixture, TestBed, async } from '@angular/core/testing'; +import { EcmModelService } from '../../../../../services/ecm-model.service'; describe('DropdownEditorComponent', () => { @@ -42,8 +50,8 @@ describe('DropdownEditorComponent', () => { ] }; - table = new DynamicTableModel(null); form = new FormModel({ taskId: '' }); + table = new DynamicTableModel(form, null); table.field = new FormFieldModel(form, { id: '' }); table.rows.push(row); table.columns.push(column); @@ -126,7 +134,7 @@ describe('DropdownEditorComponent', () => { expect(component.value).toBe(row.value[column.id]); }); - it('should handle REST error', () => { + it('should handle REST error gettig options with task id', () => { column.optionType = 'rest'; const error = 'error'; @@ -139,10 +147,155 @@ describe('DropdownEditorComponent', () => { expect(component.handleError).toHaveBeenCalledWith(error); }); + it('should handle REST error getting option with processDefinitionId', () => { + column.optionType = 'rest'; + let procForm = new FormModel ({processDefinitionId: ''}); + let procTable = new DynamicTableModel(procForm, null); + procTable.field = new FormFieldModel(form, { id: '' }); + component.table = procTable; + const error = 'error'; + + spyOn(formService, 'getRestFieldValuesColumnByProcessId').and.returnValue( + Observable.throw(error) + ); + spyOn(component, 'handleError').and.stub(); + + component.ngOnInit(); + expect(component.handleError).toHaveBeenCalledWith(error); + }); + it('should update row on value change', () => { let event = { target: { value: 'two' } }; component.onValueChanged(row, column, event); expect(row.value[column.id]).toBe(column.options[1]); }); + describe('when template is ready', () => { + let dropDownEditorComponent: DropdownEditorComponent; + let fixture: ComponentFixture; + let element: HTMLElement; + let componentHandler; + let stubFormService; + let fakeOptionList: DynamicTableColumnOption[] = [{ + id: 'opt_1', + name: 'option_1' + }, { + id: 'opt_2', + name: 'option_2' + }, { id: 'opt_3', name: 'option_3' }]; + let dynamicTable: DynamicTableModel; + + beforeEach(async(() => { + componentHandler = jasmine.createSpyObj('componentHandler', ['upgradeAllRegistered', 'upgradeElement']); + window['componentHandler'] = componentHandler; + TestBed.configureTestingModule({ + imports: [CoreModule], + declarations: [DropdownEditorComponent], + providers: [FormService, EcmModelService] + }).compileComponents().then(() => { + fixture = TestBed.createComponent(DropdownEditorComponent); + dropDownEditorComponent = fixture.componentInstance; + element = fixture.nativeElement; + }); + })); + + afterEach(() => { + fixture.destroy(); + TestBed.resetTestingModule(); + }); + + describe('and dropdown is populated via taskId', () => { + + beforeEach(async(() => { + stubFormService = fixture.debugElement.injector.get(FormService); + spyOn(stubFormService, 'getRestFieldValuesColumn').and.returnValue(Observable.of(fakeOptionList)); + row = { value: { dropdown: 'one' } }; + column = { + id: 'column-id', + optionType: 'rest', + options: [ + { id: '1', name: 'one' }, + { id: '2', name: 'two' } + ] + }; + form = new FormModel({ taskId: '' }); + dynamicTable = new DynamicTableModel(form, null); + dynamicTable.field = new FormFieldModel(form, { id: '' }); + dynamicTable.rows.push(row); + dynamicTable.columns.push(column); + dropDownEditorComponent.table = dynamicTable; + dropDownEditorComponent.column = column; + dropDownEditorComponent.row = row; + dropDownEditorComponent.table.field = new FormFieldModel(form, { + id: 'dropdown-id', + name: 'date-name', + type: 'dropdown', + readOnly: 'false', + restUrl: 'fake-rest-url' + }); + dropDownEditorComponent.table.field.isVisible = true; + fixture.detectChanges(); + })); + + it('should show visible dropdown widget', async(() => { + expect(element.querySelector('#column-id')).toBeDefined(); + expect(element.querySelector('#column-id')).not.toBeNull(); + expect(element.querySelector('#opt_1')).not.toBeNull(); + expect(element.querySelector('#opt_2')).not.toBeNull(); + expect(element.querySelector('#opt_3')).not.toBeNull(); + })); + }); + + describe('and dropdown is populated via processDefinitionId', () => { + + beforeEach(async(() => { + stubFormService = fixture.debugElement.injector.get(FormService); + spyOn(stubFormService, 'getRestFieldValuesColumnByProcessId').and.returnValue(Observable.of(fakeOptionList)); + row = { value: { dropdown: 'one' } }; + column = { + id: 'column-id', + optionType: 'rest', + options: [ + { id: '1', name: 'one' }, + { id: '2', name: 'two' } + ] + }; + form = new FormModel({ processDefinitionId: '' }); + dynamicTable = new DynamicTableModel(form, null); + dynamicTable.field = new FormFieldModel(form, { id: '' }); + dynamicTable.rows.push(row); + dynamicTable.columns.push(column); + dropDownEditorComponent.table = dynamicTable; + dropDownEditorComponent.column = column; + dropDownEditorComponent.row = row; + dropDownEditorComponent.table.field = new FormFieldModel(form, { + id: 'dropdown-id', + name: 'date-name', + type: 'dropdown', + readOnly: 'false', + restUrl: 'fake-rest-url' + }); + dropDownEditorComponent.table.field.isVisible = true; + fixture.detectChanges(); + })); + + it('should show visible dropdown widget', async(() => { + expect(element.querySelector('#column-id')).toBeDefined(); + expect(element.querySelector('#column-id')).not.toBeNull(); + expect(element.querySelector('#opt_1')).not.toBeNull(); + expect(element.querySelector('#opt_2')).not.toBeNull(); + expect(element.querySelector('#opt_3')).not.toBeNull(); + })); + + it('should show visible dropdown widget', async(() => { + expect(element.querySelector('#column-id')).toBeDefined(); + expect(element.querySelector('#column-id')).not.toBeNull(); + expect(element.querySelector('#opt_1')).not.toBeNull(); + expect(element.querySelector('#opt_2')).not.toBeNull(); + expect(element.querySelector('#opt_3')).not.toBeNull(); + })); + }); + + }); + }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.ts b/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.ts index 0014cdb16b..4bdd1a118c 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/dynamic-table/editors/dropdown/dropdown.editor.ts @@ -39,10 +39,10 @@ export class DropdownEditorComponent extends CellEditorComponent implements OnIn let field = this.table.field; if (field) { if (this.column.optionType === 'rest') { - if (this.table.form && this.table.form.processDefinitionId) { - this.getValuesByProcessDefinitionId(field); - } else { + if (this.table.form && this.table.form.taskId) { this.getValuesByTaskId(field); + } else { + this.getValuesByProcessDefinitionId(field); } } else { this.options = this.column.options || [];