From d7afaf9cb865dace9c542f6fe149844418fb2f92 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Fri, 22 Apr 2022 15:05:08 +1200 Subject: [PATCH] [TLC-254] Service tests --- ...dynamic-type-bind-relation.service.spec.ts | 77 ++++++++++++------- .../ds-dynamic-type-bind-relation.service.ts | 5 -- .../shared/mocks/form-builder-service.mock.ts | 10 +-- 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.spec.ts index af00dcb7ac..c65e3c6574 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.spec.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.spec.ts @@ -1,52 +1,54 @@ import {inject, TestBed, waitForAsync} from '@angular/core/testing'; -import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; import { - DYNAMIC_FORM_CONTROL_TYPE_ARRAY, - DYNAMIC_FORM_CONTROL_TYPE_GROUP, - DynamicFormControlEvent, DynamicFormControlRelation, DynamicFormValidationService, - DynamicFormControlMatcher, DynamicFormRelationService, - DynamicInputModel, MATCH_VISIBLE, OR_OPERATOR, HIDDEN_MATCHER, DYNAMIC_MATCHERS + DynamicFormControlRelation, + DynamicFormControlMatcher, + DynamicFormRelationService, + MATCH_VISIBLE, + OR_OPERATOR, + HIDDEN_MATCHER, + DISABLED_MATCHER, + REQUIRED_MATCHER, } from '@ng-dynamic-forms/core'; - - import { mockInputWithTypeBindModel, MockRelationModel, mockDcTypeInputModel } from '../../../mocks/form-models.mock'; import {DsDynamicTypeBindRelationService} from './ds-dynamic-type-bind-relation.service'; -import {FormFieldMetadataValueObject} from "../models/form-field-metadata-value.model"; -import {FormControl, NG_ASYNC_VALIDATORS, NG_VALIDATORS, ReactiveFormsModule} from "@angular/forms"; -import {FormBuilderService} from "../form-builder.service"; -import {getMockFormBuilderService} from "../../../mocks/form-builder-service.mock"; -import {DsDynamicFormComponent} from "./ds-dynamic-form.component"; -import {DsDynamicInputModel} from "./models/ds-dynamic-input.model"; -import {FieldParser} from "../parsers/field-parser"; +import {FormFieldMetadataValueObject} from '../models/form-field-metadata-value.model'; +import {FormControl, ReactiveFormsModule} from '@angular/forms'; +import {FormBuilderService} from '../form-builder.service'; +import {getMockFormBuilderService} from '../../../mocks/form-builder-service.mock'; +import {Injector} from '@angular/core'; describe('DSDynamicTypeBindRelationService test suite', () => { let service: DsDynamicTypeBindRelationService; let dynamicFormRelationService: DynamicFormRelationService; let dynamicFormControlMatchers: DynamicFormControlMatcher[]; + let injector: Injector; beforeEach(() => { TestBed.configureTestingModule({ imports: [ReactiveFormsModule], providers: [ - //{ provide: FormBuilderService, useValue: getMockFormBuilderService() }, { provide: FormBuilderService, useValue: getMockFormBuilderService() }, { provide: DsDynamicTypeBindRelationService, useClass: DsDynamicTypeBindRelationService }, { provide: DynamicFormRelationService }, + { provide: Injector } ] }).compileComponents().then(); }); beforeEach(inject([DsDynamicTypeBindRelationService, DynamicFormRelationService], (relationService: DsDynamicTypeBindRelationService, - formRelationService: DynamicFormRelationService + formRelationService: DynamicFormRelationService, + mockInjector: Injector, ) => { service = relationService; dynamicFormRelationService = formRelationService; dynamicFormControlMatchers = []; + injector = mockInjector; + dynamicFormControlMatchers = [HIDDEN_MATCHER, REQUIRED_MATCHER, DISABLED_MATCHER]; })); describe('Test getTypeBindValue method', () => { @@ -77,12 +79,12 @@ describe('DSDynamicTypeBindRelationService test suite', () => { describe('Test getRelatedFormModel method', () => { it('Should get 0 related form models for simple type bind mock data', () => { - const testModel = mockInputWithTypeBindModel; + const testModel = MockRelationModel; const relatedModels = service.getRelatedFormModel(testModel); expect(relatedModels).toHaveSize(0); }); it('Should get 1 related form models for mock relation model data', () => { - const testModel = MockRelationModel; + const testModel = mockInputWithTypeBindModel; testModel.typeBindRelations = getTypeBindRelations(['boundType']); const relatedModels = service.getRelatedFormModel(testModel); expect(relatedModels).toHaveSize(1); @@ -91,28 +93,45 @@ describe('DSDynamicTypeBindRelationService test suite', () => { describe('Test matchesCondition method', () => { it('Should receive one subscription to dc.type type binding"', () => { - const testModel = MockRelationModel; - //testModel.typeBindRelations = getTypeBindRelations(['boundType']); - const relatedModels = service.getRelatedFormModel(testModel); + const testModel = mockInputWithTypeBindModel; + testModel.typeBindRelations = getTypeBindRelations(['boundType']); const dcTypeControl = new FormControl(); dcTypeControl.setValue('boundType'); expect(service.subscribeRelations(testModel, dcTypeControl)).toHaveSize(1); }); - it('TEST MTACHe"', () => { - const testModel = MockRelationModel; + it('Expect hasMatch to be true (ie. this should be hidden)', () => { + const testModel = mockInputWithTypeBindModel; testModel.typeBindRelations = getTypeBindRelations(['boundType']); - const relatedModels = service.getRelatedFormModel(testModel); const dcTypeControl = new FormControl(); 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); - // 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[] { diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.ts index 959952e6a6..279f47d9e0 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.ts @@ -188,8 +188,6 @@ export class DsDynamicTypeBindRelationService { const relatedModels = this.getRelatedFormModel(model); const subscriptions: Subscription[] = []; - console.dir(relatedModels); - Object.values(relatedModels).forEach((relatedModel: any) => { if (hasValue(relatedModel)) { @@ -201,9 +199,6 @@ export class DsDynamicTypeBindRelationService { ); // 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) { subscriptions.push(valueChanges.subscribe(() => { // Iterate each matcher diff --git a/src/app/shared/mocks/form-builder-service.mock.ts b/src/app/shared/mocks/form-builder-service.mock.ts index 025d5bd6ba..e6584b14cc 100644 --- a/src/app/shared/mocks/form-builder-service.mock.ts +++ b/src/app/shared/mocks/form-builder-service.mock.ts @@ -42,18 +42,18 @@ export function getMockFormBuilderService(): FormBuilderService { isRelationGroup: true, hasArrayGroupValue: true, getTypeBindModel: new DsDynamicInputModel({ - name: 'testWithTypeBind', - id: 'testWithTypeBind', + name: 'dc.type', + id: 'dc_type', readOnly: false, disabled: false, repeatable: false, value: { - value: 'testWithTypeBind', - display: 'testWithTypeBind', + value: 'boundType', + display: 'Bound Type', authority: 'bound-auth-key' }, submissionId: '1234', - metadataFields: [], + metadataFields: ['dc.type'], hasSelectableMetadata: false, typeBindRelations: [ {match: 'VISIBLE', operator: 'OR', when: [{'id': 'dc.type', 'value': 'boundType'}]}