Reputation: 3657
I started to learn angular 2 and want to get things right with unit test. So I want all my directives/components to write with tests.
In angularJS (first version) to test directive You use $compile
. Here's example from doc:
it('Replaces the element with the appropriate content', function() {
var element = $compile("<a-great-eye></a-great-eye>")($rootScope);
$rootScope.$digest();
expect(element.html()).toContain("lidless, wreathed in flame, 2 times");
});
How to compile html text in angular 2 to write a test?
I want to test simpliest direcive:
import {Component} from 'angular2/core';
@Component({
selector: 'email',
template: `Hello Email`
})
export class EmailComponent {
}
Upvotes: 1
Views: 1596
Reputation: 657356
Use TestComponentBuilder
like shown in this example from https://github.com/angular/angular/blob/9e44dd85ada181b11be869841da2c157b095ee07/modules/angular2/test/common/directives/ng_for_spec.ts
it('should reflect added elements',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
tcb.overrideTemplate(TestComponent, TEMPLATE)
.createAsync(TestComponent)
.then((fixture) => {
fixture.detectChanges();
(<number[]>fixture.debugElement.componentInstance.items).push(3);
fixture.detectChanges();
expect(fixture.debugElement.nativeElement).toHaveText('1;2;3;');
async.done();
});
}));
Upvotes: 1