簡體   English   中英

如何在 Angular 中為以下案例編寫開關案例的測試案例

[英]How to write test case for switch case in Angular for following case

這是 Angular function 代碼,我使用 switch(true) 和 switch case 作為檢查測試值的條件

  yAxisText() {
    const KilobyteConst = this.KilobyteConst;
    const MegabyteConst = this.MegabyteConst;
    // to rotate text on x axis
    d3.selectAll('g.axis-y>g.tick>text')
    .each(function(d: number, i) {
      let textValue, calculation;
      switch (true) {
        case (d > KilobyteConst && d < MegabyteConst):
          calculation = d / KilobyteConst;
          break;
        case (d > MegabyteConst):
          calculation = d / MegabyteConst;
          break;
        default:
          calculation = d;
      }

      textValue = `${d3.format(',.0f')(calculation)} <title>${d3.format('.2f')(calculation)}</title>`;
      console.log('test on ', textValue, d, i);
      d3.select(this)
      .style('font-size', '12px')
      .style('color', '#525252')
      .style('cursor', 'pointer')
      .html(textValue);
    });
  }

為了更清晰,我添加了一張圖片Angular function 帶開關盒

您不應該使用switch ,使用 if/else 進行測試會更簡單:

let textValue, calculation;
       if(d > KilobyteConst && d < MegabyteConst) {
          calculation = d / KilobyteConst;
       } else if(d > MegabyteConst)
          calculation = d / MegabyteConst;
       } else {
          calculation = d;
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM