Sunday, 29 September 2019

How to unit test a click event of a button which is inside a private function?

I have a private function in a service that looks like this. How do I write a unit test for the click event of that button? The button is inside a dialog.

1) Is that even unit testable? 2) Since I've mentioned that this is inside a service and click event is involved, should it be tested like a component? meaning I have to setup TestBed so I can actually click the button, if yes, how? OR is there a more correct way to write a unit test for it, if so, how?

Thank you!

public callOpenDialog(){
   openDialog(formA, dataView, {}, true);
}
private openDialog(formA: FormA, dataView: DataView, placeholder: ViewContainerRef, okToAll: boolean): Observable<ResponseRequiredResult> {
    return new Observable<ResponseRequiredResult>(observer => {
      let dlgComponent: ResponseRequiredModalFormComponent;
      let dialog: DialogRef<ResponseRequiredModalFormComponent>;
      const dialogId = 'ResponseRequired';

      let buttons = [
        {
          id: this.lmUniqueIdService.uniqueId([formA.busFormA.name, dialogId, DialogButtonKey.OK]),
          text: Locale.translate(DialogButtonKey.OK),
          validate : false,
          isDefault: true,
          click: (e, modal) => {
            if (this.validateResponseRequiredForm(formA.busFormA, dlgComponent.formComponent.formModel)) {
              dialog.close(<ResponseRequiredResult> {
                result:     ResponseRequiredDialogResult.RESPONSE_OK,
                specFields: formA.busFormA.fields, model: dlgComponent.formComponent.formModel
              });
            }
          }
        }
      ];

     dialog = modalDialogService
      .modal(ResponseRequiredModalFormComponent, placeholder)
      .buttons(buttons)
      .title(Locale.translate('ResponseRequired'))
      .isAlert(true)
      .open();
}


from How to unit test a click event of a button which is inside a private function?

No comments:

Post a Comment