Not working with library input components that have implemented ControlValueAccessor
See original GitHub issueI’m getting this error
error properties: Object({ originalStack: 'Error: No value accessor for form control with unspecified name attribute
when rendering an external library component that has ControlValueAccessor implemented. The library components structure is this
export class InputComponent extends InputBase<string> {
// and base
export abstract class InputBase<T> implements ControlValueAccessor {
Component is used like this
<my-input
#hexInput
(input)="onKey(hexInput.value)"
[(ngModel)]="currentColor"
[label]="'HEX code'"
[helperText]="'Please add a valid HEX color'"
[invalid]="invalidHex"
></my-input>
I can fix the issue by adding ngDefaultControl
attribute but afaik this issue occurs only because the component is not recognized as having implemented ControlValueAccessor.
Can you please assist with this?
Issue Analytics
- State:
- Created 3 years ago
- Comments:22 (12 by maintainers)
Top Results From Across the Web
Implementing controlValueAccessor in our external Angular ...
It looks like the only problem is using our Angular component library, and the host Angular application can't detect the library's ...
Read more >Never again be confused when implementing ... - InDepth.Dev
Here I'll first explain why we need ControlValueAccessor and how it's used inside Angular. Then I'll demonstrate how to wrap a 3rd party...
Read more >Create a custom form control component in Angular with ...
Introducing Control Value Accessor Interface! · You need to import either the Template Driven Forms or Reactive Forms modules in order to use...
Read more >Creating a custom form control in Angular - Orjan De Smet
So we start by implementing the ControlValueAccessor interface from @angular/forms into the component. This interface has three functions that ...
Read more >Angular Reactive Forms: Custom Form Control - Medium
First, component needs to implement ControlValueAccessor interface. · Second, component has to be added to the list of known value accesors. It ...
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 FreeTop 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
Top GitHub Comments
Ok, so we managed to do a workaround in our app. We have around 1300 test files and manually adding
ngDefaultControl
wasn’t even an option.Quick solution
The
shallowSupportForAngular10
should be added in the main test entry point. In our case it wassetup-jest.ts
Explanation
Let’s say we have the
<sps-example-test-control>
component that prints hello on screen and logs when it is created:Now let’s use our component in test component and attach
[(ngModel)]
to it:Angular 9
What we should be getting is an error, because our custom test control doesn’t implement
ControlValueAccessor
interface, which is required forngModel
to function properly, right?When we run this component in our app as a normal component it behaves correctly, it throws an error:
However!!!, our test cases pass!!! What? Why? Shouldn’t the tests throw when view throws?
It turns out when we print
ngModel
instance in our tests we get the result below. It seems thatShallow
for some reason automatically created mock of our<sps-example-test-control>
and automatically attached it tongModel
asvalueAccessor
. It should not work like this!Angular 10
In Angular 10 it was fixed, that means the test throws an error, because
Shallow
didn’t automagically treated our mock asvalueAccessor
. So everything should be fine, right? 😃It turns out that all our
valueAccessor
bindings are performed via provider inside@Component({providers: [{provide: NG_VALUE_ACCESSOR, ...}]})
decorator, BUT because Shallow mocks all of the controls, the provider is never instantiated and thus each test case that uses our custom control is broken 😦Hell yeah! Glad this worked out. I had been meaning to tackle component-level providers for a while but it hadn’t given anyone any trouble until you mentioned it.
Thanks for reporting. I’ll get a proper 10.1.0 release out soon and update the docs.