title: unit testing my angular material data table with an expansion panel either does not run or detect finish of animation?
body: <p>I have a large reusable component that wraps up the material data table. My problem is that my unit tests need to check the logic that allows the wrapper to set the initial state of each expansion row(open / closed) and I cannot tell if the click event is running or if fixture.whenRenderingDone is not actually waiting for aniamtions to finish as the height of the clicked row always has a height of 0px, its initial height.</p>
  
  <p>My @component annotation looks like this:</p>
  
  <pre><code>@Component({
      selector: '',
      templateUrl: './.html',
      styleUrls: ['./scss'],
      animations: [
          trigger('detailExpand', [
              state('collapsed', style({ height: '0px', minHeight: '0', display: 'none' })),
              state('expanded', style({ height: '*' })),
              transition('expanded &lt;=&gt; collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
          ]),
      ],
  })
  </code></pre>
  
  <p>This is standard lifted from the angular docs (removed some file names).</p>
  
  <p>My HTML matching this to produce the data table is:</p>
  
  <pre><code>&lt;ng-container *ngIf="hasExpansionPanel"&gt;
     &lt;mat-row *matRowDef="let row; columns: ['expandedDetail']" class="expansion-row no-padding" [@detailExpand]="row == expandedRow || expandAllPanels ? 'expanded' : 'collapsed'"&gt;&lt;/mat-row&gt;
  &lt;/ng-container&gt;
  </code></pre>
  
  <p>The test:</p>
  
  <pre><code>   it('should be able to display a nested table', () =&gt; {
              fixture.whenStable().then(() =&gt; {
                  const standardRows: NodeListOf&lt;HTMLElement&gt; = fixture.nativeElement.querySelectorAll('.standard-row');
                  const firstRow: HTMLElement = standardRows[0];
  
                  const expansionRows: NodeListOf&lt;HTMLElement&gt; = fixture.nativeElement.querySelectorAll('.expansion-row');
                  const firstExpansionRow: HTMLElement = expansionRows[1];
                  const originalPanelRowHeight = firstExpansionRow.clientHeight;
  
                  clickEvent(firstRow);
                  fixture.detectChanges();
  
                  fixture.whenRenderingDone().then(() =&gt; {
                      // animation
                      expect(originalPanelRowHeight).toBe(0);
                      expect(firstExpansionRow.clientHeight).toBeGreaterThan(0);
  
                  });
              });
          });
  </code></pre>
  
  <p>Some supporting code the test uses:</p>
  
  <pre><code>/**
   * Create custom DOM event the old fashioned way
   *
   * https://developer.mozilla.org/en-US/docs/Web/API/Event/initEvent
   * Although officially deprecated, some browsers (phantom) don't accept the preferred "new Event(eventName)"
   */
  export function newEvent(eventName: string, bubbles = false, cancelable = false) {
      const evt = document.createEvent('CustomEvent');  // MUST be 'CustomEvent'
      evt.initCustomEvent(eventName, bubbles, cancelable, null);
      return evt;
  }
  
  export function clickEvent(ele: Element): void {
      ele.dispatchEvent(newEvent('click'));
  }
  </code></pre>