Added more tests

This commit is contained in:
Giuseppe Digilio
2018-06-22 15:13:49 +02:00
parent 40ccbdf653
commit e6c68c9396
13 changed files with 325 additions and 45 deletions

View File

@@ -0,0 +1,94 @@
import { cold, getTestScheduler } from 'jasmine-marbles';
import { TestScheduler } from 'rxjs/Rx';
import { getMockRequestService } from '../../shared/mocks/mock-request.service';
import { ResponseCacheService } from '../cache/response-cache.service';
import { RequestService } from '../data/request.service';
import { IntegrationRequest } from '../data/request.models';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service-stub';
import { IntegrationService } from './integration.service';
import { IntegrationSearchOptions } from './models/integration-options.model';
const LINK_NAME = 'authorities';
const BROWSE = 'entries';
class TestService extends IntegrationService {
protected linkPath = LINK_NAME;
protected browseEndpoint = BROWSE;
constructor(
protected responseCache: ResponseCacheService,
protected requestService: RequestService,
protected halService: HALEndpointService) {
super();
}
}
describe('IntegrationService', () => {
let scheduler: TestScheduler;
let service: TestService;
let responseCache: ResponseCacheService;
let requestService: RequestService;
let halService: any;
let findOptions: IntegrationSearchOptions;
const name = 'type';
const metadata = 'dc.type';
const query = '';
const uuid = 'd9d30c0c-69b7-4369-8397-ca67c888974d';
const integrationEndpoint = 'https://rest.api/integration';
const serviceEndpoint = `${integrationEndpoint}/${LINK_NAME}`;
const entriesEndpoint = `${serviceEndpoint}/${name}/entries?query=${query}&metadata=${metadata}&uuid=${uuid}`;
findOptions = new IntegrationSearchOptions(uuid, name, metadata);
function initMockResponseCacheService(isSuccessful: boolean): ResponseCacheService {
return jasmine.createSpyObj('responseCache', {
get: cold('c-', {
c: {response: {isSuccessful}}
})
});
}
function initTestService(): TestService {
return new TestService(
responseCache,
requestService,
halService
);
}
beforeEach(() => {
responseCache = initMockResponseCacheService(true);
requestService = getMockRequestService();
scheduler = getTestScheduler();
halService = new HALEndpointServiceStub(integrationEndpoint);
findOptions = new IntegrationSearchOptions(uuid, name, metadata, query);
service = initTestService();
});
describe('getEntriesByName', () => {
it('should configure a new IntegrationRequest', () => {
const expected = new IntegrationRequest(requestService.generateRequestId(), entriesEndpoint);
scheduler.schedule(() => service.getEntriesByName(findOptions).subscribe());
scheduler.flush();
expect(requestService.configure).toHaveBeenCalledWith(expected);
});
});
// describe('getConfigBySearch', () => {
//
// it('should configure a new ConfigRequest', () => {
// findOptions.uuid = uuid;
// const expected = new ConfigRequest(requestService.generateRequestId(), searchEndpoint);
// scheduler.schedule(() => service.getConfigBySearch(findOptions).subscribe());
// scheduler.flush();
//
// expect(requestService.configure).toHaveBeenCalledWith(expected);
// });
// });
});

View File

