I recently joined a development team working on an Angular2 application that requires all unit tests to be written using the Jasmine framework. I'm curious if there is a tool available that can automatically generate spec files for each class, providing a starting point with test cases based on methods and attributes like *ng-If in the templates. Below is an example of the component a.component.js
import { Component, Input, Output, Inject, OnChanges, EventEmitter, OnInit } from '@angular/core';
import {Http} from '@angular/http';
@Component({
selector: 'a-component',
template : `
<div *ng-If="model">
<a-child-component [model]="model">
</a-child-component>
</div>`
})
export class AComponent implements OnInit {
@Input() anInput;
ngOnInit() {
if(this.anInput){
this.model = anInput;
}
}
constructor(@Inject(Http) http){
this.restAPI = http;
}
methodOne(arg1,arg2){
//do something
}
methodTwo(arg1,arg2){
//do something
}
//...
}
This would then generate a corresponding spec file: a.componenet.spec.js
import { beforeEach,beforeEachProviders,describe,expect,it,injectAsync } from 'angular2/testing';
import { setBaseTestProviders } from 'angular2/testing';
import { TEST_BROWSER_PLATFORM_PROVIDERS,TEST_BROWSER_APPLICATION_PROVIDERS } from 'angular2/platform/testing/browser';
setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);
import { Component, Input, Output, Inject, OnChanges, EventEmitter, OnInit } from '@angular/core';
import { ComponentFixture, TestBed, inject } from '@angular/core/testing';
import { MockComponent } from 'ng2-mock-component';
import { async } from '@angular/core/testing';
import { Http } from '@angular/http';
import { HttpMock } from '../mocks/http.mock';
import { AComponent } from './a.component';
let model = {"propOne":[],"propTwo":"valueTwo"};
describe('AComponent', () => {
let fixture;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AComponent,
MockComponent({
selector: 'a-child-component',
template:'Hello Dad!'
,inputs: ['model']
})
],
providers: [{ provide: Http, useClass: HttpMock }]
});
fixture = TestBed.createComponent(AComponent);
fixture.componentInstance.anInput= model;
});
it('should create the component',() => {
//
});
it('should test methodOne',() => {
//
});
it('should test methodTwo',() => {
//
});
it('should generate the child component when model is populated',() => {
//
});
)