简体   繁体   English

我应该为这个 Angular 组件编写哪些测试?

[英]What tests should I write for this Angular Component?

sms-plugin.component.ts:短信插件.component.ts:

export class SmsPluginComponent implements OnInit {

  smsFiles:string[];
  smsFileName: string;
  data: string[];

  constructor(private router: Router,
    private httpClientService:HttpClientService,
    private matRadioButton:MatRadioModule,
    private sharedService: SharedServiceService) { }

    ngOnInit() {this.httpClientService.getSmsFiles().subscribe(
      response =>this.handleSuccessfulResponseSmsFiles(response),
     );
    }

    handleSuccessfulResponseSmsFiles(response)
    {   
        this.smsFiles=response;
    }

    storeFileData(event: MatRadioChange){
      this.smsFileName = event.value
      this.httpClientService.getFileData(this.smsFileName).subscribe(
        response =>this.handleSuccessfulResponseSmsFile(response),
       );
    }

    handleSuccessfulResponseSmsFile(response)
    {   
        this.data=response;
        this.data["filename"]=this.smsFileName
        this.sharedService.setUserData(this.data);
    }
}

sms-plugin.component.html:短信插件.component.html:

<label id="smsfile-radio-group-label">Select file to view/edit:     </label>
<mat-radio-group [(ngModel)]="smsFileName" aria-labelledby="smsfile-radio-group-label" class="smsfile-radio-group" >
  <mat-radio-button class="smsfile-radio-button" *ngFor="let sms of smsFiles"
   [value]="sms['filename']" (change)="storeFileData($event)" >
  {{sms["filename"]}}
  </mat-radio-button>
</mat-radio-group>
<a routerLink="/display-view" class="button">Display</a>
<div>Selected file is: {{smsFileName}}</div> 

I can't find any information on how should I test mat-radio or the functions in TS.我找不到有关如何测试 mat-radio 或 TS 中的功能的任何信息。 No intuitions, whatsoever.没有任何直觉。 Could anybody guide me through, where to start.任何人都可以指导我完成,从哪里开始。

Well, it totally depends on you what all test cases you want to include.好吧,这完全取决于您要包含的所有测试用例。

Always make sure you are covering all the branches,statements in your component.始终确保您涵盖了组件中的所有分支和语句。 You will get this report using您将使用此报告

ng test --code-coverage=true
Now coming to your question, first you need to implement
    * Error Handling
    * Unsubscribing all the subscribed observables. 

You can write tests for the following :

    1. OnInit, whether you are receiving legit data from service (use mock/spies) 

    2. Get a hold of the radio button and check whether change event is getting triggered properly

    3. Use the following to achieve '2'
    let element = fixture.debugElement.query(By.css('selector').nativeElement as 
    HTMLElement;
    element.dispatchEvent(new Event('change'));
    fixture.detectChanges();
    expect(...).toEqual(..);
    4. In the same way as implemented in '2', you can also get hold of the button.

    5. Trigger its click event just by using the following.
element.click();
    6. Basically here you will need to test whether router navigation is happening as expected

    7. Now test '1' by sending error response from you mock and test whether error is getting handled properly

    8. Mimic the OnDestroy hook, using the following and test whether subscriptions are getting un-subscribed correctly.
fixture.destroy();

These are the basic tests which we can done for this component,please note this is not the end.这些是我们可以为这个组件做的基本测试,请注意这不是结束。 You can write many more test cases depending on our component.您可以根据我们的组件编写更多测试用例。

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

相关问题 我应该在 Angular index 和 app.component.html 中写什么? - What should I write in Angular index and app.component.html? 我应该在哪里用我的javascript代码编写4号角的组件 - Where should i write my javascript code in angular 4 for a component 如何使用@Input 绑定为 Angular 组件编写单元测试? - How to write unit tests for Angular component with @Input bindings? 如何正确决定Angular 2中的组件应该是什么? - How to correctly decide what should be a component in Angular 2? Angular2如果我想将某些组件限制为某些用户,应该使用什么概念。 - Angular2 What concept should I be using if I want to restrict some component to certain users. 如果出现错误,未在角度4中为此组件指定模板,该怎么办? - What should I do if I get an error that template is not specified for this component in angular 4? 我应该如何测试这个角度分量? - How should I test this angular component? 角4:我应该在ngfor中使用纯html或子组件:最佳实践 - angular 4: should I use plain html or sub component within ngfor: what's best practice 我应该直接在组件上使用Angular ngIf吗 - Should I use Angular ngIf directly on component 如何在 Angular 单元测试中更新组件变量? - How do I update component variables in Angular unit tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM