Mocking Child Components - Angular 2
I posted this question so I could post an answer as I struggled with this for a day or two. Here's how you do it:
let declarations = [
ProductSelectedComponent,
ProductSettingsComponent,
ProductEditorComponent,
ProductOptionsComponent
];
beforeEach(() => {
TestBed.configureTestingModule({
declarations: declarations,
providers: providers
})
.overrideComponent(ProductSettingsComponent, {
set: {
selector: 'product-settings',
template: `<h6>Product Settings</h6>`
}
})
.overrideComponent(ProductEditorComponent, {
set: {
selector: 'product-editor',
template: `<h6>Product Editor</h6>`
}
})
.overrideComponent(ProductOptionsComponent, {
set: {
selector: 'product-options',
template: `<h6>Product Options</h6>`
}
});
fixture = TestBed.createComponent(ProductSelectedComponent);
cmp = fixture.componentInstance;
de = fixture.debugElement.query(By.css('section'));
el = de.nativeElement;
});
You have to chain the overrideComponent
function for each of the child components.
Found a nearly perfect solution, that will also correctly throw errors if someone refactors a component: https://www.npmjs.com/package/ng-mocks
npm install ng-mocks --save-dev
Now in your .spec.ts you can do
import { MockComponent } from 'ng-mocks';
import { ChildComponent } from './child.component.ts';
// ...
beforeEach(() => {
TestBed.configureTestingModule({
imports: [FormsModule, ReactiveFormsModule, RouterTestingModule],
declarations: [
ComponentUnderTest,
MockComponent(ChildComponent), // <- here is the thing
// ...
],
});
});
This creates a new anonymous Component that has the same selector, @Input()
and @Output()
properties of the ChildComponent
, but with no code attached.
Assume that your ChildComponent
has a @Input() childValue: number
, that is bound in your component under test, <app-child-component [childValue]="inputValue" />
Although it has been mocked, you can use By.directive(ChildComponent)
in your tests, as well as By.css('app-child-component')
like so
it('sets the right value on the child component', ()=> {
component.inputValue=5;
fixture.detectChanges();
const element = fixture.debugElement.query(By.directive(ChildComponent));
expect(element).toBeTruthy();
const child: ChildComponent = element.componentInstance;
expect(child.childValue).toBe(5);
});
Careful about the NO_ERRORS_SCHEMA
. Let's quote another part of the same the docs:
Shallow component tests with NO_ERRORS_SCHEMA greatly simplify unit testing of complex templates. However, the compiler no longer alerts you to mistakes such as misspelled or misused components and directives.
I find that drawback quite contrary to the purposes of writing a test. Even more so that it's not that hard to mock a basic component.
An approach not yet mentioned here is simply to declare them at config time:
@Component({
selector: 'product-settings',
template: '<p>Mock Product Settings Component</p>'
})
class MockProductSettingsComponent {}
@Component({
selector: 'product-editor',
template: '<p>Mock Product Editor Component</p>'
})
class MockProductEditorComponent {}
... // third one
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
ProductSelectedComponent,
MockProductSettingsComponent,
MockProductEditorComponent,
// ... third one
],
providers: [/* your providers */]
});
// ... carry on
});
Generally, if you have a component used inside the view of the component that you're testing and you don't necessarily want to declare those components because they might have their own dependencies, in order to avoid the "something is not a known element" error you should use NO_ERRORS_SCHEMA
.
import { NO_ERRORS_SCHEMA } from '@angular/core';
TestBed.configureTestingModule({
declarations: declarations,
providers: providers
schemas: [ NO_ERRORS_SCHEMA ]
})
Based on the docs :
Add NO_ERRORS_SCHEMA to the testing module's schemas metadata to tell the compiler to ignore unrecognized elements and attributes. You no longer have to declare irrelevant components and directives.
More on this : https://angular.io/docs/ts/latest/guide/testing.html#!#shallow-component-test