concept Angular zone in category angular

appears as: Angular zone
Testing Angular Applications

This is an excerpt from Manning's book Testing Angular Applications.

9.5.4 Highway to the Angular zone

Protractor knows about the Angular zone; it knows when tasks are pending that might cause a change detection. When you enable waitForAngular, Protractor will cause all of your WebDriver commands to wait until there are no more tasks pending in the Angular zone. Let’s look at a simple example of running asynchronous tasks in a browser.

Assume that the code in figure 9.4 is running in the Angular zone. In this example, Protractor would wait forever, because the pollForever() function (in the lower-left red box) is constantly creating a task in the Angular zone.

Figure 9.5 Running a polling task outside the Angular zone

c09_05.png

Now you know why your test was timing out earlier. The updates from the ContactFeedService were scheduling async tasks in the Angular zone, and because it’s a continuous stream of tasks, Protractor will wait until the test times out. You could turn off waitForAngular and use expected conditions, but you also could fix the test by changing the ContactFeedDialogComponent, as in the following listing.

Listing 9.10 Using runOutsideAngular in ContactFeedDialogComponent
import {Component, OnInit, OnDestroy, NgZone, Optional, Inject} from ➥'@angular/core';
import {MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
import {ContactFeedService} from '../shared/services/contact-feed.service';
import {Subscription} from 'rxjs/Subscription';

@Component({
  selector: 'app-contact-feed',
  templateUrl: './contact-feed.component.html',
  styleUrls: ['./contact-feed.component.css']
})
export class ContactFeedDialogComponent implements OnInit, OnDestroy {
  sub: Subscription;
  updates: string[] = [];
  name: string;
  closeDisabled = true;

  constructor(public dialogRef: MdDialogRef<ContactFeedDialogComponent>, 
      private feed: ContactFeedService, 
 private zone: NgZone,    #1  
      @Optional() @Inject(MD_DIALOG_DATA) data: any) {
    this.name = data.name;
  }

  ngOnInit() {
    this.closeDisabled = false;

this.zone.runOutsideAngular(() => {    #2  
      this.sub = this.feed.getFeed().subscribe((x) => {
this.zone.run(() => {  #3  
          this.updates.push(x);
          if (this.updates.length > 4) {
            this.updates.shift();
          }
        });
      });
    });
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
}

#1   Injects NgZone into the component
#2   Runs the subscription outside the Angular zone, so it won’t block Protractor
#3   Adds the update in the Angular zone, so the change propagates to the page


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":18,\"ch\":1},{\"line\":18,\"ch\":22}],[{\"line\":26,\"ch\":0},{\"line\":26,\"ch\":35}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":21}],[{\"line\":33,\"ch\":8},{\"line\":33,\"ch\":11}],[{\"line\":34,\"ch\":6},{\"line\":34,\"ch\":9}],[{\"line\":35,\"ch\":4},{\"line\":35,\"ch\":7}],[{\"line\":33,\"ch\":8},{\"line\":33,\"ch\":11}],[{\"line\":34,\"ch\":6},{\"line\":34,\"ch\":9}],[{\"line\":35,\"ch\":4},{\"line\":35,\"ch\":7}]]"}
!@%STYLE%@!

Now that the subscription is created outside of the Angular zone, it won’t block Protractor. But you need to apply the update within the Angular zone so that Angular will know about the change to your model and will update the page. With only a small change to the component, the test passes! Protractor still will wait while each update is rendered in the dialog, but now it won’t time out waiting for the stream of updates to finish.