[TLC-254] Service tests

This commit is contained in:
Kim Shepherd
2022-04-22 15:05:08 +12:00
parent fc037d79ca
commit d7afaf9cb8
3 changed files with 53 additions and 39 deletions

View File

@@ -1,52 +1,54 @@
import {inject, TestBed, waitForAsync} from '@angular/core/testing'; import {inject, TestBed, waitForAsync} from '@angular/core/testing';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { import {
DYNAMIC_FORM_CONTROL_TYPE_ARRAY, DynamicFormControlRelation,
DYNAMIC_FORM_CONTROL_TYPE_GROUP, DynamicFormControlMatcher,
DynamicFormControlEvent, DynamicFormControlRelation, DynamicFormValidationService, DynamicFormRelationService,
DynamicFormControlMatcher, DynamicFormRelationService, MATCH_VISIBLE,
DynamicInputModel, MATCH_VISIBLE, OR_OPERATOR, HIDDEN_MATCHER, DYNAMIC_MATCHERS OR_OPERATOR,
HIDDEN_MATCHER,
DISABLED_MATCHER,
REQUIRED_MATCHER,
} from '@ng-dynamic-forms/core'; } from '@ng-dynamic-forms/core';
import { import {
mockInputWithTypeBindModel, MockRelationModel, mockDcTypeInputModel mockInputWithTypeBindModel, MockRelationModel, mockDcTypeInputModel
} from '../../../mocks/form-models.mock'; } from '../../../mocks/form-models.mock';
import {DsDynamicTypeBindRelationService} from './ds-dynamic-type-bind-relation.service'; import {DsDynamicTypeBindRelationService} from './ds-dynamic-type-bind-relation.service';
import {FormFieldMetadataValueObject} from "../models/form-field-metadata-value.model"; import {FormFieldMetadataValueObject} from '../models/form-field-metadata-value.model';
import {FormControl, NG_ASYNC_VALIDATORS, NG_VALIDATORS, ReactiveFormsModule} from "@angular/forms"; import {FormControl, ReactiveFormsModule} from '@angular/forms';
import {FormBuilderService} from "../form-builder.service"; import {FormBuilderService} from '../form-builder.service';
import {getMockFormBuilderService} from "../../../mocks/form-builder-service.mock"; import {getMockFormBuilderService} from '../../../mocks/form-builder-service.mock';
import {DsDynamicFormComponent} from "./ds-dynamic-form.component"; import {Injector} from '@angular/core';
import {DsDynamicInputModel} from "./models/ds-dynamic-input.model";
import {FieldParser} from "../parsers/field-parser";
describe('DSDynamicTypeBindRelationService test suite', () => { describe('DSDynamicTypeBindRelationService test suite', () => {
let service: DsDynamicTypeBindRelationService; let service: DsDynamicTypeBindRelationService;
let dynamicFormRelationService: DynamicFormRelationService; let dynamicFormRelationService: DynamicFormRelationService;
let dynamicFormControlMatchers: DynamicFormControlMatcher[]; let dynamicFormControlMatchers: DynamicFormControlMatcher[];
let injector: Injector;
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ReactiveFormsModule], imports: [ReactiveFormsModule],
providers: [ providers: [
//{ provide: FormBuilderService, useValue: getMockFormBuilderService() },
{ provide: FormBuilderService, useValue: getMockFormBuilderService() }, { provide: FormBuilderService, useValue: getMockFormBuilderService() },
{ provide: DsDynamicTypeBindRelationService, useClass: DsDynamicTypeBindRelationService }, { provide: DsDynamicTypeBindRelationService, useClass: DsDynamicTypeBindRelationService },
{ provide: DynamicFormRelationService }, { provide: DynamicFormRelationService },
{ provide: Injector }
] ]
}).compileComponents().then(); }).compileComponents().then();
}); });
beforeEach(inject([DsDynamicTypeBindRelationService, DynamicFormRelationService], beforeEach(inject([DsDynamicTypeBindRelationService, DynamicFormRelationService],
(relationService: DsDynamicTypeBindRelationService, (relationService: DsDynamicTypeBindRelationService,
formRelationService: DynamicFormRelationService formRelationService: DynamicFormRelationService,
mockInjector: Injector,
) => { ) => {
service = relationService; service = relationService;
dynamicFormRelationService = formRelationService; dynamicFormRelationService = formRelationService;
dynamicFormControlMatchers = []; dynamicFormControlMatchers = [];
injector = mockInjector;
dynamicFormControlMatchers = [HIDDEN_MATCHER, REQUIRED_MATCHER, DISABLED_MATCHER];
})); }));
describe('Test getTypeBindValue method', () => { describe('Test getTypeBindValue method', () => {
@@ -77,12 +79,12 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
describe('Test getRelatedFormModel method', () => { describe('Test getRelatedFormModel method', () => {
it('Should get 0 related form models for simple type bind mock data', () => { it('Should get 0 related form models for simple type bind mock data', () => {
const testModel = mockInputWithTypeBindModel; const testModel = MockRelationModel;
const relatedModels = service.getRelatedFormModel(testModel); const relatedModels = service.getRelatedFormModel(testModel);
expect(relatedModels).toHaveSize(0); expect(relatedModels).toHaveSize(0);
}); });
it('Should get 1 related form models for mock relation model data', () => { it('Should get 1 related form models for mock relation model data', () => {
const testModel = MockRelationModel; const testModel = mockInputWithTypeBindModel;
testModel.typeBindRelations = getTypeBindRelations(['boundType']); testModel.typeBindRelations = getTypeBindRelations(['boundType']);
const relatedModels = service.getRelatedFormModel(testModel); const relatedModels = service.getRelatedFormModel(testModel);
expect(relatedModels).toHaveSize(1); expect(relatedModels).toHaveSize(1);
@@ -91,28 +93,45 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
describe('Test matchesCondition method', () => { describe('Test matchesCondition method', () => {
it('Should receive one subscription to dc.type type binding"', () => { it('Should receive one subscription to dc.type type binding"', () => {
const testModel = MockRelationModel; const testModel = mockInputWithTypeBindModel;
//testModel.typeBindRelations = getTypeBindRelations(['boundType']); testModel.typeBindRelations = getTypeBindRelations(['boundType']);
const relatedModels = service.getRelatedFormModel(testModel);
const dcTypeControl = new FormControl(); const dcTypeControl = new FormControl();
dcTypeControl.setValue('boundType'); dcTypeControl.setValue('boundType');
expect(service.subscribeRelations(testModel, dcTypeControl)).toHaveSize(1); expect(service.subscribeRelations(testModel, dcTypeControl)).toHaveSize(1);
}); });
it('TEST MTACHe"', () => { it('Expect hasMatch to be true (ie. this should be hidden)', () => {
const testModel = MockRelationModel; const testModel = mockInputWithTypeBindModel;
testModel.typeBindRelations = getTypeBindRelations(['boundType']); testModel.typeBindRelations = getTypeBindRelations(['boundType']);
const relatedModels = service.getRelatedFormModel(testModel);
const dcTypeControl = new FormControl(); const dcTypeControl = new FormControl();
dcTypeControl.setValue('boundType'); dcTypeControl.setValue('boundType');
testModel.typeBindRelations[0].when[0].value = 'asdfaf'; testModel.typeBindRelations[0].when[0].value = 'anotherType';
const relation = dynamicFormRelationService.findRelationByMatcher((testModel as any).typeBindRelations, HIDDEN_MATCHER); const relation = dynamicFormRelationService.findRelationByMatcher((testModel as any).typeBindRelations, HIDDEN_MATCHER);
// console.dir(relation); const matcher = HIDDEN_MATCHER;
if (relation !== undefined) {
const hasMatch = service.matchesCondition(relation, matcher);
matcher.onChange(hasMatch, testModel, dcTypeControl, injector);
expect(hasMatch).toBeTruthy();
}
}); });
it('Expect hasMatch to be false (ie. this should NOT be hidden)', () => {
const testModel = mockInputWithTypeBindModel;
testModel.typeBindRelations = getTypeBindRelations(['boundType']);
const dcTypeControl = new FormControl();
dcTypeControl.setValue('boundType');
testModel.typeBindRelations[0].when[0].value = 'boundType';
const relation = dynamicFormRelationService.findRelationByMatcher((testModel as any).typeBindRelations, HIDDEN_MATCHER);
const matcher = HIDDEN_MATCHER;
if (relation !== undefined) {
const hasMatch = service.matchesCondition(relation, matcher);
matcher.onChange(hasMatch, testModel, dcTypeControl, injector);
expect(hasMatch).toBeFalsy();
}
});
}); });
}); });
function getTypeBindRelations(configuredTypeBindValues: string[]): DynamicFormControlRelation[] { function getTypeBindRelations(configuredTypeBindValues: string[]): DynamicFormControlRelation[] {

View File

@@ -188,8 +188,6 @@ export class DsDynamicTypeBindRelationService {
const relatedModels = this.getRelatedFormModel(model); const relatedModels = this.getRelatedFormModel(model);
const subscriptions: Subscription[] = []; const subscriptions: Subscription[] = [];
console.dir(relatedModels);
Object.values(relatedModels).forEach((relatedModel: any) => { Object.values(relatedModels).forEach((relatedModel: any) => {
if (hasValue(relatedModel)) { if (hasValue(relatedModel)) {
@@ -201,9 +199,6 @@ export class DsDynamicTypeBindRelationService {
); );
// Build up the subscriptions to watch for changes; // Build up the subscriptions to watch for changes;
// I still don't fully understand what is happening here, or the triggers in various form usage that
// cause which / what to fire change events, why the matcher has onChange() instead of a field value or
// form model, etc.
if (hasValue(this.dynamicMatchers) || true) { if (hasValue(this.dynamicMatchers) || true) {
subscriptions.push(valueChanges.subscribe(() => { subscriptions.push(valueChanges.subscribe(() => {
// Iterate each matcher // Iterate each matcher

View File

@@ -42,18 +42,18 @@ export function getMockFormBuilderService(): FormBuilderService {
isRelationGroup: true, isRelationGroup: true,
hasArrayGroupValue: true, hasArrayGroupValue: true,
getTypeBindModel: new DsDynamicInputModel({ getTypeBindModel: new DsDynamicInputModel({
name: 'testWithTypeBind', name: 'dc.type',
id: 'testWithTypeBind', id: 'dc_type',
readOnly: false, readOnly: false,
disabled: false, disabled: false,
repeatable: false, repeatable: false,
value: { value: {
value: 'testWithTypeBind', value: 'boundType',
display: 'testWithTypeBind', display: 'Bound Type',
authority: 'bound-auth-key' authority: 'bound-auth-key'
}, },
submissionId: '1234', submissionId: '1234',
metadataFields: [], metadataFields: ['dc.type'],
hasSelectableMetadata: false, hasSelectableMetadata: false,
typeBindRelations: [ typeBindRelations: [
{match: 'VISIBLE', operator: 'OR', when: [{'id': 'dc.type', 'value': 'boundType'}]} {match: 'VISIBLE', operator: 'OR', when: [{'id': 'dc.type', 'value': 'boundType'}]}