-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for dynamic list and option data based on related input value
- Loading branch information
1 parent
360ddb6
commit d9b641e
Showing
16 changed files
with
384 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
projects/ng-dynamic-forms/core/src/lib/model/misc/dynamic-form-control-data.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {Observable} from 'rxjs'; | ||
import {DynamicFormOptionConfig} from '../dynamic-option-control.model'; | ||
|
||
export interface DynamicFormControlDataRelation { | ||
rootPath?: string; | ||
id?: string; | ||
} | ||
|
||
export interface DynamicFormControlDataConfig { | ||
relation: DynamicFormControlDataRelation; | ||
service: any; | ||
} | ||
|
||
export interface DynamicFormControlListDataProvider<T> { | ||
fetchList(value: string): Observable<T[]>; | ||
} | ||
|
||
export interface DynamicFormControlOptionDataProvider<T> { | ||
fetchOptions(value: string): Observable<DynamicFormOptionConfig<T>[]>; | ||
} |
220 changes: 220 additions & 0 deletions
220
projects/ng-dynamic-forms/core/src/lib/service/dynamic-form-data.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
import {TestBed, inject, tick, fakeAsync} from "@angular/core/testing"; | ||
import {FormGroup, ReactiveFormsModule} from "@angular/forms"; | ||
import { DynamicFormService } from "./dynamic-form.service"; | ||
import { DynamicSelectModel } from "../model/select/dynamic-select.model"; | ||
import { DynamicRadioGroupModel } from "../model/radio/dynamic-radio-group.model"; | ||
import {DynamicFormDataService} from './dynamic-form-data.service'; | ||
import { | ||
DynamicFormControlListDataProvider, | ||
DynamicFormControlOptionDataProvider | ||
} from '../model/misc/dynamic-form-control-data.model'; | ||
import {Observable, of} from 'rxjs'; | ||
import {Injectable} from '@angular/core'; | ||
import {DynamicInputModel} from '../model/input/dynamic-input.model'; | ||
import {DynamicFormGroupModel} from '../model/form-group/dynamic-form-group.model'; | ||
import {DynamicFormOptionConfig} from '../model/dynamic-option-control.model'; | ||
|
||
@Injectable() | ||
class TestProvider implements DynamicFormControlListDataProvider<string>, DynamicFormControlOptionDataProvider<string> { | ||
fetchList(value: string): Observable<string[]> { | ||
return of(['test']); | ||
} | ||
|
||
fetchOptions(value: string): Observable<DynamicFormOptionConfig<string>[]> { | ||
return of([{ | ||
label: 'Test', | ||
value: 'test' | ||
}]); | ||
} | ||
} | ||
|
||
@Injectable() | ||
class InvalidTestProvider { | ||
|
||
} | ||
|
||
describe("DynamicFormDataService test suite", () => { | ||
|
||
let service: DynamicFormDataService, | ||
group: FormGroup, | ||
model: DynamicInputModel = new DynamicInputModel({ | ||
id: "testInput2", | ||
list: ['item-1', 'item-2', 'item-3'], | ||
value: "item-1", | ||
dataProvider: { | ||
relation: { | ||
id: 'testInput' | ||
}, | ||
service: TestProvider, | ||
} | ||
}), | ||
select: DynamicSelectModel<any> = new DynamicSelectModel({ | ||
id: "testSelect", | ||
options: [{value: "option-1"}, {value: "option-2"}, {value: "option-3"}], | ||
value: "option-1", | ||
dataProvider: { | ||
relation: { | ||
id: 'testInput' | ||
}, | ||
service: TestProvider, | ||
} | ||
}), | ||
radio: DynamicRadioGroupModel<any> = new DynamicRadioGroupModel({ | ||
id: "testRadioGroup", | ||
options: [{value: "option-1"}, {value: "option-2"}, {value: "option-3"}], | ||
value: "option-1", | ||
dataProvider: { | ||
relation: { | ||
id: 'testInput' | ||
}, | ||
service: TestProvider | ||
} | ||
}), | ||
invalidListProvider: DynamicInputModel = new DynamicInputModel({ | ||
id: "invalidListProvider", | ||
list: ['item-1', 'item-2', 'item-3'], | ||
value: "item-1", | ||
dataProvider: { | ||
relation: { | ||
id: 'testInput' | ||
}, | ||
service: InvalidTestProvider, | ||
} | ||
}), | ||
invalidOptionProvider: DynamicSelectModel<any> = new DynamicSelectModel({ | ||
id: "invalidOptionProvider", | ||
options: [{value: "option-1"}, {value: "option-2"}, {value: "option-3"}], | ||
value: "option-1", | ||
dataProvider: { | ||
relation: { | ||
id: 'testInput' | ||
}, | ||
service: InvalidTestProvider, | ||
} | ||
}), | ||
referenceInvalidControl: DynamicInputModel = new DynamicInputModel({ | ||
id: "referenceInvalidControl", | ||
list: ['item-1', 'item-2', 'item-3'], | ||
value: "item-1", | ||
dataProvider: { | ||
relation: { | ||
id: 'not-an-id' | ||
}, | ||
service: TestProvider, | ||
} | ||
}), | ||
groupModel = new DynamicFormGroupModel({ | ||
id: 'test', | ||
group: [ | ||
new DynamicRadioGroupModel({ | ||
id: "testRootRadioGroup", | ||
options: [{value: "option-1"}, {value: "option-2"}, {value: "option-3"}], | ||
value: "option-1", | ||
dataProvider: { | ||
relation: { | ||
id: 'testInput' | ||
}, | ||
service: TestProvider | ||
} | ||
}), | ||
] | ||
}), | ||
groupInputTest = new DynamicInputModel({ | ||
id: "testInput", | ||
dataProvider: { | ||
relation: { | ||
rootPath: 'test.testRootRadioGroup' | ||
}, | ||
service: TestProvider, | ||
} | ||
}) | ||
; | ||
|
||
beforeEach(() => { | ||
|
||
TestBed.configureTestingModule({ | ||
imports: [ReactiveFormsModule], | ||
providers: [DynamicFormDataService, TestProvider, InvalidTestProvider] | ||
}); | ||
}); | ||
|
||
beforeEach(inject([DynamicFormDataService, DynamicFormService], | ||
(dataService: DynamicFormDataService, formService: DynamicFormService) => { | ||
|
||
service = dataService; | ||
|
||
group = formService.createFormGroup([ | ||
new DynamicInputModel({id: "testInput"}), | ||
model, | ||
select, | ||
radio, | ||
invalidListProvider, | ||
invalidOptionProvider, | ||
groupModel | ||
]); | ||
})); | ||
|
||
it("should get related form control correctly", () => { | ||
const compareControl = group.get('testInput'); | ||
const relatedFormControl = service.getRelatedFormControl(model, group); | ||
|
||
expect(relatedFormControl).toBe(compareControl); | ||
}); | ||
|
||
it("should get data from provider on related input value change", fakeAsync(() => { | ||
const triggerControl = group.get('testInput'); | ||
|
||
service.connectDynamicFormControls(model, group); | ||
triggerControl.setValue('newVal'); | ||
tick(401); | ||
model.list$.subscribe((list) => expect(list[0]).toBe('test')); | ||
})); | ||
|
||
it("should get data from provider on related select option value change", fakeAsync(() => { | ||
const triggerControl = group.get('testInput'); | ||
|
||
service.connectDynamicFormControls(select, group); | ||
triggerControl.setValue('newVal'); | ||
tick(401); | ||
select.options$.subscribe((options) => expect(options[0].value).toBe('test')); | ||
})); | ||
|
||
it("should get data from provider on related radio option value change", fakeAsync(() => { | ||
const triggerControl = group.get('testInput'); | ||
|
||
service.connectDynamicFormControls(radio, group); | ||
triggerControl.setValue('newVal'); | ||
tick(401); | ||
radio.options$.subscribe((options) => expect(options[0].value).toBe('test')); | ||
})); | ||
|
||
it("should not fail with invalid provider but receive warning with missing list data provider.", fakeAsync(() => { | ||
const triggerControl = group.get('testInput'); | ||
|
||
service.connectDynamicFormControls(invalidListProvider, group); | ||
triggerControl.setValue('newVal'); | ||
tick(401); | ||
invalidListProvider.list$.subscribe((list) => expect(list[0]).toBe('item-1')); | ||
})); | ||
|
||
it("should not fail with invalid provider but receive warning with missing options data provider.", fakeAsync(() => { | ||
const triggerControl = group.get('testInput'); | ||
|
||
service.connectDynamicFormControls(invalidOptionProvider, group); | ||
triggerControl.setValue('newVal'); | ||
tick(401); | ||
invalidOptionProvider.options$.subscribe((options) => expect(options[0].value).toBe('option-1')); | ||
})); | ||
|
||
it('should show warning with invalid relatedform control', () => { | ||
const relatedFormControl = service.getRelatedFormControl(referenceInvalidControl, group); | ||
expect(relatedFormControl).toBe(null); | ||
}); | ||
|
||
it('should get related form control from rootPath', () => { | ||
const compareControl = group.root.get('test.testRootRadioGroup'); | ||
const relatedFormControl = service.getRelatedFormControl(groupInputTest, group); | ||
|
||
expect(relatedFormControl).toBe(compareControl); | ||
}); | ||
}); |
Oops, something went wrong.