亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在角度單元測試中跳過 window.location.reload

如何在角度單元測試中跳過 window.location.reload

梵蒂岡之花 2023-03-24 14:34:46
我正在為我的其中一個組件文件編寫規范文件,其中使用了window.location.reload() 。toggleFloatingFilter() {    this.hasFloatingFilter = !this.hasFloatingFilter;    this.clearSelectedRows();    this.gridApi.setRowData(this.rowData);    if (!this.hasFloatingFilter) {      this.gridApi.setFilterModel(null);      this.loadData();    }    setTimeout(() => {      this.gridApi.refreshHeader();    }, 0);    window.location.reload();  }當我運行測試時,一旦它命中window.location.reload()部分,測試就會從頭開始并重復進行。我能知道如何在單元測試中跳過window.location.reload
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

您需要window以不同的方式使用對象來覆蓋默認行為


在你的component.ts


export class SomeComponent{


 compWindow: any;


 constructor(){

    this.compWindow = window;

 }


 toggleFloatingFilter() {

    this.hasFloatingFilter = !this.hasFloatingFilter;

    this.clearSelectedRows();

    this.gridApi.setRowData(this.rowData);

    if (!this.hasFloatingFilter) {

      this.gridApi.setFilterModel(null);

      this.loadData();

    }

    setTimeout(() => {

      this.gridApi.refreshHeader();

    }, 0);

    this.compWindow.location.reload(); // access like this

  }


}


然后在spec文件中:


  const myWindow = {

    location: {

      reload() { return 'something'; }

    }

  };


  beforeEach(async(() => {

    TestBed.configureTestingModule({

      imports: [BrowserModule],

      declarations: [SomeComponent],

      providers: 

    })

      .compileComponents()

      .then(() => {

        fixture = TestBed.createComponent(SomeComponent);

        component = fixture.componentInstance;

        component.compWindow =  myWindow; // this would override the value we are setting in constructor. 

        fixture.detectChanges(); // once we have overridden it, now call "detectChanges"

      });

  }));


查看完整回答
反對 回復 2023-03-24
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號