Added more tests and fixes

This commit is contained in:
Giuseppe Digilio
2018-06-29 18:58:53 +02:00
parent f6b665cb90
commit 8f40ea0ea5
16 changed files with 1035 additions and 224 deletions

View File

@@ -1,13 +1,17 @@
// Load the implementations that should be tested
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
import 'rxjs/add/observable/of';
import { FormControl, FormGroup } from '@angular/forms';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { SortablejsModule } from 'angular-sortablejs';
import { DynamicFormValidationService } from '@ng-dynamic-forms/core';
import { DsDatePickerComponent } from './date-picker.component';
import { FormControl, FormGroup } from '@angular/forms';
import { DynamicDsDatePickerModel } from './date-picker.model';
import { FormBuilderService } from '../../../form-builder.service';
import { FormComponent } from '../../../../form.component';
import { FormService } from '../../../../form.service';
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
TestBed.overrideComponent(type, {
@@ -19,10 +23,28 @@ function createTestComponent<T>(html: string, type: { new(...args: any[]): T }):
return fixture as ComponentFixture<T>;
}
describe('Date Picker component', () => {
export const DATE_TEST_GROUP = new FormGroup({
date: new FormControl()
});
export const DATE_TEST_MODEL_CONFIG = {
disabled: false,
errorMessages: {required: 'You must enter at least the year.'},
id: 'date',
label: 'Date',
name: 'date',
placeholder: 'Date',
readOnly: false,
required: true,
toggleIcon: 'fa fa-calendar'
};
describe('DsDatePickerComponent test suite', () => {
let testComp: TestComponent;
let dateComp: DsDatePickerComponent;
let testFixture: ComponentFixture<TestComponent>;
let dateFixture: ComponentFixture<DsDatePickerComponent>;
let html;
// async beforeEach
@@ -39,15 +61,20 @@ describe('Date Picker component', () => {
providers: [
ChangeDetectorRef,
DsDatePickerComponent,
DynamicFormValidationService,
FormBuilderService,
FormComponent,
FormService
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
});
}));
// synchronous beforeEach
beforeEach(() => {
html = `
describe('', () => {
// synchronous beforeEach
beforeEach(() => {
html = `
<ds-date-picker
[bindId]='bindId'
[group]='group'
@@ -57,14 +84,167 @@ describe('Date Picker component', () => {
(change)='onValueChange($event)'
(focus)='onFocus($event)'></ds-date-picker>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
it('should create DsDatePickerComponent', inject([DsDatePickerComponent], (app: DsDatePickerComponent) => {
expect(app).toBeDefined();
}));
});
it('should create DsDatePickerComponent', inject([DsDatePickerComponent], (app: DsDatePickerComponent) => {
describe('', () => {
describe('when init model value is empty', () => {
beforeEach(() => {
expect(app).toBeDefined();
}));
dateFixture = TestBed.createComponent(DsDatePickerComponent);
dateComp = dateFixture.componentInstance; // FormComponent test instance
dateComp.group = DATE_TEST_GROUP;
dateComp.model = new DynamicDsDatePickerModel(DATE_TEST_MODEL_CONFIG);
dateFixture.detectChanges();
});
it('should init component properly', () => {
expect(dateComp.initialYear).toBeDefined();
expect(dateComp.initialMonth).toBeDefined();
expect(dateComp.initialDay).toBeDefined();
expect(dateComp.maxYear).toBeDefined();
expect(dateComp.disabledMonth).toBeTruthy();
expect(dateComp.disabledDay).toBeTruthy();
});
it('should set year and enable month field when year field is entered', () => {
const event = {
field: 'year',
value: '1983'
};
dateComp.onChange(event);
expect(dateComp.year).toEqual('1983');
expect(dateComp.disabledMonth).toBeFalsy();
expect(dateComp.disabledDay).toBeTruthy();
});
it('should set month and enable day field when month field is entered', () => {
const event = {
field: 'month',
value: '11'
};
dateComp.year = '1983';
dateComp.disabledMonth = false;
dateFixture.detectChanges();
dateComp.onChange(event);
expect(dateComp.year).toEqual('1983');
expect(dateComp.month).toEqual('11');
expect(dateComp.disabledMonth).toBeFalsy();
expect(dateComp.disabledDay).toBeFalsy();
});
it('should set day when day field is entered', () => {
const event = {
field: 'day',
value: '18'
};
dateComp.year = '1983';
dateComp.month = '11';
dateComp.disabledMonth = false;
dateComp.disabledDay = false;
dateFixture.detectChanges();
dateComp.onChange(event);
expect(dateComp.year).toEqual('1983');
expect(dateComp.month).toEqual('11');
expect(dateComp.day).toEqual('18');
expect(dateComp.disabledMonth).toBeFalsy();
expect(dateComp.disabledDay).toBeFalsy();
});
it('should emit blur Event onBlur', () => {
spyOn(dateComp.blur, 'emit');
dateComp.onBlur(new Event('blur'));
expect(dateComp.blur.emit).toHaveBeenCalled();
});
it('should emit focus Event onFocus', () => {
spyOn(dateComp.focus, 'emit');
dateComp.onFocus(new Event('focus'));
expect(dateComp.focus.emit).toHaveBeenCalled();
});
});
describe('when init model value is not empty', () => {
beforeEach(() => {
dateFixture = TestBed.createComponent(DsDatePickerComponent);
dateComp = dateFixture.componentInstance; // FormComponent test instance
dateComp.group = DATE_TEST_GROUP;
dateComp.model = new DynamicDsDatePickerModel(DATE_TEST_MODEL_CONFIG);
dateComp.model.value = '1983-11-18';
dateFixture.detectChanges();
});
it('should init component properly', () => {
expect(dateComp.initialYear).toBeDefined();
expect(dateComp.initialMonth).toBeDefined();
expect(dateComp.initialDay).toBeDefined();
expect(dateComp.maxYear).toBeDefined();
expect(dateComp.year).toBe(1983);
expect(dateComp.month).toBe(11);
expect(dateComp.day).toBe(18);
expect(dateComp.disabledMonth).toBeFalsy();
expect(dateComp.disabledDay).toBeFalsy();
});
it('should disable month and day fields when year field is canceled', () => {
const event = {
field: 'year',
value: null
};
dateComp.onChange(event);
expect(dateComp.year).not.toBeDefined();
expect(dateComp.month).not.toBeDefined();
expect(dateComp.day).not.toBeDefined();
expect(dateComp.disabledMonth).toBeTruthy();
expect(dateComp.disabledDay).toBeTruthy();
});
it('should disable day field when month field is canceled', () => {
const event = {
field: 'month',
value: null
};
dateComp.onChange(event);
expect(dateComp.year).toBe(1983);
expect(dateComp.month).not.toBeDefined();
expect(dateComp.day).not.toBeDefined();
expect(dateComp.disabledMonth).toBeFalsy();
expect(dateComp.disabledDay).toBeTruthy();
});
it('should not disable day field when day field is canceled', () => {
const event = {
field: 'day',
value: null
};
dateComp.onChange(event);
expect(dateComp.year).toBe(1983);
expect(dateComp.month).toBe(11);
expect(dateComp.day).not.toBeDefined();
expect(dateComp.disabledMonth).toBeFalsy();
expect(dateComp.disabledDay).toBeFalsy();
});
});
});
});
@@ -75,23 +255,9 @@ describe('Date Picker component', () => {
})
class TestComponent {
group = new FormGroup({
date: new FormControl(),
});
group = DATE_TEST_GROUP;
inputDateModelConfig = {
disabled: false,
errorMessages: { required: 'You must enter at least the year.' },
id: 'date',
label: 'Date',
name: 'date',
placeholder: 'Date',
readOnly: false,
required: true,
toggleIcon: 'fa fa-calendar'
};
model = new DynamicDsDatePickerModel(this.inputDateModelConfig);
model = new DynamicDsDatePickerModel(DATE_TEST_MODEL_CONFIG);
showErrorMessages = false;

View File

@@ -31,9 +31,9 @@ export class DsDatePickerComponent implements OnInit {
initialMonth: number;
initialDay: number;
year: number;
month: number;
day: number;
year: any;
month: any;
day: any;
minYear: 0;
maxYear: number;

View File

@@ -3,7 +3,10 @@ import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/c
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { DynamicFormValidationService } from '@ng-dynamic-forms/core';
import { Store } from '@ngrx/store';
import { TranslateModule } from '@ngx-translate/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
@@ -16,15 +19,11 @@ import { FormService } from '../../../../form.service';
import { GlobalConfig } from '../../../../../../../config/global-config.interface';
import { GLOBAL_CONFIG } from '../../../../../../../config';
import { FormComponent } from '../../../../form.component';
import { DynamicFormValidationService } from '@ng-dynamic-forms/core';
import { Store } from '@ngrx/store';
import { AppState } from '../../../../../../app.reducer';
import { Observable } from 'rxjs/Observable';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { Chips } from '../../../../../chips/models/chips.model';
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
import { DsDynamicInputModel } from '../ds-dynamic-input.model';
import { By } from '@angular/platform-browser';
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
TestBed.overrideComponent(type, {
@@ -202,7 +201,6 @@ describe('DsDynamicGroupComponent test suite', () => {
}];
groupFixture.detectChanges();
const de = groupFixture.debugElement.queryAll(By.css('button'));
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
const btnEl = buttons[0];
btnEl.click();
@@ -219,7 +217,6 @@ describe('DsDynamicGroupComponent test suite', () => {
groupFixture.detectChanges();
const de = groupFixture.debugElement.queryAll(By.css('button'));
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
const btnEl = buttons[2];
btnEl.click();
@@ -231,7 +228,7 @@ describe('DsDynamicGroupComponent test suite', () => {
});
describe('when init model value is not empty', () => {
beforeEach(inject([FormBuilderService], (service: FormBuilderService) => {
beforeEach(() => {
groupFixture = TestBed.createComponent(DsDynamicGroupComponent);
groupComp = groupFixture.componentInstance; // FormComponent test instance
@@ -246,7 +243,7 @@ describe('DsDynamicGroupComponent test suite', () => {
groupComp.showErrorMessages = false;
groupFixture.detectChanges();
}));
});
afterEach(() => {
groupFixture.destroy();
@@ -279,7 +276,6 @@ describe('DsDynamicGroupComponent test suite', () => {
}];
groupFixture.detectChanges();
const de = groupFixture.debugElement.queryAll(By.css('button'));
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
const btnEl = buttons[0];
btnEl.click();
@@ -290,18 +286,17 @@ describe('DsDynamicGroupComponent test suite', () => {
expect(groupComp.formCollapsed).toEqual(Observable.of(true));
}));
it('should delete existing chips item', inject([FormBuilderService], (service: FormBuilderService) => {
it('should delete existing chips item', () => {
groupComp.onChipSelected(0);
groupFixture.detectChanges();
const de = groupFixture.debugElement.queryAll(By.css('button'));
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
const btnEl = buttons[1];
btnEl.click();
expect(groupComp.chips.getChipsItems()).toEqual([]);
expect(groupComp.formCollapsed).toEqual(Observable.of(false));
}));
});
});
});

View File

@@ -69,7 +69,7 @@
[attr.autoComplete]="model.autoComplete"
[class.is-invalid]="showErrorMessages"
[dynamicId]="bindId && model.id"
[name]="name2"
[name]="model.name + '_2'"
[type]="model.inputType"
[(ngModel)]="secondInputValue"
[disabled]="firstInputValue.length === 0 || isInputDisabled()"

View File

@@ -1,12 +1,12 @@
// Load the implementations that should be tested
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
import { async, ComponentFixture, fakeAsync, inject, TestBed, tick, } from '@angular/core/testing';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { AuthorityOptions } from '../../../../../../core/integration/models/authority-options.model';
import { DynamicFormsCoreModule } from '@ng-dynamic-forms/core';
import { DynamicFormsCoreModule, DynamicFormValidationService } from '@ng-dynamic-forms/core';
import { DynamicFormsNGBootstrapUIModule } from '@ng-dynamic-forms/ui-ng-bootstrap';
import { AuthorityService } from '../../../../../../core/integration/authority.service';
import { AuthorityServiceStub } from '../../../../../testing/authority-service-stub';
@@ -14,6 +14,13 @@ import { DsDynamicLookupComponent } from './dynamic-lookup.component';
import { DynamicLookupModel } from './dynamic-lookup.model';
import { InfiniteScrollModule } from 'ngx-infinite-scroll';
import { TranslateModule } from '@ngx-translate/core';
import { FormBuilderService } from '../../../form-builder.service';
import { FormService } from '../../../../form.service';
import { FormComponent } from '../../../../form.component';
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
import { By } from '@angular/platform-browser';
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
import { DynamicLookupNameModel } from './dynamic-lookup-name.model';
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
TestBed.overrideComponent(type, {
@@ -25,15 +32,67 @@ function createTestComponent<T>(html: string, type: { new(...args: any[]): T }):
return fixture as ComponentFixture<T>;
}
export const LOOKUP_TEST_MODEL_CONFIG = {
authorityOptions: {
closed: false,
metadata: 'lookup',
name: 'RPAuthority',
scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23'
} as AuthorityOptions,
disabled: false,
errorMessages: {required: 'Required field.'},
id: 'lookup',
label: 'Author',
maxOptions: 10,
name: 'lookup',
placeholder: 'Author',
readOnly: false,
required: true,
repeatable: true,
separator: ',',
validators: {required: null},
value: undefined
};
export const LOOKUP_NAME_TEST_MODEL_CONFIG = {
authorityOptions: {
closed: false,
metadata: 'lookup-name',
name: 'RPAuthority',
scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23'
} as AuthorityOptions,
disabled: false,
errorMessages: {required: 'Required field.'},
id: 'lookupName',
label: 'Author',
maxOptions: 10,
name: 'lookupName',
placeholder: 'Author',
readOnly: false,
required: true,
repeatable: true,
separator: ',',
validators: {required: null},
value: undefined
};
export const LOOKUP_TEST_GROUP = new FormGroup({
lookup: new FormControl(),
lookupName: new FormControl()
});
describe('Dynamic Lookup component', () => {
let testComp: TestComponent;
let lookupComp: DsDynamicLookupComponent;
let testFixture: ComponentFixture<TestComponent>;
let lookupFixture: ComponentFixture<DsDynamicLookupComponent>;
let html;
const authorityServiceStub = new AuthorityServiceStub();
// async beforeEach
beforeEach(async(() => {
const authorityServiceStub = new AuthorityServiceStub();
TestBed.configureTestingModule({
imports: [
@@ -52,6 +111,10 @@ describe('Dynamic Lookup component', () => {
providers: [
ChangeDetectorRef,
DsDynamicLookupComponent,
DynamicFormValidationService,
FormBuilderService,
FormComponent,
FormService,
{provide: AuthorityService, useValue: authorityServiceStub},
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
@@ -59,9 +122,10 @@ describe('Dynamic Lookup component', () => {
}));
// synchronous beforeEach
beforeEach(() => {
html = `
describe('', () => {
// synchronous beforeEach
beforeEach(() => {
html = `
<ds-dynamic-lookup
[bindId]="bindId"
[group]="group"
@@ -71,15 +135,203 @@ describe('Dynamic Lookup component', () => {
(change)="onValueChange($event)"
(focus)="onFocus($event)"></ds-dynamic-lookup>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
it('should create DsDynamicLookupComponent', inject([DsDynamicLookupComponent], (app: DsDynamicLookupComponent) => {
expect(app).toBeDefined();
}));
});
it('should create DsDynamicLookupComponent', inject([DsDynamicLookupComponent], (app: DsDynamicLookupComponent) => {
describe('when model is DynamicLookupModel', () => {
expect(app).toBeDefined();
}));
describe('', () => {
beforeEach(() => {
lookupFixture = TestBed.createComponent(DsDynamicLookupComponent);
lookupComp = lookupFixture.componentInstance; // FormComponent test instance
lookupComp.group = LOOKUP_TEST_GROUP;
lookupComp.model = new DynamicLookupModel(LOOKUP_TEST_MODEL_CONFIG);
lookupFixture.detectChanges();
});
it('should render only an input element', () => {
const de = lookupFixture.debugElement.queryAll(By.css('input.form-control'));
expect(de.length).toBe(1);
});
});
describe('and init model value is empty', () => {
beforeEach(() => {
lookupFixture = TestBed.createComponent(DsDynamicLookupComponent);
lookupComp = lookupFixture.componentInstance; // FormComponent test instance
lookupComp.group = LOOKUP_TEST_GROUP;
lookupComp.model = new DynamicLookupModel(LOOKUP_TEST_MODEL_CONFIG);
lookupFixture.detectChanges();
});
it('should init component properly', () => {
expect(lookupComp.firstInputValue).toBe('');
});
it('should return search results', fakeAsync(() => {
const de = lookupFixture.debugElement.queryAll(By.css('button'));
const btnEl = de[0].nativeElement;
const results$ = authorityServiceStub.getEntriesByName({} as any);
lookupComp.firstInputValue = 'test';
lookupFixture.detectChanges();
btnEl.click();
tick();
lookupFixture.detectChanges();
results$.subscribe((results) => {
expect(lookupComp.optionsList).toEqual(results.payload);
})
}));
it('should select a results entry properly', fakeAsync(() => {
let de = lookupFixture.debugElement.queryAll(By.css('button'));
const btnEl = de[0].nativeElement;
const selectedValue = Object.assign(new AuthorityValueModel(), {id: 1, display: 'one', value: 1});
spyOn(lookupComp.change, 'emit');
lookupComp.firstInputValue = 'test';
lookupFixture.detectChanges();
btnEl.click();
tick();
lookupFixture.detectChanges();
de = lookupFixture.debugElement.queryAll(By.css('button.dropdown-item'));
const entryEl = de[0].nativeElement;
entryEl.click();
expect(lookupComp.firstInputValue).toEqual('one');
expect(lookupComp.model.value).toEqual(selectedValue);
expect(lookupComp.change.emit).toHaveBeenCalled();
}));
it('should set model.value on input type when AuthorityOptions.closed is false', fakeAsync(() => {
lookupComp.firstInputValue = 'test';
lookupFixture.detectChanges();
lookupComp.onInput(new Event('input'));
expect(lookupComp.model.value).toEqual(new FormFieldMetadataValueObject('test'))
}));
it('should not set model.value on input type when AuthorityOptions.closed is true', () => {
lookupComp.model.authorityOptions.closed = true;
lookupComp.firstInputValue = 'test';
lookupFixture.detectChanges();
lookupComp.onInput(new Event('input'));
expect(lookupComp.model.value).not.toBeDefined();
});
});
describe('and init model value is not empty', () => {
beforeEach(() => {
lookupFixture = TestBed.createComponent(DsDynamicLookupComponent);
lookupComp = lookupFixture.componentInstance; // FormComponent test instance
lookupComp.group = LOOKUP_TEST_GROUP;
lookupComp.model = new DynamicLookupModel(LOOKUP_TEST_MODEL_CONFIG);
lookupComp.model.value = new FormFieldMetadataValueObject('test', null, 'test001');
lookupFixture.detectChanges();
// spyOn(store, 'dispatch');
});
it('should init component properly', () => {
expect(lookupComp.firstInputValue).toBe('test')
});
});
});
describe('when model is DynamicLookupNameModel', () => {
describe('', () => {
beforeEach(() => {
lookupFixture = TestBed.createComponent(DsDynamicLookupComponent);
lookupComp = lookupFixture.componentInstance; // FormComponent test instance
lookupComp.group = LOOKUP_TEST_GROUP;
lookupComp.model = new DynamicLookupNameModel(LOOKUP_NAME_TEST_MODEL_CONFIG);
lookupFixture.detectChanges();
// spyOn(store, 'dispatch');
});
it('should render two input element', () => {
const de = lookupFixture.debugElement.queryAll(By.css('input.form-control'));
expect(de.length).toBe(2);
});
});
describe('and init model value is empty', () => {
beforeEach(() => {
lookupFixture = TestBed.createComponent(DsDynamicLookupComponent);
lookupComp = lookupFixture.componentInstance; // FormComponent test instance
lookupComp.group = LOOKUP_TEST_GROUP;
lookupComp.model = new DynamicLookupNameModel(LOOKUP_NAME_TEST_MODEL_CONFIG);
lookupFixture.detectChanges();
});
it('should select a results entry properly', fakeAsync(() => {
const payload = [
Object.assign(new AuthorityValueModel(), {id: 1, display: 'Name, Lastname', value: 1}),
Object.assign(new AuthorityValueModel(), {id: 2, display: 'NameTwo, LastnameTwo', value: 2}),
];
let de = lookupFixture.debugElement.queryAll(By.css('button'));
const btnEl = de[0].nativeElement;
const selectedValue = Object.assign(new AuthorityValueModel(), {id: 1, display: 'Name, Lastname', value: 1});
spyOn(lookupComp.change, 'emit');
authorityServiceStub.setNewPayload(payload);
lookupComp.firstInputValue = 'test';
lookupFixture.detectChanges();
btnEl.click();
tick();
lookupFixture.detectChanges();
de = lookupFixture.debugElement.queryAll(By.css('button.dropdown-item'));
const entryEl = de[0].nativeElement;
entryEl.click();
expect(lookupComp.firstInputValue).toEqual('Name');
expect(lookupComp.secondInputValue).toEqual('Lastname');
expect(lookupComp.model.value).toEqual(selectedValue);
expect(lookupComp.change.emit).toHaveBeenCalled();
}));
});
describe('and init model value is not empty', () => {
beforeEach(() => {
lookupFixture = TestBed.createComponent(DsDynamicLookupComponent);
lookupComp = lookupFixture.componentInstance; // FormComponent test instance
lookupComp.group = LOOKUP_TEST_GROUP;
lookupComp.model = new DynamicLookupNameModel(LOOKUP_NAME_TEST_MODEL_CONFIG);
lookupComp.model.value = new FormFieldMetadataValueObject('Name, Lastname', null, 'test001');
lookupFixture.detectChanges();
});
it('should init component properly', () => {
expect(lookupComp.firstInputValue).toBe('Name');
expect(lookupComp.secondInputValue).toBe('Lastname');
});
});
});
});
// declare a test component
@@ -89,31 +341,9 @@ describe('Dynamic Lookup component', () => {
})
class TestComponent {
group: FormGroup = new FormGroup({
lookup: new FormControl(),
});
group: FormGroup = LOOKUP_TEST_GROUP;
inputLookupModelConfig = {
authorityOptions: {
closed: false,
metadata: 'lookup',
name: 'RPAuthority',
scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23'
} as AuthorityOptions,
disabled: false,
errorMessages: {required: 'Required field.'},
id: 'lookup',
label: 'Author',
maxOptions: 10,
name: 'lookup',
placeholder: 'Author',
readOnly: false,
required: true,
repeatable: true,
separator: ',',
validators: {required: null},
value: undefined
};
inputLookupModelConfig = LOOKUP_TEST_MODEL_CONFIG;
model = new DynamicLookupModel(this.inputLookupModelConfig);

View File

@@ -32,7 +32,6 @@ export class DsDynamicLookupComponent implements OnDestroy, OnInit {
public loading = false;
public pageInfo: PageInfo;
public optionsList: any;
public name2: string;
protected searchOptions: IntegrationSearchOptions;
protected sub: Subscription;
@@ -50,11 +49,6 @@ export class DsDynamicLookupComponent implements OnDestroy, OnInit {
this.model.maxOptions,
1);
// Switch Lookup/LookupName
if (this.isLookupName()) {
this.name2 = this.model.name + '2';
}
this.setInputsValue(this.model.value);
this.model.valueUpdates

View File

@@ -7,9 +7,9 @@
[readonly]="model.readOnly"
[type]="model.inputType"
[value]="formatItemForInput(model.value)"
(blur)="onBlurEvent($event)"
(blur)="onBlur($event)"
(click)="$event.stopPropagation(); openDropdown(sdRef);"
(focus)="onFocusEvent($event)"
(focus)="onFocus($event)"
(keypress)="$event.preventDefault()">
<button aria-describedby="collectionControlsMenuLabel"
class="ds-form-input-btn btn btn-outline-primary"

View File

@@ -1,7 +1,7 @@
// Load the implementations that should be tested
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
import { async, ComponentFixture, fakeAsync, inject, TestBed, tick, } from '@angular/core/testing';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
@@ -14,6 +14,11 @@ import { InfiniteScrollModule } from 'ngx-infinite-scroll';
import { TranslateModule } from '@ngx-translate/core';
import { DsDynamicScrollableDropdownComponent } from './dynamic-scrollable-dropdown.component';
import { DynamicScrollableDropdownModel } from './dynamic-scrollable-dropdown.model';
import { DsDynamicTypeaheadComponent } from '../typeahead/dynamic-typeahead.component';
import { DynamicTypeaheadModel } from '../typeahead/dynamic-typeahead.model';
import { TYPEAHEAD_TEST_GROUP, TYPEAHEAD_TEST_MODEL_CONFIG } from '../typeahead/dynamic-typeahead.component.spec';
import { By } from '@angular/platform-browser';
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
TestBed.overrideComponent(type, {
@@ -25,15 +30,48 @@ function createTestComponent<T>(html: string, type: { new(...args: any[]): T }):
return fixture as ComponentFixture<T>;
}
function hasClass(element: any, className: string): boolean {
const classes = element.getAttribute('class');
return classes.split(' ').indexOf(className) !== -1;
}
export const SD_TEST_GROUP = new FormGroup({
dropdown: new FormControl(),
});
export const SD_TEST_MODEL_CONFIG = {
authorityOptions: {
closed: false,
metadata: 'dropdown',
name: 'common_iso_languages',
scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23'
} as AuthorityOptions,
disabled: false,
errorMessages: {required: 'Required field.'},
id: 'dropdown',
label: 'Language',
maxOptions: 10,
name: 'dropdown',
placeholder: 'Language',
readOnly: false,
required: false,
repeatable: false,
value: undefined
};
describe('Dynamic Dynamic Scrollable Dropdown component', () => {
let testComp: TestComponent;
let scrollableDropdownComp: DsDynamicScrollableDropdownComponent;
let testFixture: ComponentFixture<TestComponent>;
let scrollableDropdownFixture: ComponentFixture<DsDynamicScrollableDropdownComponent>;
let html;
let modelValue;
const authorityServiceStub = new AuthorityServiceStub();
// async beforeEach
beforeEach(async(() => {
const authorityServiceStub = new AuthorityServiceStub();
TestBed.configureTestingModule({
imports: [
@@ -59,9 +97,10 @@ describe('Dynamic Dynamic Scrollable Dropdown component', () => {
}));
// synchronous beforeEach
beforeEach(() => {
html = `
describe('', () => {
// synchronous beforeEach
beforeEach(() => {
html = `
<ds-dynamic-scrollable-dropdown [bindId]="bindId"
[group]="group"
[model]="model"
@@ -70,15 +109,128 @@ describe('Dynamic Dynamic Scrollable Dropdown component', () => {
(change)="onValueChange($event)"
(focus)="onFocus($event)"></ds-dynamic-scrollable-dropdown>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
it('should create DsDynamicScrollableDropdownComponent', inject([DsDynamicScrollableDropdownComponent], (app: DsDynamicScrollableDropdownComponent) => {
expect(app).toBeDefined();
}));
});
it('should create DsDynamicScrollableDropdownComponent', inject([DsDynamicScrollableDropdownComponent], (app: DsDynamicScrollableDropdownComponent) => {
describe('', () => {
describe('when init model value is empty', () => {
beforeEach(() => {
expect(app).toBeDefined();
}));
scrollableDropdownFixture = TestBed.createComponent(DsDynamicScrollableDropdownComponent);
scrollableDropdownComp = scrollableDropdownFixture.componentInstance; // FormComponent test instance
scrollableDropdownComp.group = SD_TEST_GROUP;
scrollableDropdownComp.model = new DynamicScrollableDropdownModel(SD_TEST_MODEL_CONFIG);
scrollableDropdownFixture.detectChanges();
});
afterEach(() => {
scrollableDropdownFixture.destroy();
scrollableDropdownComp = null;
});
it('should init component properly', () => {
const results$ = authorityServiceStub.getEntriesByName({} as any);
expect(scrollableDropdownComp.optionsList).toBeDefined();
results$.subscribe((results) => {
expect(scrollableDropdownComp.optionsList).toEqual(results.payload);
})
});
it('should display dropdown menu entries', () => {
const de = scrollableDropdownFixture.debugElement.query(By.css('button.ds-form-input-btn'));
const btnEl = de.nativeElement;
const deMenu = scrollableDropdownFixture.debugElement.query(By.css('div.scrollable-dropdown-menu'));
const menuEl = deMenu.nativeElement;
btnEl.click();
scrollableDropdownFixture.detectChanges();
expect(hasClass(menuEl, 'show')).toBeTruthy();
});
it('should fetch the next set of results when the user scroll to the end of the list', fakeAsync(() => {
scrollableDropdownComp.pageInfo.currentPage = 1;
scrollableDropdownComp.pageInfo.totalPages = 2;
scrollableDropdownFixture.detectChanges();
scrollableDropdownComp.onScroll();
tick();
expect(scrollableDropdownComp.optionsList.length).toBe(4);
}));
it('should select a results entry properly', fakeAsync(() => {
const selectedValue = Object.assign(new AuthorityValueModel(), {id: 1, display: 'one', value: 1});
let de: any = scrollableDropdownFixture.debugElement.query(By.css('button.ds-form-input-btn'));
let btnEl = de.nativeElement;
de = scrollableDropdownFixture.debugElement.query(By.css('div.scrollable-dropdown-menu'));
const menuEl = de.nativeElement;
btnEl.click();
scrollableDropdownFixture.detectChanges();
de = scrollableDropdownFixture.debugElement.queryAll(By.css('button.dropdown-item'));
btnEl = de[0].nativeElement;
btnEl.click();
scrollableDropdownFixture.detectChanges();
expect((scrollableDropdownComp.model as any).value).toEqual(selectedValue);
}));
it('should emit blur Event onBlur', () => {
spyOn(scrollableDropdownComp.blur, 'emit');
scrollableDropdownComp.onBlur(new Event('blur'));
expect(scrollableDropdownComp.blur.emit).toHaveBeenCalled();
});
it('should emit focus Event onFocus', () => {
spyOn(scrollableDropdownComp.focus, 'emit');
scrollableDropdownComp.onFocus(new Event('focus'));
expect(scrollableDropdownComp.focus.emit).toHaveBeenCalled();
});
});
describe('when init model value is not empty', () => {
beforeEach(() => {
scrollableDropdownFixture = TestBed.createComponent(DsDynamicScrollableDropdownComponent);
scrollableDropdownComp = scrollableDropdownFixture.componentInstance; // FormComponent test instance
scrollableDropdownComp.group = SD_TEST_GROUP;
modelValue = Object.assign(new AuthorityValueModel(), {id: 1, display: 'one', value: 1});
scrollableDropdownComp.model = new DynamicScrollableDropdownModel(SD_TEST_MODEL_CONFIG);
scrollableDropdownComp.model.value = modelValue;
scrollableDropdownFixture.detectChanges();
});
afterEach(() => {
scrollableDropdownFixture.destroy();
scrollableDropdownComp = null;
});
it('should init component properly', () => {
const results$ = authorityServiceStub.getEntriesByName({} as any);
expect(scrollableDropdownComp.optionsList).toBeDefined();
results$.subscribe((results) => {
expect(scrollableDropdownComp.optionsList).toEqual(results.payload);
expect(scrollableDropdownComp.model.value).toEqual(modelValue);
})
});
});
});
});
// declare a test component
@@ -88,31 +240,9 @@ describe('Dynamic Dynamic Scrollable Dropdown component', () => {
})
class TestComponent {
group: FormGroup = new FormGroup({
lookup: new FormControl(),
});
group: FormGroup = SD_TEST_GROUP;
inputDropdownModelConfig = {
authorityOptions: {
closed: false,
metadata: 'lookup',
name: 'common_iso_languages',
scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23'
} as AuthorityOptions,
disabled: false,
errorMessages: {required: 'Required field.'},
id: 'dropdown',
label: 'Language',
maxOptions: 10,
name: 'dropdown',
placeholder: 'Language',
readOnly: false,
required: false,
repeatable: false,
value: undefined
};
model = new DynamicScrollableDropdownModel(this.inputDropdownModelConfig);
model = new DynamicScrollableDropdownModel(SD_TEST_MODEL_CONFIG);
showErrorMessages = false;

View File

@@ -76,19 +76,17 @@ export class DsDynamicScrollableDropdownComponent implements OnInit {
}
}
onBlurEvent(event: Event) {
onBlur(event: Event) {
this.blur.emit(event);
}
onFocusEvent(event) {
onFocus(event) {
this.focus.emit(event);
}
onSelect(event) {
this.group.markAsDirty();
// (this.model as DynamicScrollableDropdownModel).parent as
// this.group.get(this.model.id).setValue(event);
this.model.valueUpdates.next(event)
this.model.valueUpdates.next(event);
this.change.emit(event);
}
}

View File

@@ -12,7 +12,7 @@
[placeholder]="model.label"
[readonly]="model.readOnly"
[(ngModel)]="currentValue"
(blur)="onBlurEvent($event)"
(blur)="onBlur($event)"
(keypress)="preventEventsPropagation($event)"
(keydown)="preventEventsPropagation($event)"
(keyup)="onKeyUp($event)" />
@@ -33,8 +33,8 @@
[readonly]="model.readOnly"
[resultTemplate]="rt"
[type]="model.inputType"
(blur)="onBlurEvent($event)"
(focus)="onFocusEvent($event)"
(blur)="onBlur($event)"
(focus)="onFocus($event)"
(change)="$event.stopPropagation()"
(input)="onInput($event)"
(selectItem)="onSelectItem($event)"

View File

@@ -1,18 +1,24 @@
// Load the implementations that should be tested
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
import { async, ComponentFixture, fakeAsync, flush, inject, TestBed, } from '@angular/core/testing';
import { AuthorityOptions } from '../../../../../../core/integration/models/authority-options.model';
import { DynamicFormsCoreModule } from '@ng-dynamic-forms/core';
import { DynamicFormsNGBootstrapUIModule } from '@ng-dynamic-forms/ui-ng-bootstrap';
import { NgbModule, NgbTypeaheadSelectItemEvent } from '@ng-bootstrap/ng-bootstrap';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of'
import { AuthorityOptions } from '../../../../../../core/integration/models/authority-options.model';
import { AuthorityService } from '../../../../../../core/integration/authority.service';
import { AuthorityServiceStub } from '../../../../../testing/authority-service-stub';
import { DsDynamicTagComponent } from './dynamic-tag.component';
import { DynamicTagModel } from './dynamic-tag.model';
import { GlobalConfig } from '../../../../../../../config/global-config.interface';
import { GLOBAL_CONFIG } from '../../../../../../../config';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { Chips } from '../../../../../chips/models/chips.model';
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
TestBed.overrideComponent(type, {
@@ -24,11 +30,50 @@ function createTestComponent<T>(html: string, type: { new(...args: any[]): T }):
return fixture as ComponentFixture<T>;
}
describe('Dynamic Dynamic Tag component', () => {
function createKeyUpEvent(key: number) {
/* tslint:disable:no-empty */
const event = {
keyCode: key, preventDefault: () => {
}, stopPropagation: () => {
}
};
/* tslint:enable:no-empty */
spyOn(event, 'preventDefault');
spyOn(event, 'stopPropagation');
return event;
}
export const TAG_TEST_GROUP = new FormGroup({
tag: new FormControl(),
});
export const TAG_TEST_MODEL_CONFIG = {
authorityOptions: {
closed: false,
metadata: 'tag',
name: 'common_iso_languages',
scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23'
} as AuthorityOptions,
disabled: false,
id: 'tag',
label: 'Keywords',
minChars: 3,
name: 'tag',
placeholder: 'Keywords',
readOnly: false,
required: false,
repeatable: false
};
describe('DsDynamicTagComponent test suite', () => {
let testComp: TestComponent;
let tagComp: DsDynamicTagComponent;
let testFixture: ComponentFixture<TestComponent>;
let tagFixture: ComponentFixture<DsDynamicTagComponent>;
let html;
let chips: Chips;
let modelValue: any;
// async beforeEach
beforeEach(async(() => {
@@ -57,9 +102,10 @@ describe('Dynamic Dynamic Tag component', () => {
}));
// synchronous beforeEach
beforeEach(() => {
html = `
describe('', () => {
// synchronous beforeEach
beforeEach(() => {
html = `
<ds-dynamic-tag [bindId]="bindId"
[group]="group"
[model]="model"
@@ -68,15 +114,176 @@ describe('Dynamic Dynamic Tag component', () => {
(change)="onValueChange($event)"
(focus)="onFocus($event)"></ds-dynamic-tag>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
it('should create DsDynamicTagComponent', inject([DsDynamicTagComponent], (app: DsDynamicTagComponent) => {
expect(app).toBeDefined();
}));
});
it('should create DsDynamicTagComponent', inject([DsDynamicTagComponent], (app: DsDynamicTagComponent) => {
describe('when authorityOptions are setted', () => {
describe('and init model value is empty', () => {
beforeEach(() => {
expect(app).toBeDefined();
}));
tagFixture = TestBed.createComponent(DsDynamicTagComponent);
tagComp = tagFixture.componentInstance; // FormComponent test instance
tagComp.group = TAG_TEST_GROUP;
tagComp.model = new DynamicTagModel(TAG_TEST_MODEL_CONFIG);
tagFixture.detectChanges();
});
afterEach(() => {
tagFixture.destroy();
tagComp = null;
});
it('should init component properly', () => {
chips = new Chips([], 'display');
expect(tagComp.chips.getChipsItems()).toEqual(chips.getChipsItems());
expect(tagComp.searchOptions).toBeDefined();
});
it('should search when 3+ characters typed', fakeAsync(() => {
spyOn((tagComp as any).authorityService, 'getEntriesByName').and.callThrough();
tagComp.search(Observable.of('test')).subscribe(() => {
expect((tagComp as any).authorityService.getEntriesByName).toHaveBeenCalled();
});
}));
it('should select a results entry properly', fakeAsync(() => {
modelValue = [
Object.assign(new AuthorityValueModel(), {id: 1, display: 'Name, Lastname', value: 1})
];
const event: NgbTypeaheadSelectItemEvent = {
item: Object.assign(new AuthorityValueModel(), {id: 1, display: 'Name, Lastname', value: 1}),
preventDefault: () => {
return;
}
};
spyOn(tagComp.change, 'emit');
tagComp.onSelectItem(event);
tagFixture.detectChanges();
flush();
expect(tagComp.chips.getChipsItems()).toEqual(modelValue);
expect(tagComp.model.value).toEqual(modelValue);
expect(tagComp.currentValue).toBeNull();
expect(tagComp.change.emit).toHaveBeenCalled();
}));
it('should emit blur Event onBlur', () => {
spyOn(tagComp.blur, 'emit');
tagComp.onBlur(new Event('blur'));
expect(tagComp.blur.emit).toHaveBeenCalled();
});
it('should emit focus Event onFocus', () => {
spyOn(tagComp.focus, 'emit');
tagComp.onFocus(new Event('focus'));
expect(tagComp.focus.emit).toHaveBeenCalled();
});
it('should emit change Event onBlur when currentValue is not empty', fakeAsync(() => {
tagComp.currentValue = 'test value';
tagFixture.detectChanges();
spyOn(tagComp.blur, 'emit');
spyOn(tagComp.change, 'emit');
tagComp.onBlur(new Event('blur'));
tagFixture.detectChanges();
flush();
expect(tagComp.change.emit).toHaveBeenCalled();
expect(tagComp.blur.emit).toHaveBeenCalled();
}));
});
describe('and init model value is not empty', () => {
beforeEach(() => {
tagFixture = TestBed.createComponent(DsDynamicTagComponent);
tagComp = tagFixture.componentInstance; // FormComponent test instance
tagComp.group = TAG_TEST_GROUP;
tagComp.model = new DynamicTagModel(TAG_TEST_MODEL_CONFIG);
modelValue = [
new FormFieldMetadataValueObject('a', null, 'test001'),
new FormFieldMetadataValueObject('b', null, 'test002'),
new FormFieldMetadataValueObject('c', null, 'test003'),
];
tagComp.model.value = modelValue;
tagFixture.detectChanges();
});
afterEach(() => {
tagFixture.destroy();
tagComp = null;
});
it('should init component properly', () => {
chips = new Chips(modelValue, 'display');
expect(tagComp.chips.getChipsItems()).toEqual(chips.getChipsItems());
expect(tagComp.searchOptions).toBeDefined();
});
});
});
describe('when authorityOptions are not setted', () => {
describe('and init model value is empty', () => {
beforeEach(() => {
tagFixture = TestBed.createComponent(DsDynamicTagComponent);
tagComp = tagFixture.componentInstance; // FormComponent test instance
tagComp.group = TAG_TEST_GROUP;
const config = TAG_TEST_MODEL_CONFIG;
config.authorityOptions = null;
tagComp.model = new DynamicTagModel(config);
tagFixture.detectChanges();
});
afterEach(() => {
tagFixture.destroy();
tagComp = null;
});
it('should init component properly', () => {
chips = new Chips([], 'display');
expect(tagComp.chips.getChipsItems()).toEqual(chips.getChipsItems());
expect(tagComp.searchOptions).not.toBeDefined();
});
it('should add an item on ENTER or key press is \',\' or \';\'', fakeAsync(() => {
let event = createKeyUpEvent(13);
tagComp.currentValue = 'test value';
tagFixture.detectChanges();
tagComp.onKeyUp(event);
flush();
expect(tagComp.model.value).toEqual(['test value']);
expect(tagComp.currentValue).toBeNull();
event = createKeyUpEvent(188);
tagComp.currentValue = 'test value';
tagFixture.detectChanges();
tagComp.onKeyUp(event);
flush();
expect(tagComp.model.value).toEqual(['test value']);
expect(tagComp.currentValue).toBeNull();
}));
});
});
});
// declare a test component
@@ -86,29 +293,9 @@ describe('Dynamic Dynamic Tag component', () => {
})
class TestComponent {
group: FormGroup = new FormGroup({
lookup: new FormControl(),
});
group: FormGroup = TAG_TEST_GROUP;
inputTagModelConfig = {
authorityOptions: {
closed: false,
metadata: 'tag',
name: 'common_iso_languages',
scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23'
} as AuthorityOptions,
disabled: false,
id: 'tag',
label: 'Keywords',
minChars: 3,
name: 'tag',
placeholder: 'Keywords',
readOnly: false,
required: false,
repeatable: false
};
model = new DynamicTagModel(this.inputTagModelConfig);
model = new DynamicTagModel(TAG_TEST_MODEL_CONFIG);
showErrorMessages = false;

View File

@@ -8,7 +8,7 @@ import { AuthorityService } from '../../../../../../core/integration/authority.s
import { DynamicTagModel } from './dynamic-tag.model';
import { IntegrationSearchOptions } from '../../../../../../core/integration/models/integration-options.model';
import { Chips } from '../../../../../chips/models/chips.model';
import { hasValue, isEmpty, isNotEmpty } from '../../../../../empty.util';
import { hasValue, isNotEmpty } from '../../../../../empty.util';
import { isEqual } from 'lodash';
import { GlobalConfig } from '../../../../../../../config/global-config.interface';
import { GLOBAL_CONFIG } from '../../../../../../../config';
@@ -107,14 +107,14 @@ export class DsDynamicTagComponent implements OnInit {
this.cdr.detectChanges();
}
onBlurEvent(event: Event) {
onBlur(event: Event) {
if (isNotEmpty(this.currentValue)) {
this.addTagsToChips();
this.addTagsToChips();
}
this.blur.emit(event);
}
onFocusEvent($event) {
onFocus(event) {
this.focus.emit(event);
}
@@ -138,7 +138,7 @@ export class DsDynamicTagComponent implements OnInit {
onKeyUp(event) {
if (event.keyCode === 13 || event.keyCode === 188) {
event.preventDefault();
// Key: Enter or , or ;
// Key: Enter or ',' or ';'
this.addTagsToChips();
event.stopPropagation();
}
@@ -147,7 +147,6 @@ export class DsDynamicTagComponent implements OnInit {
preventEventsPropagation(event) {
event.stopPropagation();
if (event.keyCode === 13) {
// Key: Enter or , or ;
event.preventDefault();
}
}
@@ -180,15 +179,4 @@ export class DsDynamicTagComponent implements OnInit {
this.updateModel(event);
}
}
removeChips(event) {
// console.log("Removed chips index: "+event);
this.model.valueUpdates.next(this.chips.getChipsItems());
this.change.emit(event);
}
changeChips(event) {
this.model.valueUpdates.next(this.chips.getChipsItems());
this.change.emit(event);
}
}

View File

@@ -15,9 +15,9 @@
[resultTemplate]="rt"
[type]="model.inputType"
[(ngModel)]="currentValue"
(blur)="onBlurEvent($event)"
(focus)="onFocusEvent($event)"
(change)="onChangeEvent($event)"
(blur)="onBlur($event)"
(focus)="onFocus($event)"
(change)="onChange($event)"
(input)="onInput($event)"
(selectItem)="onSelectItem($event)">

View File

@@ -1,7 +1,11 @@
// Load the implementations that should be tested
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
import { async, ComponentFixture, fakeAsync, inject, TestBed, } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { AuthorityOptions } from '../../../../../../core/integration/models/authority-options.model';
import { DynamicFormsCoreModule } from '@ng-dynamic-forms/core';
@@ -13,6 +17,7 @@ import { GLOBAL_CONFIG } from '../../../../../../../config';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { DsDynamicTypeaheadComponent } from './dynamic-typeahead.component';
import { DynamicTypeaheadModel } from './dynamic-typeahead.model';
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
TestBed.overrideComponent(type, {
@@ -24,10 +29,35 @@ function createTestComponent<T>(html: string, type: { new(...args: any[]): T }):
return fixture as ComponentFixture<T>;
}
describe('Dynamic Dynamic Typeahead component', () => {
export const TYPEAHEAD_TEST_GROUP = new FormGroup({
typeahead: new FormControl(),
});
export const TYPEAHEAD_TEST_MODEL_CONFIG = {
authorityOptions: {
closed: false,
metadata: 'typeahead',
name: 'EVENTAuthority',
scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23'
} as AuthorityOptions,
disabled: false,
id: 'typeahead',
label: 'Conference',
minChars: 3,
name: 'typeahead',
placeholder: 'Conference',
readOnly: false,
required: false,
repeatable: false,
value: undefined
};
describe('DsDynamicTypeaheadComponent test suite', () => {
let testComp: TestComponent;
let typeaheadComp: DsDynamicTypeaheadComponent;
let testFixture: ComponentFixture<TestComponent>;
let typeaheadFixture: ComponentFixture<DsDynamicTypeaheadComponent>;
let html;
// async beforeEach
@@ -57,9 +87,10 @@ describe('Dynamic Dynamic Typeahead component', () => {
}));
// synchronous beforeEach
beforeEach(() => {
html = `
describe('', () => {
// synchronous beforeEach
beforeEach(() => {
html = `
<ds-dynamic-typeahead [bindId]="bindId"
[group]="group"
[model]="model"
@@ -68,15 +99,122 @@ describe('Dynamic Dynamic Typeahead component', () => {
(change)="onValueChange($event)"
(focus)="onFocus($event)"></ds-dynamic-typeahead>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
it('should create DsDynamicTypeaheadComponent', inject([DsDynamicTypeaheadComponent], (app: DsDynamicTypeaheadComponent) => {
expect(app).toBeDefined();
}));
});
it('should create DsDynamicTypeaheadComponent', inject([DsDynamicTypeaheadComponent], (app: DsDynamicTypeaheadComponent) => {
describe('', () => {
describe('when init model value is empty', () => {
beforeEach(() => {
expect(app).toBeDefined();
}));
typeaheadFixture = TestBed.createComponent(DsDynamicTypeaheadComponent);
typeaheadComp = typeaheadFixture.componentInstance; // FormComponent test instance
typeaheadComp.group = TYPEAHEAD_TEST_GROUP;
typeaheadComp.model = new DynamicTypeaheadModel(TYPEAHEAD_TEST_MODEL_CONFIG);
typeaheadFixture.detectChanges();
});
afterEach(() => {
typeaheadFixture.destroy();
typeaheadComp = null;
});
it('should init component properly', () => {
expect(typeaheadComp.currentValue).not.toBeDefined();
});
it('should search when 3+ characters typed', fakeAsync(() => {
spyOn((typeaheadComp as any).authorityService, 'getEntriesByName').and.callThrough();
typeaheadComp.search(Observable.of('test')).subscribe(() => {
expect((typeaheadComp as any).authorityService.getEntriesByName).toHaveBeenCalled();
});
}));
it('should set model.value on input type when AuthorityOptions.closed is false', () => {
const inputDe = typeaheadFixture.debugElement.query(By.css('input.form-control'));
const inputElement = inputDe.nativeElement;
inputElement.value = 'test value';
inputElement.dispatchEvent(new Event('input'));
expect((typeaheadComp.model as any).value).toEqual(new FormFieldMetadataValueObject('test value'))
});
it('should not set model.value on input type when AuthorityOptions.closed is true', () => {
typeaheadComp.model.authorityOptions.closed = true;
typeaheadFixture.detectChanges();
const inputDe = typeaheadFixture.debugElement.query(By.css('input.form-control'));
const inputElement = inputDe.nativeElement;
inputElement.value = 'test value';
inputElement.dispatchEvent(new Event('input'));
expect(typeaheadComp.model.value).not.toBeDefined();
});
it('should emit blur Event onBlur', () => {
spyOn(typeaheadComp.blur, 'emit');
typeaheadComp.onBlur(new Event('blur'));
expect(typeaheadComp.blur.emit).toHaveBeenCalled();
});
it('should emit change Event onBlur when AuthorityOptions.closed is false', () => {
typeaheadComp.inputValue = 'test value';
typeaheadFixture.detectChanges();
spyOn(typeaheadComp.blur, 'emit');
spyOn(typeaheadComp.change, 'emit');
typeaheadComp.onBlur(new Event('blur'));
// expect(typeaheadComp.change.emit).toHaveBeenCalled();
expect(typeaheadComp.blur.emit).toHaveBeenCalled();
});
it('should emit focus Event onFocus', () => {
spyOn(typeaheadComp.focus, 'emit');
typeaheadComp.onFocus(new Event('focus'));
expect(typeaheadComp.focus.emit).toHaveBeenCalled();
});
});
describe('and init model value is not empty', () => {
beforeEach(() => {
typeaheadFixture = TestBed.createComponent(DsDynamicTypeaheadComponent);
typeaheadComp = typeaheadFixture.componentInstance; // FormComponent test instance
typeaheadComp.group = TYPEAHEAD_TEST_GROUP;
typeaheadComp.model = new DynamicTypeaheadModel(TYPEAHEAD_TEST_MODEL_CONFIG);
(typeaheadComp.model as any).value = new FormFieldMetadataValueObject('test', null, 'test001');
typeaheadFixture.detectChanges();
});
afterEach(() => {
typeaheadFixture.destroy();
typeaheadComp = null;
});
it('should init component properly', () => {
expect(typeaheadComp.currentValue).toEqual(new FormFieldMetadataValueObject('test', null, 'test001'));
});
it('should emit change Event onChange and currentValue is empty', () => {
typeaheadComp.currentValue = null;
spyOn(typeaheadComp.change, 'emit');
typeaheadComp.onChange(new Event('change'));
expect(typeaheadComp.change.emit).toHaveBeenCalled();
expect(typeaheadComp.model.value).toBeNull();
});
});
});
});
// declare a test component
@@ -86,30 +224,9 @@ describe('Dynamic Dynamic Typeahead component', () => {
})
class TestComponent {
group: FormGroup = new FormGroup({
typeahead: new FormControl(),
});
group: FormGroup = TYPEAHEAD_TEST_GROUP;
inputTypeaheadModelConfig = {
authorityOptions: {
closed: false,
metadata: 'typeahead',
name: 'EVENTAuthority',
scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23'
} as AuthorityOptions,
disabled: false,
id: 'typeahead',
label: 'Conference',
minChars: 3,
name: 'typeahead',
placeholder: 'Conference',
readOnly: false,
required: false,
repeatable: false,
value: undefined
}
model = new DynamicTypeaheadModel(this.inputTypeaheadModelConfig);
model = new DynamicTypeaheadModel(TYPEAHEAD_TEST_MODEL_CONFIG);
showErrorMessages = false;

View File

@@ -92,12 +92,9 @@ export class DsDynamicTypeaheadComponent implements OnInit {
this.inputValue = valueObj;
this.model.valueUpdates.next(this.inputValue);
}
if (event.data) {
// this.group.markAsDirty();
}
}
onBlurEvent(event: Event) {
onBlur(event: Event) {
if (!this.model.authorityOptions.closed && isNotEmpty(this.inputValue)) {
this.change.emit(this.inputValue);
this.inputValue = null;
@@ -105,7 +102,7 @@ export class DsDynamicTypeaheadComponent implements OnInit {
this.blur.emit(event);
}
onChangeEvent(event: Event) {
onChange(event: Event) {
event.stopPropagation();
if (isEmpty(this.currentValue)) {
this.model.valueUpdates.next(null);
@@ -113,7 +110,7 @@ export class DsDynamicTypeaheadComponent implements OnInit {
}
}
onFocusEvent(event) {
onFocus(event) {
this.focus.emit(event);
}

View File

@@ -2,11 +2,20 @@ import { Observable } from 'rxjs/Observable';
import { IntegrationSearchOptions } from '../../core/integration/models/integration-options.model';
import { IntegrationData } from '../../core/integration/integration-data';
import { PageInfo } from '../../core/shared/page-info.model';
import { AuthorityValueModel } from '../../core/integration/models/authority-value.model';
export class AuthorityServiceStub {
private _payload = [
Object.assign(new AuthorityValueModel(),{id: 1, display: 'one', value: 1}),
Object.assign(new AuthorityValueModel(),{id: 2, display: 'two', value: 2}),
];
setNewPayload(payload) {
this._payload = payload;
}
getEntriesByName(options: IntegrationSearchOptions) {
const payload = [{id: 1, display: 'one', value: 1} as any, {id: 2, display: 'two', value: 2} as any];
return Observable.of(new IntegrationData(new PageInfo(), payload));
return Observable.of(new IntegrationData(new PageInfo(), this._payload));
}
}