mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 10:34:15 +00:00
Added testing utils functions
This commit is contained in:
@@ -12,16 +12,7 @@ import { FormBuilderService } from '../../../form-builder.service';
|
|||||||
|
|
||||||
import { FormComponent } from '../../../../form.component';
|
import { FormComponent } from '../../../../form.component';
|
||||||
import { FormService } from '../../../../form.service';
|
import { FormService } from '../../../../form.service';
|
||||||
|
import { createTestComponent } from '../../../../../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const DATE_TEST_GROUP = new FormGroup({
|
export const DATE_TEST_GROUP = new FormGroup({
|
||||||
date: new FormControl()
|
date: new FormControl()
|
||||||
|
@@ -16,7 +16,6 @@ import { FormRowModel, SubmissionFormsModel } from '../../../../../../core/share
|
|||||||
import { FormFieldModel } from '../../../models/form-field.model';
|
import { FormFieldModel } from '../../../models/form-field.model';
|
||||||
import { FormBuilderService } from '../../../form-builder.service';
|
import { FormBuilderService } from '../../../form-builder.service';
|
||||||
import { FormService } from '../../../../form.service';
|
import { FormService } from '../../../../form.service';
|
||||||
import { GlobalConfig } from '../../../../../../../config/global-config.interface';
|
|
||||||
import { GLOBAL_CONFIG } from '../../../../../../../config';
|
import { GLOBAL_CONFIG } from '../../../../../../../config';
|
||||||
import { FormComponent } from '../../../../form.component';
|
import { FormComponent } from '../../../../form.component';
|
||||||
import { AppState } from '../../../../../../app.reducer';
|
import { AppState } from '../../../../../../app.reducer';
|
||||||
@@ -24,16 +23,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
|
|||||||
import { Chips } from '../../../../../chips/models/chips.model';
|
import { Chips } from '../../../../../chips/models/chips.model';
|
||||||
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
|
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
|
||||||
import { DsDynamicInputModel } from '../ds-dynamic-input.model';
|
import { DsDynamicInputModel } from '../ds-dynamic-input.model';
|
||||||
|
import { createTestComponent } from '../../../../../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const FORM_GROUP_TEST_MODEL_CONFIG = {
|
export const FORM_GROUP_TEST_MODEL_CONFIG = {
|
||||||
disabled: false,
|
disabled: false,
|
||||||
|
@@ -79,9 +79,17 @@ export class DsDynamicGroupComponent implements OnDestroy, OnInit {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.formId = this.formService.getUniqueId(this.model.id);
|
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;
|
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.subs.push(
|
||||||
this.chips.chipsItems
|
this.chips.chipsItems
|
||||||
.subscribe((subItems: any[]) => {
|
.subscribe((subItems: any[]) => {
|
||||||
|
@@ -16,16 +16,7 @@ import { AuthorityServiceStub } from '../../../../../testing/authority-service-s
|
|||||||
import { DynamicListRadioGroupModel } from './dynamic-list-radio-group.model';
|
import { DynamicListRadioGroupModel } from './dynamic-list-radio-group.model';
|
||||||
import { By } from '@angular/platform-browser';
|
import { By } from '@angular/platform-browser';
|
||||||
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
|
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
|
||||||
|
import { createTestComponent } from '../../../../../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const LAYOUT_TEST = {
|
export const LAYOUT_TEST = {
|
||||||
element: {
|
element: {
|
||||||
|
@@ -21,16 +21,7 @@ import { FormFieldMetadataValueObject } from '../../../models/form-field-metadat
|
|||||||
import { By } from '@angular/platform-browser';
|
import { By } from '@angular/platform-browser';
|
||||||
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
|
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
|
||||||
import { DynamicLookupNameModel } from './dynamic-lookup-name.model';
|
import { DynamicLookupNameModel } from './dynamic-lookup-name.model';
|
||||||
|
import { createTestComponent } from '../../../../../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const LOOKUP_TEST_MODEL_CONFIG = {
|
export const LOOKUP_TEST_MODEL_CONFIG = {
|
||||||
authorityOptions: {
|
authorityOptions: {
|
||||||
|
@@ -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 { TYPEAHEAD_TEST_GROUP, TYPEAHEAD_TEST_MODEL_CONFIG } from '../typeahead/dynamic-typeahead.component.spec';
|
||||||
import { By } from '@angular/platform-browser';
|
import { By } from '@angular/platform-browser';
|
||||||
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
|
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
|
||||||
|
import { hasClass, createTestComponent } from '../../../../../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
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({
|
export const SD_TEST_GROUP = new FormGroup({
|
||||||
dropdown: new FormControl(),
|
dropdown: new FormControl(),
|
||||||
|
@@ -19,16 +19,7 @@ import { GLOBAL_CONFIG } from '../../../../../../../config';
|
|||||||
import { Chips } from '../../../../../chips/models/chips.model';
|
import { Chips } from '../../../../../chips/models/chips.model';
|
||||||
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
|
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
|
||||||
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
|
import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model';
|
||||||
|
import { createTestComponent } from '../../../../../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createKeyUpEvent(key: number) {
|
function createKeyUpEvent(key: number) {
|
||||||
/* tslint:disable:no-empty */
|
/* tslint:disable:no-empty */
|
||||||
|
@@ -18,16 +18,7 @@ import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
|
|||||||
import { DsDynamicTypeaheadComponent } from './dynamic-typeahead.component';
|
import { DsDynamicTypeaheadComponent } from './dynamic-typeahead.component';
|
||||||
import { DynamicTypeaheadModel } from './dynamic-typeahead.model';
|
import { DynamicTypeaheadModel } from './dynamic-typeahead.model';
|
||||||
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
|
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
|
||||||
|
import { createTestComponent } from '../../../../../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const TYPEAHEAD_TEST_GROUP = new FormGroup({
|
export const TYPEAHEAD_TEST_GROUP = new FormGroup({
|
||||||
typeahead: new FormControl(),
|
typeahead: new FormControl(),
|
||||||
|
@@ -2,7 +2,7 @@ import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/c
|
|||||||
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
|
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
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 'rxjs/add/observable/of';
|
||||||
import {
|
import {
|
||||||
DynamicFormArrayModel,
|
DynamicFormArrayModel,
|
||||||
@@ -23,16 +23,7 @@ import { FormChangeAction, FormStatusChangeAction } from './form.actions';
|
|||||||
import { MockStore } from '../testing/mock-store';
|
import { MockStore } from '../testing/mock-store';
|
||||||
import { FormFieldMetadataValueObject } from './builder/models/form-field-metadata-value.model';
|
import { FormFieldMetadataValueObject } from './builder/models/form-field-metadata-value.model';
|
||||||
import { GLOBAL_CONFIG } from '../../../config';
|
import { GLOBAL_CONFIG } from '../../../config';
|
||||||
|
import { createTestComponent } from '../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const TEST_FORM_MODEL = [
|
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', () => {
|
it('should display form errors when errors are added to the state', () => {
|
||||||
const errors = [{
|
const errors = [{
|
||||||
fieldId: 'dc_title',
|
fieldId: 'dc_title',
|
||||||
|
fieldIndex: 0,
|
||||||
message: 'error.validation.required'
|
message: 'error.validation.required'
|
||||||
}];
|
}];
|
||||||
|
|
||||||
|
@@ -1,23 +1,13 @@
|
|||||||
// Load the implementations that should be tested
|
// Load the implementations that should be tested
|
||||||
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
|
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
|
||||||
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
|
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
|
||||||
import 'rxjs/add/observable/of';
|
|
||||||
|
|
||||||
import { UploaderService } from '../uploader/uploader.service';
|
import { UploaderService } from '../uploader/uploader.service';
|
||||||
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
|
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
|
||||||
import { By } from '@angular/platform-browser';
|
import { By } from '@angular/platform-browser';
|
||||||
import { NumberPickerComponent } from './number-picker.component';
|
import { NumberPickerComponent } from './number-picker.component';
|
||||||
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
||||||
|
import { createTestComponent } from '../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('NumberPickerComponent test suite', () => {
|
describe('NumberPickerComponent test suite', () => {
|
||||||
|
|
||||||
|
@@ -44,16 +44,7 @@ import { EnumKeysPipe } from '../utils/enum-keys-pipe';
|
|||||||
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
|
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
|
||||||
|
|
||||||
import { GLOBAL_CONFIG, ENV_CONFIG } from '../../../config';
|
import { GLOBAL_CONFIG, ENV_CONFIG } from '../../../config';
|
||||||
|
import { createTestComponent } from '../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: { template: html }
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function expectPages(fixture: ComponentFixture<any>, pagesDef: string[]): void {
|
function expectPages(fixture: ComponentFixture<any>, pagesDef: string[]): void {
|
||||||
const de = fixture.debugElement.query(By.css('.pagination'));
|
const de = fixture.debugElement.query(By.css('.pagination'));
|
||||||
|
32
src/app/shared/testing/utils.ts
Normal file
32
src/app/shared/testing/utils.ts
Normal file
@@ -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 = <T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> => {
|
||||||
|
TestBed.overrideComponent(type, {
|
||||||
|
set: {template: html}
|
||||||
|
});
|
||||||
|
const fixture = TestBed.createComponent(type);
|
||||||
|
|
||||||
|
fixture.detectChanges();
|
||||||
|
return fixture as ComponentFixture<T>;
|
||||||
|
};
|
@@ -9,16 +9,7 @@ import { UploaderOptions } from './uploader-options.model';
|
|||||||
import { UploaderComponent } from './uploader.component';
|
import { UploaderComponent } from './uploader.component';
|
||||||
import { FileUploadModule } from 'ng2-file-upload';
|
import { FileUploadModule } from 'ng2-file-upload';
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
|
import { createTestComponent } from '../testing/utils';
|
||||||
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
|
|
||||||
TestBed.overrideComponent(type, {
|
|
||||||
set: {template: html}
|
|
||||||
});
|
|
||||||
const fixture = TestBed.createComponent(type);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
return fixture as ComponentFixture<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('Chips component', () => {
|
describe('Chips component', () => {
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user