-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ControlValueAccessor Mock not working on MatInput #305
Comments
Hi, thanks for the report! I'll take a look today. Might you share how Also is it really |
Also is it on 11.7.0 or 11.8.0? there was a regression before: #302 |
MatInput is imported in "MyModule" via imports: [MatInputModule] (so I think that should be mocked).
(also with mock(MatInputModule)
Yes it's find. But it's interesting - the mock-helper.d.ts does not contain a reveal Method (11.7.0) |
Ah true :) I forgot that it will be transformed to |
I've the same behavior with 11.8.0 |
thanks, then I'll investigate what I did break again. |
So, the thing is that
Before, all mock directives and components implemented But still, let's discuss it, maybe I missed something. In the example you provided, the real Therefore, to simulate its behavior, it should be mocked. MockBuilder(MyComponent, MyModule)
.keep(ReactiveFormsModule)
.mock(DefaultValueAccessor) // <-- this is the change
); const instance = ngMocks.get(ngMocks.find(['data-testid', 'inputControl']), DefaultValueAccessor);
expect(isMockControlValueAccessor(instance)).toEqual(true); import { Component, NgModule } from '@angular/core';
import { DefaultValueAccessor, FormControl, ReactiveFormsModule } from '@angular/forms';
import { MatInput, MatInputModule } from '@angular/material/input';
import { isMockControlValueAccessor, MockBuilder, MockRender, ngMocks } from 'ng-mocks';
@Component({
selector: 'my',
template: `
<input data-testid="inputControl" matInput [formControl]="myControl" required>
`,
})
class MyComponent {
public readonly myControl = new FormControl();
}
@NgModule({
declarations: [MyComponent],
exports: [MyComponent],
imports: [ReactiveFormsModule, MatInputModule],
})
class MyModule {}
describe('issue-305', () => {
beforeEach(() => MockBuilder(MyComponent, MyModule).keep(ReactiveFormsModule).mock(DefaultValueAccessor));
it('correctly mocks matInput', () => {
MockRender(MyComponent);
// MatInput does not implement ControlValueAccessor
const matInput = ngMocks.get(ngMocks.find(['data-testid', 'inputControl']), MatInput);
expect(isMockControlValueAccessor(matInput)).toEqual(false);
// DefaultValueAccessor does implement ControlValueAccessor
const valueAccessor = ngMocks.get(ngMocks.find(['data-testid', 'inputControl']), DefaultValueAccessor);
expect(isMockControlValueAccessor(valueAccessor)).toEqual(true);
});
}); |
Ah, doesn't work in no-ivy. I think the best way would be to provide |
Fixed. Instead of calling (now it works correctly and it is fine to continue using it) const instance = ngMocks.get(ngMocks.find(['data-testid', 'inputControl']), DefaultValueAccessor);
if (isMockControlValueAccessor(instance)) {
instance.__simulateChange(123);
} you can use the next syntax now const el = ngMocks.find(['data-testid', 'inputControl']);
ngMocks.change(el, 'desiredValue'); |
v11.9.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems. |
Works like a charm! THANKS! |
When having an input element like that:
with a test setup like that:
I can get a MatInput Mock Instance with:
But this mock is not a "MockControlValueAccessor" (isMockControlValueAccessor is false). I also tried other approaches but did not find a way to get it working. Ticket #73 is about a fix for that, but maybe this fix got lost by the refactoring between the version!?
The text was updated successfully, but these errors were encountered: