-
Notifications
You must be signed in to change notification settings - Fork 25
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
Does shallow-render support ViewChild queries? #64
Comments
Since we use If it's giving you trouble, check that your lib is using at least ng-mocks@6.2.3 and it should work. I will leave this issue open until I get a chance to confirm it. Since this is a big-feature, I plan on releasing another version of |
Just cut a new release with support for this!! Check out the example spec here. |
I'm actually trying to do something similar and I'm finding that the https://stackblitz.com/edit/github-jc7sw5?file=examples%2Fcomponent-with-view-child.spec.ts
|
Looks like the issue you're seeing here is that, Shallow mocks ALL modules except for If your project uses Shallow.neverMock(FormsModule); If you just need a one-off, you can do it right in your test: shallow = new Shallow(MyComponent, MyModule).dontMock(FormsModule); ^-- This worked in my fork of your StackBlitz example |
@getsaf, thanks for you help with that scenario and we are indeed using import { ContentChild, Component, NgModule, AfterContentInit, ViewChild } from '@angular/core';
import { Shallow } from 'shallow-render';
import { FormsModule } from '@angular/forms';
import { fakeAsync, tick } from '@angular/core/testing';
////// Module Setup //////
@Component({
selector: 'my-form',
template: `
<form #simpleForm="ngForm">
<input type="text" id="username" name="adminUsername" [(ngModel)]="adminUsernameText" #adminUsername="ngModel">
</form>`,
})
class FormComponent {
@ViewChild('simpleForm') myForm: any;
adminUsername:string = 'user1';
}
@NgModule({
declarations: [FormComponent],
imports: [FormsModule]
})
class FormModule {}
//////////////////////////
describe('form component with @ViewChild', () => {
let shallow: Shallow<FormComponent>;
const render = fakeAsync(async () => {
const rendering = await shallow.render();
tick();
rendering.fixture.detectChanges();
return rendering;
});
beforeEach(() => {
shallow = new Shallow(FormComponent, FormModule)
});
it('should find reference to form template ref', async () => {
const {instance} = await render();
expect(instance.myForm).toBeDefined('myForm should be defined');
});
it('should find reference to form template ref with value prop', async () => {
const {instance, fixture, find} = await render();
const input = find('#username').nativeElement;
expect(input).toBeDefined('Input should be defined');
input.value = "user@abc";
fixture.detectChanges();
expect(instance.myForm.vaule).toBeDefined('myForm.value should be defined');
expect(instance.myForm.vaule.adminUsername).toEqual('user@abc');
});
}); |
Hi @ticklemycode , I noticed a few problems with your stackblitz example:
Here are my updates: |
@ike18t, ah interesting. So anytime we need to get a reference to the a ngForm's This helps a lot! Thanks for taking a look! |
Hello,
I have the next component tree:
The ParentComponent has a ViewChild reference to ChildComponent. When i mock ChildComponent by hand in the ParentComponent spec, it renders but never gets the reference to the ViewChild. Is any way to solve this using shallow-render?
If it helps, this repo has an example of the case i explained above: https://github.com/Ivykp/test-viewchild-mock
Thanks!
The text was updated successfully, but these errors were encountered: