【问题标题】:Writing unit test for mat-autocomplete that filters customers为过滤客户的 mat-autocomplete 编写单元测试
【发布时间】:2020-03-21 01:22:41
【问题描述】:

我正在为 Angular 应用程序编写单元测试,确切地说是 angular6。我在代码中有mat-autocomplete,我想要测试的是根据我在输入框中输入的输入过滤的用户。我受到enter link description here 帖子的启发。

这里是spec文件的sn-p代码

  it('should filter customer',async () => {
    fixture.detectChanges();
    let toggleButton = fixture.debugElement.query(By.css('input'));
    console.log(toggleButton.nativeElement);

    toggleButton.nativeElement.dispatchEvent(new Event('focusin'));
    toggleButton.nativeElement.value = "xyz";
    toggleButton.nativeElement.dispatchEvent(new Event('input'));
    fixture.detectChanges();
    await fixture.whenStable();
    fixture.detectChanges();

    const matOptions = document.querySelectorAll('mat-option');
    expect(matOptions.length).toBe(1); //test fails

  })


  <mat-form-field >
     <input id="inputCustomer" matInput [matAutocomplete]="auto"  [formControl]="customerFilterControl">
     <mat-autocomplete panelWidth ="450px" #auto="matAutocomplete" [displayWith] = "displayFn" style="width:750px;">
         <mat-option  *ngFor="let customer of filteredOptions | async" [value] ="customer.AccountID + '('+ customer.AccountName + ')'" (onSelectionChange)="onCustomerChange(customer)">
         {{customer.AccountID}} ({{customer.AccountName}})
        </mat-option>
     </mat-autocomplete>
  </mat-form-field>

**typescript.ts**
    this.filteredOptions = this.customerFilterControl.valueChanges.pipe(
      startWith(''),
      map(value => this._filter(value))
    );  


  _filter(value:any):any[] {
    const filterValue = value.toLowerCase();
    return this.customers.filter(function (option) {
      if(option.AccountID.includes(filterValue) || option.AccountName.includes(filterValue)) {
        return option;
       }
    });
  }

【问题讨论】:

  • 如果我定义 customers 数组,那么你的测试对我有用

标签: angular typescript unit-testing


【解决方案1】:

您似乎缺少初始化选项

  it('should filter customer',async () => {
    // Prepare the data
    const option1 = {AccountID: 1, AccountName: 'AccountName1'};
    const option2 = {AccountID: 2, AccountName: 'xyz'}
    // Prepare the data source
    fixture.component.filteredOptions = of([option1,option2])
    fixture.detectChanges();
    let toggleButton = let toggleButton = fixture.debugElement.query(By.css('#inputCustomer'));
    console.log(toggleButton.nativeElement);

    toggleButton.nativeElement.dispatchEvent(new Event('focusin'));
    toggleButton.nativeElement.value = "xyz";
    toggleButton.nativeElement.dispatchEvent(new Event('input'));
    fixture.detectChanges();
    await fixture.whenStable();
    fixture.component._filter("xyz");
    fixture.detectChanges();

    const matOptions = document.querySelectorAll('mat-option');
    expect(matOptions.length).toBe(1); //test fails

  })

这样,当您添加自动完成值时,您的组件将有一些值可供选择。

请记住,您应该使用 of 运算符(让我知道它是否有效,因为我没有测试过它)才能使异步管道工作。

【讨论】:

  • 嗨 Athanasios Kataras,filteredOptions: Observable;我是 rxjs 新手,这里显示错误
  • 期望(matOptions.length).toBe(1);失败,因为预期 2 为 1。
  • 这是我传递的数据 const option1 = {AccountID: 'abc', AccountName: 'AccountName1'}; const option2 = {AccountID: 'xyz', AccountName: 'xyz'}
  • 好的,现在您至少有数据,但它们没有被过滤。我会尽力解决这个问题。
  • 我已经用更多信息更新了我的问题,我已经给出了 typescript 过滤方法
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多