Comments (12)
@ike18t Will do! Hopefully I can find some time this weekend.
from ng-mocks.
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.
from ng-mocks.
from ng-mocks.
Thanks for the request, I have a little experience with Reactive Forms but I'm not following your question. Do you mind setting up a quick StackBlitz project to describe the issue? Don't worry about creating test in the StackBlitz, just a component that is representative of this issue so I can follow along with this feature request.
Thanks!
from ng-mocks.
Here it is
https://stackblitz.com/github/vespertilian/testing-reactive-forms-third-party-components
Might be easier to pull it and run it from the CLI
https://github.com/vespertilian/testing-reactive-forms-third-party-components
it('should update the form when you fill in the type "select" field', () => {
const {fixture, component} = setup();
const typeSelect: MockMdSelectComponent = fixture.debugElement.query(By.css('mat-select')).componentInstance;
typeSelect.propagateChange('Ninja');
expect(component.warriorForm.value.type).toEqual('Ninja');
});
Basically when testing forms I prefer to drive the form not just use for formController instance in the component controller as that does not tell you if you have bound to the form via "formControlName"
from ng-mocks.
@getsaf Just checking in, you are probably busy, which is fine. If there is anything else I can do to help move this issue along please let me know.
from ng-mocks.
@vespertilian What do you think about writeValue triggering the change callback? Would that solve your problem?
from ng-mocks.
Hey @ike18t, sorry for the late reply this slipped by me.
I had thought that as well initially, it's just as write value is normally used to set the value on the custom form control, I was concerned this will lead to unexpected behaviour, or hard to discover bugs.
I think this would work when you just grab the custom form control from the debug element and call writeValue.
However say you, or some other part of your test calls myFormControl.setValue(name: 'vespert'), if we bound it to writeValue on the form control, the value will be propagated back out, which would not usually happen as write value is normally only called when angular reactive formsl changes the value, so if you were say subscribing to value changes you would get a false output.
Also not sure if binding the input and output together could lead to some type of infinite feedback loop / weird change detection issues.
interface ControlValueAccessor {
writeValue(obj: any): void
registerOnChange(fn: any): void
registerOnTouched(fn: any): void
setDisabledState(isDisabled: boolean)?: void
}
# it's these two 'fn' we want to be able to mock out and manually activate
registerOnChange(fn: any): void
registerOnTouched(fn: any): void
We could do it with a getter and setter on value and touched maybe?
formControl.value = 'vespert'
I still think
propagateChange = (_: any) => {};
setTouched = (_: any) => {};
// or
__propagateChange = (_: any) => {};
__setTouched = (_: any) => {};
is the clearest and cleanest way to accomplish this, I would probably go with the __ just in case someone has a function on there custom form control called propagateChange.
from ng-mocks.
@vespertilian Fair enough. I don't have that much experience dealing with reactive forms so I'm going to have to lean on your experience. Feel free to submit a PR.
from ng-mocks.
@ike18t No internet this weekend, it will have to be some time next week!
from ng-mocks.
@vespertilian A weekend w/o internet? Are you OK? Did you survive?
from ng-mocks.
Wow! That is awesome!
from ng-mocks.
Related Issues (20)
- Docs: Rename testing guide NGRS to NGXS HOT 2
- Bug: ngMocks.globalReplace() doesn't work HOT 3
- Bug: circleci IE HOT 1
- Bug: MockInstance won't override values for InjectionToken HOT 1
- Bug: Overwriting defaultMock of 'get' or readonly property results in `Property ... does not have access type get` HOT 2
- Bug: Memory Leak HOT 2
- Bug: MockInstance doesn't manipulate the instance (findInstance works though) HOT 1
- MockInstance is not clear how spy methods get assigned Bug: HOT 1
- Feature Request: Should ngMocks set signals mocks to singal(null) rather than null? HOT 2
- Bug: Mocking standalone components may cause warnings HOT 6
- Feature Request: Support EnvironmentProviders in provide and HttpInterceptorFn exemple HOT 2
- Bug: MockInstance.scope(); is mentioned in "Getting started" doc although being unnecessary nowadays(?) HOT 7
- Bug: Angular 16 ngMocks.findInstance swallows original error HOT 2
- Bug: MockedComponentFixture is actually assignable to ComponentFixture contrary to what doc says HOT 3
- Feature Request: docs how to add a new angular version HOT 1
- Bug: ngMocks.findInstance unable to find instance (Angular 17) HOT 19
- Bug: Unable to install ng-mocks with Angular 17 HOT 9
- Bug: Unable to correctly import TranslocoTestingModule.forRoot in component unit test HOT 2
- Feature request: Schematics HOT 1
- Update docs about animation HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ng-mocks.