Angular 4 Unit Testing
Angular 4 Unit Testing
ng new AngularTesting
2. app.component.spec.ts
describe(AppComponent, () => {
});
<div>
<button (click)=increment()>Increment</button>
<button (click)=decrement()>Decrement</button>
</div>
counter.component.ts
@Component({
selector: app-counter,
templateUrl: ./counter.component.html,
styleUrls: [./counter.component.css]
})
export class CounterComponent {
counter: number;
constructor() {
this.counter = 1;
}
increment() {
this.counter++;
}
decrement() {
this.counter--;
}
};
counter.component.spec.ts
describe(CounterComponent, () => {
let component: CounterComponent;
let fixture: ComponentFixture<CounterComponent>;
let debugElement: DebugElement;
let htmlElement: HTMLElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations : [CounterComponent]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CounterComponent);
component = fixture.componentInstance;
fixture.detechChanges();
debugElement = fixture.debugElement.query(By.css(p));
htmlElement = debugElement.nativeElement;
});
// Arrange
const initialValue = component.counter;
//Act
component.increment();
fixture.detectChanges();
const newValue = component.counter;
// Assert
expect(newValue).toBeGreaterThan(initialValue);
});
Unit/Component:
Test a single Component, Service, Pipe etc.
Test a single specific behavior in a controlled environment.
Mock everything you need to test this functionality.
Use code coverage analysis to make sure everything is covered.
(Usually) Improves code structure and quality.
Test edge cases on the most detailed level.
Know if a change breaks a specific behavior.
Does not test if the whole application or a process works in a real life.
ng new letslearn-ci
Points are increased, if then you click reset, the points go back to 0.
With @angular/cli, you get a e2e folder and structure that suggests to create a page object
which helps you to access the elements. The page object for the default page is found in
./e2e/app.po.ts.
What: Page Object: A class with functions that return elements like Buttons, Texts or
Whatever you need for the test.
Why: If you change your page structure, you only have to change the Page Object, but the
Tests can stay the same.
Now we can write our e2e tests that use those elements to interact with the page and test its
result.
app.e2e-spec.ts
beforeEach(() => {
page = new LetslearnPage();
});
expect(page.getPoints()).toEqual('1');
page.getPlus1Button().click();
expect(page.getPoints()).toEqual('2');
page.getPlus1Button().click();
page.getPlus1Button().click();
page.getPlus1Button().click();
expect(page.getPoints()).toEqual('5');
});
page.getPlus1Button().click();
page.getPlus1Button().click();
page.getPlus1Button().click();
expect(page.getPoints()).toEqual('4');
page.getResetButton().click();
expect(page.getPoints()).toEqual('0');
});
});
Tests should speak for themselves, meaning that you should be able to read them top down as if
it was a story ( with some imaginiation).
5. Run the tests
ng e2e
Solution: You only import the operators you need (tree shaking) from RxJS. There are two types
of RxJS operators Static and Instance. A static method is applied to the Observable class itself.
This concept is important because the methods are located in different places.
Instance rxjs/add/operator/{method}
Static - rxjs/add/observable/{method}
hello.combineLatest(world)
import rxjs/add/observable/combineLatest ;
Observable.combineLatest(hello, world);
Solution: You extract data by subscribing to Observables. You have two main ways of going
about this in Angular.
( 1 ). Subscribe with async pipe in the HTML or
( 2 ). Subscribe manually in the TypeScript.
In this example, we use method 1 for cats and method 2 for dogs. The result is exactly the same.
cats: FirebaseListObservable<any[]>;
dogs: Array<any[]>;
ngOnInit() {
this.cats = this.db.list('/cats')
this.db.list('/dogs').subscribe(dogs => {
this.dogs = dogs
})
}
In the HTML, we unwrap cats with the async pipe, but dogs are just iterated over like a normal
array.
Solution: When you use the async pipe this will be handled automatically. If you created a
Subscription in the TypeScript, you will need to unsubscribe manually. The OnDestroy
ngOnDestroy() {
this.subscription.unsubscribe()
})
Problem: You need to extract data from observableA before you can load a related
observable.
Solution: This is a common problem with authentication. You might need to load a user,
then fetch their associated data from the database. In this example, we load the human,
then load the pets that are owned by this human. We use the switchMap operator ( aka
mergeMap) to make this possible.
human: FirebaseObjectObservable<any>;
dogs: Observable<any[]>;
ngOnInit() {
this.human = this.db.object('/humans/jeff')
this.dogs = this.human.switchMap(human => {
return this.db.list('/dogs', {
query: {
orderByChild: 'owner',
equalTo: human.name
}
})
})
}
5. Mapping Observables
Problem: You want to transform the structure of the data inside an observable.
Solution: Lets say we want to return the length of a list observable, rather than the data itself.
The map operator allows us to change convert the array into a number by returning its length.
We can also take an object observable and return one of its properties a string observable.
catCount: Observable<number>;
dogName: Observable<string>;
ngOnInit() {
this.catCount = this.db.list('/cats')
.map(cats => {
return cats.length
})
this.dogName = this.db.object('/dogs/-KpRuD47u8810-BXEn5Q')
.map(dog => {
return dog.name
})
}
6. Combining Observables
Solution: Let add a cat and dog into their own observable using the combineLatest operator.
There are many different combine operators in RxJS, so check out the docs to see what works
best in your situation.
animals: Observable<any[]>
ngOnInit() {
this.cat = this.db.object('/cats/-KpRuC...Id')
this.dog = this.db.object('/dogs/-KpRuD...Id')
this.animals = Observable
.combineLatest(this.cat, this.dog)
}
A BehaviorSubject is an observable that can receive new data by calling next(data). In this
example, make one of the dogs a the currentDog, then subscribe to it in the HTML.
This may seem trivial in a single component, but you can create subjects in services, then share
the current state of data throughout your application by injecting the service into components.
dogs: FirebaseListObservable<any[]>;
currentDog = new BehaviorSubject(null);
ngOnInit() {
this.dogs = this.db.list('/dogs')
}
changeDog(dog) {
this.currentDog.next(dog)
}
<div *ngFor = let dog of dogs | async (click)=changeDog(dog)>
{{ dog.name }}
<img [src]=dog.url/>
</div>
<div>
{{ (currentDog | async)?.name }}
<img [src]=(currentDog | async)?.url>
</div>
This is probably the most common and straightforward method of sharing data. It works by using
the Input() decorator to allow data to be passed via the template.
parent.component.ts
child.component.ts
ViewChild allows a one component to be injected into another, giving the parent access to its attributes
and functions. One caveat, however, is that child wont be available until after the view has been
initialized. This means we need to implement the AfterViewInit lifecycle hook to receive the data from
the child.
parent.component.ts
child.component.ts
Another way to share data is to emit data from the child, which can be listed to by the parent. This
approach is ideal when you want to share data changes that occur on things like button clicks, form
entires, and other user events.
In the parent, we create a function to receive the message and set it equal to the message variable.
In the child, we declare a messageEvent variable with the Output decorator and set it equal to a new
event emitter. Then we create a function named sendMessage that calls emit on this event with the
message we want to send. Lastly, we create a button to trigger this function.
The parent can now subscribe to this messageEvent thats outputted by the child component, then run
the receive message function whenever this event occurs.
parent.component.ts
child.component.ts
When passing data between components that lack a direct connection, such as siblings, grandchildren,
etc, you should you a shared service. When you have data that should aways been in sync, I find
the RxJS BehaviorSubject very useful in this situation.
You can also use a regular RxJS Subject for sharing data via the service, but heres why I prefer a
BehaviorSubject.
It will always return the current value on subscription - there is no need to call onnext
It has a getValue() function to extract the last value as raw data.
It ensures that the component always receives the most recent data.
In the service, we create a private BehaviorSubject that will hold the current value of the message. We
define a currentMessage variable handle this data stream as an observable that will be used by the
components. Lastly, we create function that calls next on the BehaviorSubject to change its value.
The parent, child, and sibling components all receive the same treatment. We inject the DataService in
the constructor, then subscribe to the currentMessage observable and set its value equal to the
message variable.
Now if we create a function in any one of these components that changes the value of the message.
when this function is executed the new data its automatically broadcast to all other components.
data.service.ts
parent.component.ts
sibling.component.ts