-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
ngMocks#findInstance not working for ViewContainerRef#createComponent case example #1596
Comments
Hi, thanks for the report. This one will be the first fix right after the stable release. |
Hi, happy Sunday! Works well for me on v13.0.0. import { Component, ViewContainerRef } from '@angular/core';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';
@Component({
selector: 'parent',
template: '',
})
class ParentComponent {
public constructor(viewContainerRef: ViewContainerRef) {
const vcr: any = viewContainerRef;
if (vcr.createComponent) {
vcr.createComponent(ChildComponent);
}
}
}
@Component({
selector: 'child',
template: '',
})
class ChildComponent {}
describe('issue-1596', () => {
beforeEach(() => MockBuilder(ParentComponent, ChildComponent));
it('finds child component', () => {
MockRender(ParentComponent);
expect(
ngMocks.findInstance(ChildComponent, undefined),
).toBeDefined();
});
}); |
Ah, it doesn't work via TestBed. Checking that. Aha, a nice one, thanks! I'll be working on its fix. |
I guess the I am not sure if this information helps though; probably it is just confusing. If you need more information I can elaborate on it. I also have other case examples which probably have the same root cause. I am not 100% sure where the mocked component instances are registered within ng-mocks, but I guess the modification has to be made there. |
what I found so far is the problem of the root level. for example, you could try to use it as a temp solution, and i'll be working on a proper fix for it. |
Hi there, I think there is a good question to discuss. In the rendered HTML child component is actually outside of current fixture. <div id="root0" ng-version="13.2.1">
<div>parent</div>
</div>
<child>
<span>child</span>
</child> |
The desired behavior works only in ivy mode. |
fix: looking in vcr.createComponent on root node #1596
v13.0.1 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. |
@satanTime First of all I would like to thank you for resolving the issue. Secondly, I would like to ask you for a favour: Can you probably point me to the source code where the instance of the mocked component is actually registered? I assume that ngMocks uses some kind of registry. In general, I am very curious how this library works behind the scenes. Unfortunately I could not grasp it with the source code alone. I guess that explaining the whole concept is probably to time consuming, but maybe you can share some thoughts. Furthermore, it would be cool if this repository would provide a "Discussion" channel, where topics like this could be tackled. Greets, |
Hi @LukasMachetanz, the registry is on Angular's side. Source code is a mess, and with every fix I make it more messy. Answering your question:
|
Hi @ike18t, long time no see, happy new year! Could you enable "Discussion" in the repo? |
Hey @satanTime ! I think you are good to go! |
Great thanks! |
Hi everyone!
As the title states I am presenting a simplified case example using
ViewContainerRef#createComponent
, wherengMocks#findInstance
does not return the created instance. I am wondering if this is expected or a bug. Any help appreciated.Let's assume having the following
ParentComponent
, which will be the Subject Under Test (SUT).And let's assume having the following
ChildComponent
.The corresponding test suite could look something like this:
ngMocks.findInstance(ChildComponent)
does not return the instance created withthis.viewContainerRef.createComponent(ChildComponent)
.fixture.debugElement.query(By.directive(ChildComponent))
finds the dynamically created component, which indicates that the mock could be use properly. The only problem isngMocks#findInstance
, which does not return the obviously available instance.As I stated before, I am wondering if this is intended or a bug. Thanks.
The text was updated successfully, but these errors were encountered: