首页 > 解决方案 > 测试 window.addEventListener("message", (event) => , jest, angular-11

问题描述

我正在尝试为以下函数编写联合测试代码,基本上这个函数是由来自子事件的 IFRAME 加载事件触发的。

  uploadDone() {
    this.paramObj = this.helper.createIframePayload(this.appStateSnapshot);
    const iFrameRef = document.getElementById('iframeLoadOtherApp');
    if (iFrameRef == null) return;
    const iframeContentWindow: any = (<HTMLIFrameElement>iFrameRef).contentWindow;

    iframeContentWindow.postMessage({ message: "getAppData", value: this.paramObj }, window.location.origin);

    window.addEventListener("message", (event) => {
      if (event.origin !== window.location.origin){
        return;
      }
      if (event.data === 'oldMHLoaded') {
        iframeContentWindow.postMessage({ message: "getAppData", value: this.paramObj }, window.location.origin);
      }
      if (event.data === 'iframeSessionExpire') {
        this.store.dispatch(new LogoutAction()).subscribe(() => {
          this.router.navigateByUrl('login');
        });
      }
      if (event.data === 'goToHome') {
        this.router.navigateByUrl('home');
      }
    });
  }

所以,upto iframeContentWindow.postMessage,已经过测试,但是window.addEventListener,我无法测试,需要一些帮助。

标签: angulartypescriptjestjsjasmine

解决方案


推荐阅读