Improved test coverage for dropdown

This commit is contained in:
Vito Albano
2016-11-14 18:33:51 +00:00
committed by Mario Romano
parent 8328863dab
commit c8e5c07d76

View File

@@ -111,7 +111,10 @@ describe('DropdownWidget', () => {
let element: HTMLElement; let element: HTMLElement;
let componentHandler; let componentHandler;
let stubFormService; let stubFormService;
let fakeOptionList: FormFieldOption[] = [{ id: 'opt_1', name: 'option_1' }, { let fakeOptionList: FormFieldOption[] = [{
id: 'opt_1',
name: 'option_1'
}, {
id: 'opt_2', id: 'opt_2',
name: 'option_2' name: 'option_2'
}, { id: 'opt_3', name: 'option_3' }]; }, { id: 'opt_3', name: 'option_3' }];
@@ -130,6 +133,8 @@ describe('DropdownWidget', () => {
}); });
})); }));
describe('and dropdown is populated via taskId', () => {
beforeEach(async(() => { beforeEach(async(() => {
stubFormService = fixture.debugElement.injector.get(FormService); stubFormService = fixture.debugElement.injector.get(FormService);
visibilityService = fixture.debugElement.injector.get(WidgetVisibilityService); visibilityService = fixture.debugElement.injector.get(WidgetVisibilityService);
@@ -142,15 +147,11 @@ describe('DropdownWidget', () => {
readOnly: 'false', readOnly: 'false',
restUrl: 'fake-rest-url' restUrl: 'fake-rest-url'
}); });
dropDownWidget.field.emptyOption = { id: 'empty', name: 'Choose one...' };
dropDownWidget.field.isVisible = true; dropDownWidget.field.isVisible = true;
fixture.detectChanges(); fixture.detectChanges();
})); }));
afterEach(() => {
fixture.destroy();
TestBed.resetTestingModule();
});
it('should show visible dropdown widget', async(() => { it('should show visible dropdown widget', async(() => {
expect(element.querySelector('#dropdown-id')).toBeDefined(); expect(element.querySelector('#dropdown-id')).toBeDefined();
expect(element.querySelector('#dropdown-id')).not.toBeNull(); expect(element.querySelector('#dropdown-id')).not.toBeNull();
@@ -159,7 +160,7 @@ describe('DropdownWidget', () => {
expect(element.querySelector('#opt_3')).not.toBeNull(); expect(element.querySelector('#opt_3')).not.toBeNull();
})); }));
it('should select the default value', async(() => { it('should select the default value when an option is chosen as default', async(() => {
dropDownWidget.field.value = 'option_2'; dropDownWidget.field.value = 'option_2';
fixture.detectChanges(); fixture.detectChanges();
fixture.whenStable() fixture.whenStable()
@@ -168,6 +169,19 @@ describe('DropdownWidget', () => {
expect(dropDownElement).not.toBeNull(); expect(dropDownElement).not.toBeNull();
expect(element.querySelector('#opt_2')).not.toBeNull(); expect(element.querySelector('#opt_2')).not.toBeNull();
expect(dropDownElement.value).toBe('option_2'); expect(dropDownElement.value).toBe('option_2');
expect(dropDownElement.selectedOptions[0].textContent).toBe('option_2');
});
}));
it('should select the empty value when no default is chosen', async(() => {
dropDownWidget.field.value = 'empty';
fixture.detectChanges();
fixture.whenStable()
.then(() => {
let dropDownElement: HTMLSelectElement = <HTMLSelectElement> element.querySelector('#dropdown-id');
expect(dropDownElement).not.toBeNull();
expect(dropDownElement.value).toBe('empty');
expect(dropDownElement.selectedOptions[0].textContent).toBe('Choose one...');
}); });
})); }));
@@ -181,5 +195,70 @@ describe('DropdownWidget', () => {
}); });
})); }));
it('should became visibile when isVisible is true', async(() => {
dropDownWidget.field.isVisible = false;
fixture.detectChanges();
expect(element.querySelector('#dropdown-id')).toBeNull();
dropDownWidget.field.isVisible = true;
fixture.detectChanges();
fixture.whenStable()
.then(() => {
expect(element.querySelector('#dropdown-id')).not.toBeNull();
});
}));
});
describe('and dropdown is populated via processDefinitionId', () => {
beforeEach(async(() => {
stubFormService = fixture.debugElement.injector.get(FormService);
visibilityService = fixture.debugElement.injector.get(WidgetVisibilityService);
spyOn(visibilityService, 'refreshVisibility').and.stub();
spyOn(stubFormService, 'getRestFieldValuesByProcessId').and.returnValue(Observable.of(fakeOptionList));
dropDownWidget.field = new FormFieldModel(new FormModel({ processDefinitionId: 'fake-process-id' }), {
id: 'dropdown-id',
name: 'date-name',
type: 'dropdown',
readOnly: 'false',
restUrl: 'fake-rest-url'
});
dropDownWidget.field.emptyOption = { id: 'empty', name: 'Choose one...' };
dropDownWidget.field.isVisible = true;
fixture.detectChanges();
}));
it('should show visible dropdown widget', async(() => {
expect(element.querySelector('#dropdown-id')).toBeDefined();
expect(element.querySelector('#dropdown-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 select the default value when an option is chosen as default', async(() => {
dropDownWidget.field.value = 'option_2';
fixture.detectChanges();
fixture.whenStable()
.then(() => {
let dropDownElement: HTMLSelectElement = <HTMLSelectElement> element.querySelector('#dropdown-id');
expect(dropDownElement).not.toBeNull();
expect(element.querySelector('#opt_2')).not.toBeNull();
expect(dropDownElement.value).toBe('option_2');
expect(dropDownElement.selectedOptions[0].textContent).toBe('option_2');
});
}));
it('should select the empty value when no default is chosen', async(() => {
dropDownWidget.field.value = 'empty';
fixture.detectChanges();
fixture.whenStable()
.then(() => {
let dropDownElement: HTMLSelectElement = <HTMLSelectElement> element.querySelector('#dropdown-id');
expect(dropDownElement).not.toBeNull();
expect(dropDownElement.value).toBe('empty');
expect(dropDownElement.selectedOptions[0].textContent).toBe('Choose one...');
});
}));
});
}); });
}); });