简体   繁体   English

如何触发(matSortChange)=“sortData(i,$ event)”进行角度app中的单元测试

[英]How to trigger (matSortChange)=“sortData(i, $event)” for unit testing in angular app

I am running unit test for an angular app. 我正在为一个角度应用程序运行单元测试。 I want to manually trigger a matSortChange event and ensure a method is called upon firing this event 我想手动触发matSortChange事件并确保在触发此事件时调用方法

<table matSort (matSortChange)="sortData(i, $event)" matSortActive="enddate" matSortDirection="asc" matSortDisableClear style="margin:0px;" class="table table-striped table-bordered">

<tr>
  <ng-container *ngFor="let subheader of subheaders; let j = index">
  <ng-container *ngIf="subheader[1] == 'activationid'; else second">
    <th class="headers" style="width:300px;" mat-sort-header=" 
     {{subheader[1]}}">{{subheader[0]}} </th>
</tr>

Didn't try it in unit test's but the matSortChange is triggered when the arrow for sorting is clicked. 没有在单元测试中尝试它,但是当单击用于排序的箭头时触发matSortChange you could access the arrow from the DOM like 你可以从DOM访问箭头

document.getElementsByClassName('mat-sort-header-arrow')[indexOfTheColumn].click()

This should fire the matSortChange event and don't forget to spy on sortData method to check the result. 这应该触发matSortChange事件,不要忘记监视sortData方法以检查结果。

Hope this helps to answer my question, this is the angular generated code, I saw this in the inspect element 希望这有助于回答我的问题,这是角度生成的代码,我在inspect元素中看到了这一点

 <div class="mat-sort-header-container">
  <button class="mat-sort-header-button" type="button" aria-label="Change sorting for 
   count"> Count </button>

  <div class="mat-sort-header-arrow ng-trigger ng-trigger-arrowPosition" 
   style="transform: translateY(25%); opacity: 0;">
    <div class="mat-sort-header-stem"></div><div class="mat-sort-header-indicator ng- 
     trigger ng-trigger-indicator" style="transform: translateY(0px);">
       <div class="mat-sort-header-pointer-left ng-trigger ng-trigger-leftPointer" 
        style="transform: rotate(-45deg);"></div>
       <div class="mat-sort-header-pointer-right ng-trigger ng-trigger-rightPointer" 
        style="transform: rotate(45deg);"></div>
       <div class="mat-sort-header-pointer-middle"></div>
     </div>
   </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM