Update mock component to allow setting values on a parent reactive form via the component
See original GitHub issueHi, Nice Library!
Would you be open to updating your mock component to allow Reactive Form values to be propagated out. I like to drive the reactive form, via the form element component, when I am testing so that I know that it is bound correctly.
Something like this:
Sample Class
export class MockReactiveFormsElement implements ControlValueAccessor {
propagateChange = (_: any) => {};
setTouched = (_: any) => {};
writeValue(value: any) { }
registerOnChange(fn) {
this.propagateChange = fn;
}
registerOnTouched(fn) {
this.setTouched = fn;
}
setDisabledState(isDisabled: boolean) {}
}
Use in your test like this:
const typeSelect: MockMdSelectComponent = fixture.debugElement.query(By.css('md-select')).componentInstance;
typeSelect.propagateChange('Ninja');
expect(component.warriorForm.value.type).toEqual('Ninja');
Maybe use __propagatesChange
or some type prefix so as not to clash with a component that has that method, or it could be a subclass that has a seperate function mockReactiveFormComponent
Happy to submit a pull request if you will merge. Just let me know your preferred approach.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Update mock component to allow setting values on a parent ...
Hi, Nice Library! Would you be open to updating your mock component to allow Reactive Form values to be propagated out.
Read more >Mocking a parent FormGroup via @input in Jasmine
I was able to setup a successful Karma spec test for a Reactive Form Parent <-> Child component. Hopefully the example below will...
Read more >Angular Reactive Forms in parent-child components
The parent component will have the FormModel with all the validations, either those are built-in, custom, async or dynamic validators. We will ...
Read more >Implementing reusable and reactive forms in ... - inDepth.dev
We will look at two approaches to achieve this: Using the ControlContainer which enables use to pass a parent form down to it's...
Read more >Component testing scenarios - Angular
To simulate user input, find the input element and set its value property. You will call fixture.detectChanges() to trigger Angular's change detection. But ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
fe547af8d357a477774a161c0dcda7dd417ca7bb
Yeah I had a great weekend, I was actually at a JavaScript camp. https://ix.campjs.com. So I figured it would be a good place to get the work done but every-time I tried to go to Github the 100+ other devs were also trying to download something, so I just gave up on the third timeout attempt.
I could have tried harder, but there were also waterfall walks and mountain vistas to checkout.
https://twitter.com/campjs