Unit Testing in Angular
1. Component
How to Test Component .
[Link]
import { Component } from '@angular/core';
@Component({
selector: 'app-counter',
templateUrl: './[Link]',
styleUrls: ['./[Link]']
})
export class CounterComponent {
counter : number;
constructor( ) {
[Link] = 0;
}
inc(){
[Link]++;
}
dec(){
[Link]--;
}
[Link]
<p>
Number : {{counter}}
</p>
<button (click) ="inc();">Increment</button>
<button (click) ="dec();">Decrement</button>
[Link]
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CounterComponent } from './[Link]';
import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';
describe('CounterComponent', () => {
let component: CounterComponent;
let fixture: ComponentFixture<CounterComponent>;
let debugElement: DebugElement;
let HtmlElement: HTMLElement;
beforeEach(async(() => {
[Link]({
declarations: [ CounterComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = [Link](CounterComponent);
component = [Link];
[Link]();
debugElement = [Link]([Link]('p'));
HtmlElement = [Link];
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should display the current number of the counter', () => {
const initialvalue = [Link];
// expect([Link]).toEqual('Number : 0');
expect(initialvalue).toBe(0);
});
it('should increment the counter number by one', () => {
const initialvalue = [Link];
[Link]();
[Link]();
const newvalue = [Link];
expect(newvalue).toBeGreaterThan(initialvalue);
});
it('should decrement the counter number by one', () => {
const initialvalue = [Link];
[Link]();
[Link]();
const newvalue = [Link];
expect(newvalue).toBeLessThan(initialvalue);
});
});
[Link] Module Testing