title: How to test angular component which has setTimeout inside ngOnInt using jasmine
body: <p>I am writing unit test in jamsine and I am using nvd3 to display chart.</p>
  <p><strong>chart.component.ts</strong></p>
  <pre><code>export class ChartComponent implements OnInit {
  
      chartConfigs = [];
      @Input chartDetails;
  
      ngOnInit() {
          if(chartDetails.data) {
              setTimeout(() =&gt; {
                  this.chartConfigs.push('1');
              });
          }
      }
  }
  </code></pre>
  <p><strong>chart.component.spec.ts</strong></p>
  <pre><code>it('shall render the chart', fakeAsync(() =&gt; {
      component.chartComponent.data = [
          { x: 0, y: 3.160340011 },
          { x: 1, y: 5.16379641 },
          { x: 2, y: 1.16379641 },
          { x: 3, y: -1.16379641 },
          { x: 4, y: 0.379641 },
          { x: 5, y: 0 },
          { x: 6, y: -15.16379641 },
          { x: 7, y: -111.16379641 }
      ]];
      fixture.detectChanges();
      expect(component.chartComponent.chartConfigs.length).toBe(0);
      tick();
      fixture.detectChanges();
      const chartElement = fixture.debugElement.nativeElement.querySelector('nvd3');
      expect(chartElement.hasChildNodes()).toBeTruthy(); //  error: Cannot read property 'hasChildNodes' of null
      expect(component.chartComponent.chartConfigs.length).toBe(1); // error: length is 0
  }));
  </code></pre>
  <p><strong>chart.component.html</strong></p>
  <pre><code>&lt;div *ngIf=&quot;chartConfigs.length&quot; &gt;
      &lt;nvd3 [chartConfig]=&quot;chartConfigs[0]&quot; [data]=&quot;[chartDetails.data]&quot;&gt;&lt;/nvd3&gt;
  &lt;/div&gt;
  </code></pre>
  <p>I am getting error as:</p>
  <pre><code>TypeError: Cannot read property 'hasChildNodes' of null
  </code></pre>
  <p>The error is coming because chartElement is coming as null. I debugged the code and found that nvd3 is not getting rendered. <strong>Please help me fix this.</strong></p>
  <p>Note: I tried using setTimeOut in spec.ts and test cases were passing, but I don't want to use setTimeout() in spec file.</p>