angular test inside subscribe of modal

26 views Asked by At

I woud like to test the code inside the subscribe of a showmodal in ngOnInit in my modal component:

ngOnInit() {

        this.modalService.showModal.subscribe(
            val=>{this.modalStyle = val;
//              console.log(" modal val -------- ", JSON.stringify(val));
                if (this.modalStyle !== undefined) {
                    this.isActive=true;
                    this.modalTitle=this.modalStyle.title;
                    this.modalContent=this.modalStyle.content;
                    this.modalStack=this.modalStyle.stack;
                    this.modalHeight=this.modalStyle.height;
                    this.modalWidth=this.modalStyle.width;
                    if (this.modalStyle.type===ModalType.ALERT) {
                        this.isButtonConfirmVisible=false;
                        this.isButtonCancelVisible=false;
                        this.isButtonCloseVisible=true;
                    } else if (this.modalStyle.type===ModalType.CONFERMATION) {
                        this.isButtonConfirmVisible=true;
                        this.isButtonCancelVisible=true;
                        this.isButtonCloseVisible=false;
                    } else {
                        this.isButtonConfirmVisible=true;
                        this.isButtonCancelVisible=true;
                        this.isButtonCloseVisible=true;
                    }
                } else {
                    this.isActive=false;
                }
            }
        )
    }

I tried with

let val:EventEmitter<ModalStyle>
//={"title":"Detaglio errori","content":"441 Errore durante la chiamata SOAP GetMessage col seguente errore: OSB-386200: General web service security error","stack":"it.unipolsai.ivass.iper.exceptions.IperException: Errore durante la chiamata SOAP GetMessage col seguente errore: OSB-386200: ","height":600,"width":1000,"type":1}

it('test mock showModal', async () => {
  
  const serviceSpy: ModalService = TestBed.get(ModalService);
  spyOn(serviceSpy, 'showModal').and.returnValue(val);
  let myComp = component.ngOnInit();
  expect(serviceSpy.showModal).toHaveBeenCalled();
});

this test return : TypeError: this.modalService.showModal.subscribe is not a function

and this

myComp = comp.ngOnInit();
    comp.onCancelActionEvent();
    expect(comp.isActive)
      .withContext('isActive false')
      .toBe(false);

doesn't but doesn't test the code inside val=>{

This is my modal service code:

import { Injectable, EventEmitter } from '@angular/core';
import { ModalStyle, TypeOfClose } from '../types/modalStyle.type';
@Injectable({
  providedIn: 'root'
})
export class ModalService {
  private outBoundPromise:any=null;
  public showModal:EventEmitter<ModalStyle>=new EventEmitter();
  constructor() {}
  public requestStart(modalStyle:ModalStyle):Promise<string> {
    console.log("Modal request open ");
    if (this.outBoundPromise) throw "Another Modal is Running";
    var that=this;

    this.showModal.emit(modalStyle);
    var promise = new Promise<string>(async function (resolve) {
      that.outBoundPromise=resolve;
    });
    return promise;
  }

  public modalClose (typeOfClose:TypeOfClose) {
    console.log("Modal request close ");
    if (this.outBoundPromise == null) return;
    this.outBoundPromise(typeOfClose);
    this.outBoundPromise=null;
  }

}

Everything works when I try to run and deploy my application. Tanks.

0

There are 0 answers