@@ -1,6 +1,6 @@
// 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 { async, ComponentFixture, fakeAsync, inject, TestBed, tick, } from '@angular/core/testing';
import 'rxjs/add/observable/of';
import { Chips } from './models/chips.model';
@@ -8,6 +8,9 @@ import { UploaderService } from '../uploader/uploader.service';
import { ChipsComponent } from './chips.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { SortablejsModule } from 'angular-sortablejs';
import { By } from '@angular/platform-browser';
import { PaginationComponent } from '../pagination/pagination.component';
import { TruncatableComponent } from '../truncatable/truncatable.component';
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
TestBed.overrideComponent(type, {
@@ -22,8 +25,11 @@ function createTestComponent<T>(html: string, type: { new(...args: any[]): T }):
describe('Chips component', () => {
let testComp: TestComponent;
let chipsComp: ChipsComponent;
let testFixture: ComponentFixture<TestComponent>;
let chipsFixture: ComponentFixture<ChipsComponent>;
let html;
let chips: Chips;
// async beforeEach
beforeEach(async(() => {
@@ -53,7 +59,7 @@ describe('Chips component', () => {
<ds-chips
*ngIf="chips.hasItems()"
[chips]="chips"
[editable]="true"
[editable]="editable"
(selected)="onChipSelected($event)"></ds-chips>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
@@ -61,10 +67,68 @@ describe('Chips component', () => {
});
it('should create Chips Component', inject([ChipsComponent], (app: ChipsComponent) => {
expect(app).toBeDefined();
}));
beforeEach(() => {
chips = new Chips(['a', 'b', 'c']);
chipsFixture = TestBed.createComponent(ChipsComponent);
chipsComp = chipsFixture.componentInstance; // TruncatableComponent test instance
chipsComp.editable = true;
chipsComp.chips = chips;
chipsFixture.detectChanges();
});
afterEach(() => {
chipsFixture.destroy();
chipsComp = null;
});
it('should set edit mode when a chip item is selected', fakeAsync(() => {
spyOn(chipsComp.selected, 'emit');
chipsComp.chipsSelected(new Event('click'), 1);
chipsFixture.detectChanges();
tick();
const item = chipsComp.chips.getChipByIndex(1);
expect(item.editMode).toBe(true);
expect(chipsComp.selected.emit).toHaveBeenCalledWith(1);
}));
it('should not set edit mode when a chip item is selected and editable is false', fakeAsync(() => {
chipsComp.editable = false;
spyOn(chipsComp.selected, 'emit');
chipsComp.chipsSelected(new Event('click'), 1);
chipsFixture.detectChanges();
tick();
const item = chipsComp.chips.getChipByIndex(1);
expect(item.editMode).toBe(false);
expect(chipsComp.selected.emit).not.toHaveBeenCalledWith(1);
}));
it('should emit when a chip item is removed and editable is true', fakeAsync(() => {
spyOn(chipsComp.chips, 'remove');
const item = chipsComp.chips.getChipByIndex(1);
chipsComp.removeChips(new Event('click'), 1);
chipsFixture.detectChanges();
tick();
expect(chipsComp.chips.remove).toHaveBeenCalledWith(item);
}));
// it('should chipsSelected', inject([ChipsComponent], (app: ChipsComponent) => {
// app.chipsSelected(new Event('click'), 1);
// expect(app).toBeDefined();
// }));
});
// declare a test component
@@ -74,6 +138,6 @@ describe('Chips component', () => {
})
class TestComponent {
public chips = new Chips([]);
public chips = new Chips(['a', 'b', 'c']);
public editable = true;
}

View File

@@ -16,7 +16,7 @@ import { UploaderService } from '../uploader/uploader.service';
export class ChipsComponent implements OnChanges {
@Input() chips: Chips;
@Input() wrapperClass: string;
@Input() editable: boolean;
@Input() editable = true;
@Output() selected: EventEmitter<number> = new EventEmitter<number>();
@Output() remove: EventEmitter<number> = new EventEmitter<number>();
@@ -36,12 +36,6 @@ export class ChipsComponent implements OnChanges {
};
}
ngOnInit() {
if (!this.editable) {
this.editable = false;
}
}
ngOnChanges(changes: SimpleChanges) {
if (changes.chips && !changes.chips.isFirstChange()) {
this.chips = changes.chips.currentValue;

View File

@@ -2,6 +2,7 @@ import { DynamicFormControlLayout, DynamicFormGroupModel, DynamicFormGroupModelC
import { isNotEmpty } from '../../../../empty.util';
import { DsDynamicInputModel } from './ds-dynamic-input.model';
import { AuthorityValueModel } from '../../../../../core/integration/models/authority-value.model';
import { FormFieldMetadataValueObject } from '../../models/form-field-metadata-value.model';
export const CONCAT_GROUP_SUFFIX = '_CONCAT_GROUP';
export const CONCAT_FIRST_INPUT_SUFFIX = '_CONCAT_FIRST_INPUT';
@@ -27,13 +28,13 @@ export class DynamicConcatModel extends DynamicFormGroupModel {
const firstValue = (this.get(0) as DsDynamicInputModel).value;
const secondValue = (this.get(1) as DsDynamicInputModel).value;
if (isNotEmpty(firstValue) && isNotEmpty(secondValue)) {
return firstValue + this.separator + secondValue;
return new FormFieldMetadataValueObject(firstValue + this.separator + secondValue);
} else {
return null
return null;
}
}
set value(value: string | AuthorityValueModel) {
set value(value: string | FormFieldMetadataValueObject) {
let values;
if (typeof value === 'string') {
values = value ? value.split(this.separator) : [null, null];

View File

@@ -71,11 +71,11 @@ export class ConcatFieldParser extends FieldParser {
// Init values
if (isNotEmpty(fieldValue)) {
const values = fieldValue.split(this.separator);
const values = fieldValue.value.split(this.separator);
if (values.length > 1) {
input1ModelConfig.value = values[0];
input2ModelConfig.value = values[1];
input1ModelConfig.value = values[0].trim();
input2ModelConfig.value = values[1].trim();
}
}

View File

@@ -3,11 +3,12 @@ import { DynamicConcatModel } from '../ds-dynamic-form-ui/models/ds-dynamic-conc
import { SeriesFieldParser } from './series-field-parser';
import { DateFieldParser } from './date-field-parser';
import { DynamicDsDatePickerModel } from '../ds-dynamic-form-ui/models/date-picker/date-picker.model';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
describe('DateFieldParser test suite', () => {
let field: FormFieldModel;
let initFormValues: any = {};
const initFormValues = {};
const readOnly = false;
beforeEach(() => {
@@ -44,4 +45,16 @@ describe('DateFieldParser test suite', () => {
expect(fieldModel instanceof DynamicDsDatePickerModel).toBe(true);
});
it('should set init value properly', () => {
initFormValues = {
date: [new FormFieldMetadataValueObject('1983-11-18')],
};
const expectedValue = '1983-11-18';
const parser = new DateFieldParser(field, initFormValues, readOnly);
const fieldModel = parser.parse();
expect(fieldModel.value).toEqual(expectedValue);
});
});

View File

@@ -8,13 +8,13 @@ import { FormFieldMetadataValueObject } from '../models/form-field-metadata-valu
export class DateFieldParser extends FieldParser {
public modelFactory(fieldValue: FormFieldMetadataValueObject): any {
let malformedDate = false;
const inputDateModelConfig: DynamicDatePickerModelConfig = this.initModel();
inputDateModelConfig.toggleIcon = 'fa fa-calendar';
this.setValues(inputDateModelConfig as any, fieldValue);
// Init Data and validity check
if (isNotEmpty(inputDateModelConfig.value)) {
let malformedData = false;
const value = inputDateModelConfig.value.toString();
if (value.length >= 4) {
const valuesArray = value.split(DS_DATE_PICKER_SEPARATOR);
@@ -22,23 +22,15 @@ export class DateFieldParser extends FieldParser {
for (let i = 0; i < valuesArray.length; i++) {
const len = i === 0 ? 4 : 2;
if (valuesArray[i].length !== len) {
malformedData = true;
malformedDate = true;
}
}
}
if (malformedData) {
// TODO Set error message
// const errorMessage = 'The stored date is not compliant';
// dateModel.validators = Object.assign({}, dateModel.validators, {malformedDate: null});
// dateModel.errorMessages = Object.assign({}, dateModel.errorMessages, {malformedDate: errorMessage});
// this.formService.addErrorToField(this.group.get(this.model.id), this.model, errorMessage)
}
}
}
const dateModel = new DynamicDsDatePickerModel(inputDateModelConfig);
dateModel.malformedDate = malformedDate;
return dateModel;
}
}

View File

@@ -185,7 +185,7 @@ export abstract class FieldParser {
controlModel.placeholder = this.configData.label;
if (this.configData.mandatory && setErrors) {
this.setErrors(controlModel);
this.markAsRequired(controlModel);
}
// Available Languages
@@ -196,7 +196,7 @@ export abstract class FieldParser {
return controlModel;
}
protected setErrors(controlModel) {
protected markAsRequired(controlModel) {
controlModel.required = true;
controlModel.validators = Object.assign({}, controlModel.validators, {required: null});
controlModel.errorMessages = Object.assign(

View File

@@ -1,18 +1,21 @@
import { FormFieldModel } from '../models/form-field.model';
import { NameFieldParser } from './name-field-parser';
import { DynamicConcatModel } from '../ds-dynamic-form-ui/models/ds-dynamic-concat.model';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
describe('NameFieldParser test suite', () => {
let field1: FormFieldModel;
let field2: FormFieldModel;
let field3: FormFieldModel;
let initFormValues: any = {};
const initFormValues = {};
const readOnly = false;
beforeEach(() => {
field1 = {
input: {type: 'name'},
input: {
type: 'name'
},
label: 'Name',
mandatory: 'false',
repeatable: false,
@@ -82,4 +85,17 @@ describe('NameFieldParser test suite', () => {
expect((fieldModel as DynamicConcatModel).separator).toBe(', ');
});
it('should set init value properly', () => {
initFormValues = {
name: [new FormFieldMetadataValueObject('test, name')],
};
const expectedValue = new FormFieldMetadataValueObject('test, name');
const parser = new NameFieldParser(field1, initFormValues, readOnly);
const fieldModel = parser.parse();
expect(fieldModel.value).toEqual(expectedValue);
});
});

View File

@@ -1,11 +1,12 @@
import { FormFieldModel } from '../models/form-field.model';
import { DynamicConcatModel } from '../ds-dynamic-form-ui/models/ds-dynamic-concat.model';
import { SeriesFieldParser } from './series-field-parser';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
describe('SeriesFieldParser test suite', () => {
let field: FormFieldModel;
let initFormValues: any = {};
const initFormValues = {};
const readOnly = false;
beforeEach(() => {
@@ -47,4 +48,17 @@ describe('SeriesFieldParser test suite', () => {
expect((fieldModel as DynamicConcatModel).separator).toBe('; ');
});
it('should set init value properly', () => {
initFormValues = {
series: [new FormFieldMetadataValueObject('test; series')],
};
const expectedValue = new FormFieldMetadataValueObject('test; series');
const parser = new SeriesFieldParser(field, initFormValues, readOnly);
const fieldModel = parser.parse();
expect(fieldModel.value).toEqual(expectedValue);
});
});

View File

@@ -157,9 +157,8 @@ describe('FormService test suite', () => {
model = builderService.findById('title', formModel);
service.addErrorToField(control, model, 'error.required');
errorKeys = Object.keys(control.errors);
service.removeErrorFromField(control, model, errorKeys[0]);
service.removeErrorFromField(control, model, 'error.required');
expect(errorKeys.length).toBe(1);

View File

@@ -0,0 +1,87 @@
import { deleteProperty, difference, isObjectEmpty } from './object.util';
describe('Object Utils', () => {
let object: any = {};
let anotherObject: any = {};
let objectExpected: any = {};
describe('deleteProperty', () => {
it('should return object without property \'a\'', () => {
object = {a: 'a', b: 'b'};
objectExpected = {b: 'b'};
expect(deleteProperty(object, 'a')).toEqual(objectExpected);
});
it('should return same object', () => {
object = {a: 'a', b: 'b'};
expect(deleteProperty(object, 'c')).toEqual(object);
});
});
describe('isObjectEmpty', () => {
it('should return true when object is empty', () => {
object = {};
expect(isObjectEmpty(object)).toBe(true);
});
it('should return true when object has a null property', () => {
object = {a: null};
expect(isObjectEmpty(object)).toBe(true);
});
it('should return true when object property has an empty array as value', () => {
object = {a: []};
expect(isObjectEmpty(object)).toBe(true);
});
it('should return true when object property has an empty object as value', () => {
object = {a: {}};
expect(isObjectEmpty(object)).toBe(true);
});
it('should return false when object is not empty', () => {
object = {a: 'a', b: 'b'};
expect(isObjectEmpty(object)).toBe(false);
});
it('should return false when object has at least a valued property', () => {
object = {a: [], b: 'b'};
expect(isObjectEmpty(object)).toBe(false);
});
});
describe('difference', () => {
it('should return an empty object', () => {
object = {};
anotherObject = {};
objectExpected = {};
expect(difference(object, anotherObject)).toEqual(objectExpected);
});
it('should return object properties that are not included in the base object', () => {
object = {a: 'a', b: 'b'};
anotherObject = {a: 'a'};
objectExpected = {b: 'b'};
expect(difference(object, anotherObject)).toEqual(objectExpected);
});
it('should not return object properties that are included only in the base object', () => {
object = {a: 'a'};
anotherObject = {a: 'a', b: 'b'};
objectExpected = {};
expect(difference(object, anotherObject)).toEqual(objectExpected);
});
it('should not return empty object properties that are not included in the base object', () => {
object = {a: 'a', b: {}};
anotherObject = {a: 'a'};
objectExpected = {};
expect(difference(object, anotherObject)).toEqual(objectExpected);
});
});
});

View File

@@ -10,14 +10,13 @@ export function deleteProperty(object, key): object {
}
/**
* Returns true if the passed value is null or undefined.
* hasNoValue(); // true
* hasNoValue(null); // true
* hasNoValue(undefined); // true
* hasNoValue(''); // false
* hasNoValue({}); // false
* hasNoValue([]); // false
* hasNoValue(function() {}); // false
* Returns true if the passed object is empty or has only empty property.
* isObjectEmpty({}); // true
* isObjectEmpty({a: null}); // true
* isObjectEmpty({a: []}); // true
* isObjectEmpty({a: [], b: {}); // true
* isObjectEmpty({a: 'a', b: 'b'}); // false
* isObjectEmpty({a: [], b: 'b'}); // false
*/
export function isObjectEmpty(obj: any): boolean {
const objectType = typeof obj;
@@ -37,6 +36,13 @@ export function isObjectEmpty(obj: any): boolean {
}
}
/**
* Returns diff from the base object.
* difference({}, {}); // {}
* difference({a: 'a', b: 'b'}, {a: 'a'}); // {b: 'b'}
* difference({a: 'a', b: {}}, {a: 'a'}); // {}
* difference({a: 'a'}, {a: 'a', b: 'b'}); // {}
*/
export function difference(object, base) {
const changes = (o, b) => {
return transform(o, (result, value, key) => {