component test doubles for Angular 2+
install the package from npm
npm install angular-component-double --save-dev
import the componentDouble
helper function and pass any component decorator meta-data that will be required for the test double
import { componentDouble } from 'angular-component-double';
describe('Test', () => {
// ...
let ChildComponentDouble: any;
beforeEach(async(() => {
ChildComponentDouble = componentDouble({
selector: 'child',
inputs: ['name'],
outputs: ['greeting']
});
TestBed.configureTestingModule({
declarations: [ParentComponent, ChildComponentDouble]
});
});
// ...
{
template: '',
inputs: []
outputs: []
}
each time the component double is rendered in the template, it will be included in the ComponentDouble's list of instances
// ...
it('has a list of instances', () => {
const [firstChildComponent, ...rest] = ChildComponentDouble.instances;
});
// ...
inputs will be bound on the component
// ...
it('assigns the input properties', () => {
expect(firstChildComponent.name).toEqual('Michael Scott');
});
// ...
event emitters will be assigned to each output
// ...
it('stubs the output subscriptions', () => {
firstChildComponent.greeting.next('hello');
});
// ...