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 index 76cd0947bb..02f1415e99 100644 --- 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 @@ -12,16 +12,7 @@ import { FormBuilderService } from '../../../form-builder.service'; import { FormComponent } from '../../../../form.component'; import { FormService } from '../../../../form.service'; - -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; -} +import { createTestComponent } from '../../../../../testing/utils'; export const DATE_TEST_GROUP = new FormGroup({ date: new FormControl() 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 index 4d8ff006fb..d1e6f67287 100644 --- 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 @@ -16,7 +16,6 @@ import { FormRowModel, SubmissionFormsModel } from '../../../../../../core/share 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 { AppState } from '../../../../../../app.reducer'; @@ -24,16 +23,7 @@ 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'; - -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; -} +import { createTestComponent } from '../../../../../testing/utils'; export const FORM_GROUP_TEST_MODEL_CONFIG = { disabled: false, diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-group/dynamic-group.components.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-group/dynamic-group.components.ts index 6fe4686974..a55e7aff9d 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-group/dynamic-group.components.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-group/dynamic-group.components.ts @@ -79,9 +79,17 @@ export class DsDynamicGroupComponent implements OnDestroy, OnInit { }); this.formId = this.formService.getUniqueId(this.model.id); - this.formModel = this.formBuilderService.modelFromConfiguration(config, this.model.scopeUUID, {}, this.model.submissionScope, this.model.readOnly); + this.formModel = this.formBuilderService.modelFromConfiguration( + config, + this.model.scopeUUID, + {}, + this.model.submissionScope, + this.model.readOnly); const initChipsValue = this.model.isEmpty() ? [] : this.model.value; - this.chips = new Chips(initChipsValue, 'value', this.model.mandatoryField); + this.chips = new Chips( + initChipsValue, + 'value', + this.model.mandatoryField); this.subs.push( this.chips.chipsItems .subscribe((subItems: 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 index 29babc2da9..6a765eba4a 100644 --- 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 @@ -16,16 +16,7 @@ import { AuthorityServiceStub } from '../../../../../testing/authority-service-s import { DynamicListRadioGroupModel } from './dynamic-list-radio-group.model'; import { By } from '@angular/platform-browser'; import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.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; -} +import { createTestComponent } from '../../../../../testing/utils'; export const LAYOUT_TEST = { element: { 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 index b2cce76be6..ce45453bee 100644 --- 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 @@ -21,16 +21,7 @@ import { FormFieldMetadataValueObject } from '../../../models/form-field-metadat import { By } from '@angular/platform-browser'; import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model'; import { DynamicLookupNameModel } from './dynamic-lookup-name.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; -} +import { createTestComponent } from '../../../../../testing/utils'; export const LOOKUP_TEST_MODEL_CONFIG = { authorityOptions: { 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 index de35d300f5..49cdb5d890 100644 --- 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 @@ -19,21 +19,7 @@ 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(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; -} - -function hasClass(element: any, className: string): boolean { - const classes = element.getAttribute('class'); - return classes.split(' ').indexOf(className) !== -1; -} +import { hasClass, createTestComponent } from '../../../../../testing/utils'; export const SD_TEST_GROUP = new FormGroup({ dropdown: new FormControl(), 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 index 5788c00062..24959f4be4 100644 --- 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 @@ -19,16 +19,7 @@ import { GLOBAL_CONFIG } from '../../../../../../../config'; 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(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; -} +import { createTestComponent } from '../../../../../testing/utils'; function createKeyUpEvent(key: number) { /* tslint:disable:no-empty */ 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 index 4429cfc574..2ed145b03a 100644 --- 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 @@ -18,16 +18,7 @@ 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(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; -} +import { createTestComponent } from '../../../../../testing/utils'; export const TYPEAHEAD_TEST_GROUP = new FormGroup({ typeahead: new FormControl(), diff --git a/src/app/shared/form/form.component.spec.ts b/src/app/shared/form/form.component.spec.ts index 576af76141..53cc1d7736 100644 --- a/src/app/shared/form/form.component.spec.ts +++ b/src/app/shared/form/form.component.spec.ts @@ -2,7 +2,7 @@ import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/c import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing'; import { CommonModule } from '@angular/common'; import { BrowserModule } from '@angular/platform-browser'; -import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms'; +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import 'rxjs/add/observable/of'; import { DynamicFormArrayModel, @@ -23,16 +23,7 @@ import { FormChangeAction, FormStatusChangeAction } from './form.actions'; import { MockStore } from '../testing/mock-store'; import { FormFieldMetadataValueObject } from './builder/models/form-field-metadata-value.model'; import { GLOBAL_CONFIG } from '../../../config'; - -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; -} +import { createTestComponent } from '../testing/utils'; export const TEST_FORM_MODEL = [ @@ -212,6 +203,7 @@ describe('FormComponent test suite', () => { it('should display form errors when errors are added to the state', () => { const errors = [{ fieldId: 'dc_title', + fieldIndex: 0, message: 'error.validation.required' }]; diff --git a/src/app/shared/number-picker/number-picker.component.spec.ts b/src/app/shared/number-picker/number-picker.component.spec.ts index b7f696a42e..3f6ae441f8 100644 --- a/src/app/shared/number-picker/number-picker.component.spec.ts +++ b/src/app/shared/number-picker/number-picker.component.spec.ts @@ -1,23 +1,13 @@ // 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 { UploaderService } from '../uploader/uploader.service'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { By } from '@angular/platform-browser'; import { NumberPickerComponent } from './number-picker.component'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; - -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; -} +import { createTestComponent } from '../testing/utils'; describe('NumberPickerComponent test suite', () => { diff --git a/src/app/shared/pagination/pagination.component.spec.ts b/src/app/shared/pagination/pagination.component.spec.ts index d5d0c4980c..1bd97086d8 100644 --- a/src/app/shared/pagination/pagination.component.spec.ts +++ b/src/app/shared/pagination/pagination.component.spec.ts @@ -44,16 +44,7 @@ import { EnumKeysPipe } from '../utils/enum-keys-pipe'; import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model'; import { GLOBAL_CONFIG, ENV_CONFIG } from '../../../config'; - -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; -} +import { createTestComponent } from '../testing/utils'; function expectPages(fixture: ComponentFixture, pagesDef: string[]): void { const de = fixture.debugElement.query(By.css('.pagination')); diff --git a/src/app/shared/testing/utils.ts b/src/app/shared/testing/utils.ts new file mode 100644 index 0000000000..9343ae50fd --- /dev/null +++ b/src/app/shared/testing/utils.ts @@ -0,0 +1,32 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +/** + * Returns true if a Native Element has a specified css class. + * + * @param element + * the Native Element + * @param className + * the class name to find + */ +export const hasClass = (element: any, className: string): boolean => { + const classes = element.getAttribute('class'); + return classes.split(' ').indexOf(className) !== -1; +}; + +/** + * Creates an instance of a component and returns test fixture. + * + * @param html + * the component's template as html + * @param type + * the type of the component to instantiate + */ +export const 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; +}; diff --git a/src/app/shared/uploader/uploader.component.spec.ts b/src/app/shared/uploader/uploader.component.spec.ts index f1e12e7693..a36bd7241b 100644 --- a/src/app/shared/uploader/uploader.component.spec.ts +++ b/src/app/shared/uploader/uploader.component.spec.ts @@ -9,16 +9,7 @@ import { UploaderOptions } from './uploader-options.model'; import { UploaderComponent } from './uploader.component'; import { FileUploadModule } from 'ng2-file-upload'; 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; -} +import { createTestComponent } from '../testing/utils'; describe('Chips component', () => {