From d1d809910f7c930b632e02bb99ebb560d918c2f8 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Wed, 13 Jun 2018 18:25:09 +0200 Subject: [PATCH] Added unit test --- .../date-picker/date-picker.component.spec.ts | 98 +++++++++++ .../models/ds-dynamic-input.model.ts | 4 +- .../dynamic-group.component.spec.ts | 159 ++++++++++++++++++ .../list/dynamic-list-checkbox-group.model.ts | 2 +- .../list/dynamic-list.component.spec.ts | 126 ++++++++++++++ .../lookup/dynamic-lookup.component.spec.ts | 124 ++++++++++++++ ...amic-scrollable-dropdown.component.spec.ts | 121 +++++++++++++ .../models/tag/dynamic-tag.component.spec.ts | 117 +++++++++++++ .../models/tag/dynamic-tag.model.spec.ts | 143 ---------------- .../dynamic-typeahead.component.spec.ts | 118 +++++++++++++ .../shared/testing/authority-service-stub.ts | 12 ++ 11 files changed, 878 insertions(+), 146 deletions(-) create mode 100644 src/app/shared/form/builder/ds-dynamic-form-ui/models/date-picker/date-picker.component.spec.ts create mode 100644 src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-group/dynamic-group.component.spec.ts create mode 100644 src/app/shared/form/builder/ds-dynamic-form-ui/models/list/dynamic-list.component.spec.ts create mode 100644 src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.component.spec.ts create mode 100644 src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.spec.ts create mode 100644 src/app/shared/form/builder/ds-dynamic-form-ui/models/tag/dynamic-tag.component.spec.ts delete mode 100644 src/app/shared/form/builder/ds-dynamic-form-ui/models/tag/dynamic-tag.model.spec.ts create mode 100644 src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.spec.ts create mode 100644 src/app/shared/testing/authority-service-stub.ts diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/date-picker/date-picker.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/date-picker/date-picker.component.spec.ts new file mode 100644 index 0000000000..f7d5fc1501 --- /dev/null +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/date-picker/date-picker.component.spec.ts @@ -0,0 +1,98 @@ +// 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 { NgbModule } from '@ng-bootstrap/ng-bootstrap'; +import { SortablejsModule } from 'angular-sortablejs'; +import { DsDatePickerComponent } from './date-picker.component'; +import { FormControl, FormGroup } from '@angular/forms'; +import { DynamicDsDatePickerModel } from './date-picker.model'; + +function createTestComponent(html: string, type: { new(...args: any[]): T }): ComponentFixture { + TestBed.overrideComponent(type, { + set: {template: html} + }); + const fixture = TestBed.createComponent(type); + + fixture.detectChanges(); + return fixture as ComponentFixture; +} + +describe('Date Picker component', () => { + + let testComp: TestComponent; + let testFixture: ComponentFixture; + let html; + + // async beforeEach + beforeEach(async(() => { + + TestBed.configureTestingModule({ + imports: [ + NgbModule.forRoot() + ], + declarations: [ + DsDatePickerComponent, + TestComponent, + ], // declare the test component + providers: [ + ChangeDetectorRef, + DsDatePickerComponent, + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA] + }); + + })); + + // synchronous beforeEach + beforeEach(() => { + html = ` + `; + + testFixture = createTestComponent(html, TestComponent) as ComponentFixture; + testComp = testFixture.componentInstance; + }); + + it('should create DsDatePickerComponent', inject([DsDatePickerComponent], (app: DsDatePickerComponent) => { + + expect(app).toBeDefined(); + })); + +}); + +// declare a test component +@Component({ + selector: 'ds-test-cmp', + template: `` +}) +class TestComponent { + + group = new FormGroup({ + date: new FormControl(), + }); + + 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); + + showErrorMessages = false; + +} diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model.ts index d8869f2829..f739c17cf3 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model.ts @@ -12,8 +12,8 @@ import { hasValue } from '../../../../empty.util'; import { FormFieldMetadataValueObject } from '../../models/form-field-metadata-value.model'; export interface DsDynamicInputModelConfig extends DynamicInputModelConfig { - authorityOptions: AuthorityOptions; - languageCodes: LanguageCode[]; + authorityOptions?: AuthorityOptions; + languageCodes?: LanguageCode[]; language?: string; value?: any; } diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-group/dynamic-group.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-group/dynamic-group.component.spec.ts new file mode 100644 index 0000000000..51b0838fd5 --- /dev/null +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-group/dynamic-group.component.spec.ts @@ -0,0 +1,159 @@ +// 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 { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms'; + +import { TranslateModule } from '@ngx-translate/core'; +import 'rxjs/add/observable/of'; +import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; + +import { DsDynamicGroupComponent } from './dynamic-group.components'; +import { DynamicGroupModel } from './dynamic-group.model'; +import { FormRowModel } from '../../../../../../core/shared/config/config-submission-forms.model'; +import { FormFieldModel } from '../../../models/form-field.model'; +import { FormBuilderService } from '../../../form-builder.service'; +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'; + +function createTestComponent(html: string, type: { new(...args: any[]): T }): ComponentFixture { + TestBed.overrideComponent(type, { + set: {template: html} + }); + const fixture = TestBed.createComponent(type); + + fixture.detectChanges(); + return fixture as ComponentFixture; +} + +describe('Dynamic Group component', () => { + + let testComp: TestComponent; + let testFixture: ComponentFixture; + let html; + + const mockStore: Store = jasmine.createSpyObj('store', { + dispatch: {}, + select: Observable.of(true) + }); + + // async beforeEach + beforeEach(async(() => { + + TestBed.configureTestingModule({ + imports: [ + FormsModule, + ReactiveFormsModule, + NgbModule.forRoot(), + TranslateModule.forRoot() + ], + declarations: [ + FormComponent, + DsDynamicGroupComponent, + TestComponent, + ], // declare the test component + providers: [ + ChangeDetectorRef, + DsDynamicGroupComponent, + DynamicFormValidationService, + FormBuilderService, + FormComponent, + FormService, + {provide: GLOBAL_CONFIG, useValue: {} as GlobalConfig}, + {provide: Store, useValue: mockStore}, + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA] + }); + + })); + + // synchronous beforeEach + beforeEach(() => { + html = ` + `; + + testFixture = createTestComponent(html, TestComponent) as ComponentFixture; + testComp = testFixture.componentInstance; + }); + + it('should create DsDynamicGroupComponent', inject([DsDynamicGroupComponent], (app: DsDynamicGroupComponent) => { + + expect(app).toBeDefined(); + })); + +}); + +// declare a test component +@Component({ + selector: 'ds-test-cmp', + template: `` +}) +class TestComponent { + + group = new FormGroup({ + date: new FormControl(), + }); + + groupModelConfig = { + disabled: false, + errorMessages: {required: 'You must specify at least one author.'}, + formConfiguration: [{ + fields: [{ + hints: 'Enter the name of the author.', + input: {type: 'onebox'}, + label: 'Authors', + languageCodes: [], + mandatory: 'true', + mandatoryMessage: 'Required field!', + repeatable: false, + selectableMetadata: [{ + authority: 'RPAuthority', + closed: false, + metadata: 'dc.contributor.author' + }], + } as FormFieldModel] + } as FormRowModel, { + fields: [{ + hints: 'Enter the affiliation of the author.', + input: {type: 'onebox'}, + label: 'Affiliation', + languageCodes: [], + mandatory: 'false', + repeatable: false, + selectableMetadata: [{ + authority: 'OUAuthority', + closed: false, + metadata: 'local.contributor.affiliation' + }] + } as FormFieldModel] + } as FormRowModel], + id: 'date', + label: 'Date', + mandatoryField: 'dc.contributor.author', + name: 'date', + placeholder: 'Date', + readOnly: false, + relationFields: ['local.contributor.affiliation'], + required: true, + scopeUUID: '43fe1f8c-09a6-4fcf-9c78-5d4fed8f2c8f', + submissionScope: undefined, + validators: {required: null} + }; + + model = new DynamicGroupModel(this.groupModelConfig); + + showErrorMessages = false; + +} diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/list/dynamic-list-checkbox-group.model.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/list/dynamic-list-checkbox-group.model.ts index 775da3bd33..1537a02cd1 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/list/dynamic-list-checkbox-group.model.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/list/dynamic-list-checkbox-group.model.ts @@ -11,7 +11,7 @@ import { hasValue } from '../../../../../empty.util'; export interface DynamicListCheckboxGroupModelConfig extends DynamicFormGroupModelConfig { authorityOptions: AuthorityOptions; - groupLength: number; + groupLength?: number; repeatable: boolean; value?: any; } diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/list/dynamic-list.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/list/dynamic-list.component.spec.ts new file mode 100644 index 0000000000..45bbbaf564 --- /dev/null +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/list/dynamic-list.component.spec.ts @@ -0,0 +1,126 @@ +// Load the implementations that should be tested +import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; +import { FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms'; +import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing'; + +import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; + +import { DsDynamicListComponent } from './dynamic-list.component'; +import { DynamicListCheckboxGroupModel } from './dynamic-list-checkbox-group.model'; +import { AuthorityOptions } from '../../../../../../core/integration/models/authority-options.model'; +import { FormBuilderService } from '../../../form-builder.service'; +import { DynamicFormControlLayout, 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'; +import { FormBuilderServiceStub } from '../../../../../testing/form-builder-service-stub'; + +function createTestComponent(html: string, type: { new(...args: any[]): T }): ComponentFixture { + TestBed.overrideComponent(type, { + set: {template: html} + }); + const fixture = TestBed.createComponent(type); + + fixture.detectChanges(); + return fixture as ComponentFixture; +} + +describe('Dynamic List component', () => { + + let testComp: TestComponent; + let testFixture: ComponentFixture; + let html; + + // async beforeEach + beforeEach(async(() => { + const authorityServiceStub = new AuthorityServiceStub(); + const formBuilderServiceStub = new FormBuilderServiceStub(); + + TestBed.configureTestingModule({ + imports: [ + DynamicFormsCoreModule, + DynamicFormsNGBootstrapUIModule, + FormsModule, + ReactiveFormsModule, + NgbModule.forRoot() + ], + declarations: [ + DsDynamicListComponent, + TestComponent, + ], // declare the test component + providers: [ + AuthorityService, + ChangeDetectorRef, + DsDynamicListComponent, + DynamicFormValidationService, + FormBuilderService, + {provide: AuthorityService, useValue: authorityServiceStub}, + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA] + }); + + })); + + // synchronous beforeEach + beforeEach(() => { + html = ` + `; + + testFixture = createTestComponent(html, TestComponent) as ComponentFixture; + testComp = testFixture.componentInstance; + }); + + it('should create DsDynamicListComponent', inject([DsDynamicListComponent], (app: DsDynamicListComponent) => { + + expect(app).toBeDefined(); + })); + +}); + +// declare a test component +@Component({ + selector: 'ds-test-cmp', + template: `` +}) +class TestComponent { + + group: FormGroup = new FormGroup({ + list: new FormGroup({}), + }); + + inputListModelConfig = { + authorityOptions: { + closed: false, + metadata: 'list', + name: 'type_programme', + scope: 'c1c16450-d56f-41bc-bb81-27f1d1eb5c23' + } as AuthorityOptions, + disabled: false, + errorMessages: {required: 'You must enter at least the year.'}, + id: 'list', + label: 'Programme', + name: 'list', + placeholder: 'Programme', + readOnly: false, + required: true, + repeatable: true + }; + + layout: DynamicFormControlLayout = { + element: { + group: '' + } + }; + + model = new DynamicListCheckboxGroupModel(this.inputListModelConfig, this.layout); + + showErrorMessages = false; + +} diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.component.spec.ts new file mode 100644 index 0000000000..8a92106387 --- /dev/null +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.component.spec.ts @@ -0,0 +1,124 @@ +// 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 { NgbModule } from '@ng-bootstrap/ng-bootstrap'; + +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 { AuthorityService } from '../../../../../../core/integration/authority.service'; +import { AuthorityServiceStub } from '../../../../../testing/authority-service-stub'; +import { FormBuilderServiceStub } from '../../../../../testing/form-builder-service-stub'; +import { DsDynamicLookupComponent } from './dynamic-lookup.component'; +import { DynamicLookupModel } from './dynamic-lookup.model'; +import { InfiniteScrollModule } from 'ngx-infinite-scroll'; +import { TranslateModule } from '@ngx-translate/core'; + +function createTestComponent(html: string, type: { new(...args: any[]): T }): ComponentFixture { + TestBed.overrideComponent(type, { + set: {template: html} + }); + const fixture = TestBed.createComponent(type); + + fixture.detectChanges(); + return fixture as ComponentFixture; +} + +describe('Dynamic Lookup component', () => { + + let testComp: TestComponent; + let testFixture: ComponentFixture; + let html; + + // async beforeEach + beforeEach(async(() => { + const authorityServiceStub = new AuthorityServiceStub(); + const formBuilderServiceStub = new FormBuilderServiceStub(); + + TestBed.configureTestingModule({ + imports: [ + DynamicFormsCoreModule, + DynamicFormsNGBootstrapUIModule, + FormsModule, + InfiniteScrollModule, + ReactiveFormsModule, + NgbModule.forRoot(), + TranslateModule.forRoot() + ], + declarations: [ + DsDynamicLookupComponent, + TestComponent, + ], // declare the test component + providers: [ + ChangeDetectorRef, + DsDynamicLookupComponent, + {provide: AuthorityService, useValue: authorityServiceStub}, + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA] + }); + + })); + + // synchronous beforeEach + beforeEach(() => { + html = ` + `; + + testFixture = createTestComponent(html, TestComponent) as ComponentFixture; + testComp = testFixture.componentInstance; + }); + + it('should create DsDynamicLookupComponent', inject([DsDynamicLookupComponent], (app: DsDynamicLookupComponent) => { + + expect(app).toBeDefined(); + })); + +}); + +// declare a test component +@Component({ + selector: 'ds-test-cmp', + template: `` +}) +class TestComponent { + + group: FormGroup = new FormGroup({ + lookup: new FormControl(), + }); + + 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 + }; + + model = new DynamicLookupModel(this.inputLookupModelConfig); + + showErrorMessages = false; + +} diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.spec.ts new file mode 100644 index 0000000000..70152c2cb3 --- /dev/null +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.spec.ts @@ -0,0 +1,121 @@ +// 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 { NgbModule } from '@ng-bootstrap/ng-bootstrap'; + +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 { AuthorityService } from '../../../../../../core/integration/authority.service'; +import { AuthorityServiceStub } from '../../../../../testing/authority-service-stub'; +import { FormBuilderServiceStub } from '../../../../../testing/form-builder-service-stub'; +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'; + +function createTestComponent(html: string, type: { new(...args: any[]): T }): ComponentFixture { + TestBed.overrideComponent(type, { + set: {template: html} + }); + const fixture = TestBed.createComponent(type); + + fixture.detectChanges(); + return fixture as ComponentFixture; +} + +describe('Dynamic Dynamic Scrollable Dropdown component', () => { + + let testComp: TestComponent; + let testFixture: ComponentFixture; + let html; + + // async beforeEach + beforeEach(async(() => { + const authorityServiceStub = new AuthorityServiceStub(); + const formBuilderServiceStub = new FormBuilderServiceStub(); + + TestBed.configureTestingModule({ + imports: [ + DynamicFormsCoreModule, + DynamicFormsNGBootstrapUIModule, + FormsModule, + InfiniteScrollModule, + ReactiveFormsModule, + NgbModule.forRoot(), + TranslateModule.forRoot() + ], + declarations: [ + DsDynamicScrollableDropdownComponent, + TestComponent, + ], // declare the test component + providers: [ + ChangeDetectorRef, + DsDynamicScrollableDropdownComponent, + {provide: AuthorityService, useValue: authorityServiceStub}, + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA] + }); + + })); + + // synchronous beforeEach + beforeEach(() => { + html = ` + `; + + testFixture = createTestComponent(html, TestComponent) as ComponentFixture; + testComp = testFixture.componentInstance; + }); + + it('should create DsDynamicScrollableDropdownComponent', inject([DsDynamicScrollableDropdownComponent], (app: DsDynamicScrollableDropdownComponent) => { + + expect(app).toBeDefined(); + })); + +}); + +// declare a test component +@Component({ + selector: 'ds-test-cmp', + template: `` +}) +class TestComponent { + + group: FormGroup = new FormGroup({ + lookup: new FormControl(), + }); + + 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); + + showErrorMessages = false; + +} diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/tag/dynamic-tag.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/tag/dynamic-tag.component.spec.ts new file mode 100644 index 0000000000..3c599caf8d --- /dev/null +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/tag/dynamic-tag.component.spec.ts @@ -0,0 +1,117 @@ +// 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 { 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 { AuthorityService } from '../../../../../../core/integration/authority.service'; +import { AuthorityServiceStub } from '../../../../../testing/authority-service-stub'; +import { FormBuilderServiceStub } from '../../../../../testing/form-builder-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'; + +function createTestComponent(html: string, type: { new(...args: any[]): T }): ComponentFixture { + TestBed.overrideComponent(type, { + set: {template: html} + }); + const fixture = TestBed.createComponent(type); + + fixture.detectChanges(); + return fixture as ComponentFixture; +} + +describe('Dynamic Dynamic Tag component', () => { + + let testComp: TestComponent; + let testFixture: ComponentFixture; + let html; + + // async beforeEach + beforeEach(async(() => { + const authorityServiceStub = new AuthorityServiceStub(); + const formBuilderServiceStub = new FormBuilderServiceStub(); + + TestBed.configureTestingModule({ + imports: [ + DynamicFormsCoreModule, + DynamicFormsNGBootstrapUIModule, + FormsModule, + NgbModule.forRoot(), + ReactiveFormsModule, + ], + declarations: [ + DsDynamicTagComponent, + TestComponent, + ], // declare the test component + providers: [ + ChangeDetectorRef, + DsDynamicTagComponent, + {provide: AuthorityService, useValue: authorityServiceStub}, + {provide: GLOBAL_CONFIG, useValue: {} as GlobalConfig}, + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA] + }); + + })); + + // synchronous beforeEach + beforeEach(() => { + html = ` + `; + + testFixture = createTestComponent(html, TestComponent) as ComponentFixture; + testComp = testFixture.componentInstance; + }); + + it('should create DsDynamicTagComponent', inject([DsDynamicTagComponent], (app: DsDynamicTagComponent) => { + + expect(app).toBeDefined(); + })); + +}); + +// declare a test component +@Component({ + selector: 'ds-test-cmp', + template: `` +}) +class TestComponent { + + group: FormGroup = new FormGroup({ + lookup: new FormControl(), + }); + + 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); + + showErrorMessages = false; + +} diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/tag/dynamic-tag.model.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/tag/dynamic-tag.model.spec.ts deleted file mode 100644 index 08f26a99de..0000000000 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/tag/dynamic-tag.model.spec.ts +++ /dev/null @@ -1,143 +0,0 @@ -// import { AUTOCOMPLETE_OFF, DYNAMIC_FORM_CONTROL_INPUT_TYPE_TEXT } from '@ng-dynamic-forms/core'; -// import { Observable } from 'rxjs/Observable'; -// -// import { -// DYNAMIC_FORM_CONTROL_TYPE_TYPEAHEAD, DynamicTypeaheadModel, -// DynamicTypeaheadResponseModel -// } from './dynamic-tag.model'; -// import { PageInfo } from '../../../../../../core/shared/page-info.model'; -// -// describe('DynamicTypeaheadModel test suite', () => { -// -// let model: any; -// const search = (text: string): Observable => -// Observable.of({ -// list: ['One', 'Two', 'Three'], -// pageInfo: new PageInfo() -// }); -// const config = { -// id: 'input', -// minChars: 3, -// search: search -// }; -// -// beforeEach(() => model = new DynamicTypeaheadModel(config)); -// -// it('tests if correct default type property is set', () => { -// -// expect(model.type).toEqual(DYNAMIC_FORM_CONTROL_TYPE_TYPEAHEAD); -// }); -// -// it('tests if correct default input type property is set', () => { -// -// expect(model.inputType).toEqual(DYNAMIC_FORM_CONTROL_INPUT_TYPE_TEXT); -// }); -// -// it('tests if correct default autoComplete property is set', () => { -// -// expect(model.autoComplete).toEqual(AUTOCOMPLETE_OFF); -// }); -// -// it('tests if correct default autoFocus property is set', () => { -// -// expect(model.autoFocus).toBe(false); -// }); -// -// it('tests if correct default cls properties aree set', () => { -// -// expect(model.cls).toBeDefined(); -// expect(model.cls.element.container).toEqual(''); -// expect(model.cls.element.control).toEqual(''); -// expect(model.cls.element.errors).toEqual(''); -// expect(model.cls.element.label).toEqual(''); -// expect(model.cls.grid.container).toEqual(''); -// expect(model.cls.grid.control).toEqual(''); -// expect(model.cls.grid.errors).toEqual(''); -// expect(model.cls.grid.label).toEqual(''); -// }); -// -// it('tests if correct default hint property is set', () => { -// -// expect(model.hint).toBeNull(); -// }); -// -// it('tests if correct default label property is set', () => { -// -// expect(model.label).toBeNull(); -// }); -// -// it('tests if correct default max property is set', () => { -// -// expect(model.max).toBeNull(); -// }); -// -// it('tests if correct default maxLength property is set', () => { -// -// expect(model.maxLength).toBeNull(); -// }); -// -// it('tests if correct default minLength property is set', () => { -// -// expect(model.minLength).toBeNull(); -// }); -// -// it('tests if correct minChars property is set', () => { -// -// expect(model.minChars).toEqual(3); -// }); -// -// it('tests if correct default min property is set', () => { -// -// expect(model.min).toBeNull(); -// }); -// -// it('tests if correct default placeholder property is set', () => { -// -// expect(model.placeholder).toEqual(''); -// }); -// -// it('tests if correct default readonly property is set', () => { -// -// expect(model.readOnly).toBe(false); -// }); -// -// it('tests if correct default required property is set', () => { -// -// expect(model.required).toBe(false); -// }); -// -// it('tests if correct default spellcheck property is set', () => { -// -// expect(model.spellCheck).toBe(false); -// }); -// -// it('tests if correct default step property is set', () => { -// -// expect(model.step).toBeNull(); -// }); -// -// it('tests if correct default prefix property is set', () => { -// -// expect(model.prefix).toBeNull(); -// }); -// -// it('tests if correct default suffix property is set', () => { -// -// expect(model.suffix).toBeNull(); -// }); -// -// it('tests if correct search function is set', () => { -// -// expect(model.search).toBe(search); -// }); -// -// it('should serialize correctly', () => { -// -// const json = JSON.parse(JSON.stringify(model)); -// -// expect(json.id).toEqual(model.id); -// expect(json.disabled).toEqual(model.disabled); -// expect(json.value).toBe(model.value); -// expect(json.type).toEqual(DYNAMIC_FORM_CONTROL_TYPE_TYPEAHEAD); -// }); -// }); diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.spec.ts new file mode 100644 index 0000000000..4feaea012a --- /dev/null +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.spec.ts @@ -0,0 +1,118 @@ +// 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 { 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 { AuthorityService } from '../../../../../../core/integration/authority.service'; +import { AuthorityServiceStub } from '../../../../../testing/authority-service-stub'; +import { FormBuilderServiceStub } from '../../../../../testing/form-builder-service-stub'; +import { GlobalConfig } from '../../../../../../../config/global-config.interface'; +import { GLOBAL_CONFIG } from '../../../../../../../config'; +import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; +import { DsDynamicTypeaheadComponent } from './dynamic-typeahead.component'; +import { DynamicTypeaheadModel } from './dynamic-typeahead.model'; + +function createTestComponent(html: string, type: { new(...args: any[]): T }): ComponentFixture { + TestBed.overrideComponent(type, { + set: {template: html} + }); + const fixture = TestBed.createComponent(type); + + fixture.detectChanges(); + return fixture as ComponentFixture; +} + +describe('Dynamic Dynamic Typeahead component', () => { + + let testComp: TestComponent; + let testFixture: ComponentFixture; + let html; + + // async beforeEach + beforeEach(async(() => { + const authorityServiceStub = new AuthorityServiceStub(); + const formBuilderServiceStub = new FormBuilderServiceStub(); + + TestBed.configureTestingModule({ + imports: [ + DynamicFormsCoreModule, + DynamicFormsNGBootstrapUIModule, + FormsModule, + NgbModule.forRoot(), + ReactiveFormsModule, + ], + declarations: [ + DsDynamicTypeaheadComponent, + TestComponent, + ], // declare the test component + providers: [ + ChangeDetectorRef, + DsDynamicTypeaheadComponent, + {provide: AuthorityService, useValue: authorityServiceStub}, + {provide: GLOBAL_CONFIG, useValue: {} as GlobalConfig}, + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA] + }); + + })); + + // synchronous beforeEach + beforeEach(() => { + html = ` + `; + + testFixture = createTestComponent(html, TestComponent) as ComponentFixture; + testComp = testFixture.componentInstance; + }); + + it('should create DsDynamicTypeaheadComponent', inject([DsDynamicTypeaheadComponent], (app: DsDynamicTypeaheadComponent) => { + + expect(app).toBeDefined(); + })); + +}); + +// declare a test component +@Component({ + selector: 'ds-test-cmp', + template: `` +}) +class TestComponent { + + group: FormGroup = new FormGroup({ + typeahead: new FormControl(), + }); + + 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); + + showErrorMessages = false; + +} diff --git a/src/app/shared/testing/authority-service-stub.ts b/src/app/shared/testing/authority-service-stub.ts new file mode 100644 index 0000000000..df1b98c787 --- /dev/null +++ b/src/app/shared/testing/authority-service-stub.ts @@ -0,0 +1,12 @@ +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'; + +export class AuthorityServiceStub { + + 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)); + } +}