小编sra*_*nji的帖子

用玩笑测试打字稿中的私有函数

在下面的代码中,我的测试用例按预期通过,但我使用 stryker 进行突变测试,handleError 函数在突变测试中幸存下来,所以我想通过测试是否调用 handleError 函数来杀死突变体。需要帮忙测试私有函数。

我试过 spyOn 但没有用

const orderBuilderSpy = jest.spyOn(orderBuilder, 'build')
const handleError = jest.fn()
expect(rderBuilderSpy).toHaveBeenCalledWith(handleError)
Run Code Online (Sandbox Code Playgroud)

const orderBuilderSpy = jest.spyOn(orderBuilder, 'build')
const handleError = jest.fn()
expect(rderBuilderSpy).toHaveBeenCalledWith(handleError)
Run Code Online (Sandbox Code Playgroud)

typescript jestjs nestjs

11
推荐指数
1
解决办法
2万
查看次数

在哪里可以在 NodeJS 打字稿中为 dynamodb 找到 AWS-SDK 中的所有异常类?

我正在尝试将一些数据插入 dynamodb,正如预期的那样,我得到了一个ConditionalCheckFailedException. 因此,我试图仅针对该场景捕获该异常,除此之外,我想为所有其他错误抛出服务器错误。但是要添加类型,我无法ConditionalCheckFailedException在 aws-sdk 中找到。

这就是我想要做的。

// where to import this from   
try {
   await AWS.putItem(params).promise()
} catch (e) {
  if (e instanceof ConditionalCheckFailedException) { // unable to find this exception type in AWS SDK
    throw new Error('create error')
  } else {
    throw new Error('server error')
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript error-handling node.js amazon-dynamodb nestjs

8
推荐指数
2
解决办法
3214
查看次数

如何在reactjs中的一个组件中处理多个单选按钮组?

我正在尝试通过单击发送按钮从多个单选按钮组发送所选单选按钮ID的列表,

我的问题: 我从后端获得选择单选按钮,然后我应该能够更改单选按钮并发送回后端.但是当我尝试更改单选按钮时,它无法正常工作.

我不明白的是: 如何处理on更改功能,通常在更改时我们可以更改状态但是要在加载时更改状态我们应该抓取值单选按钮.最后我被击中了,不知道如何前进.

这是线框和代码段:

线框

function CardsList(props) {
  const cards = props.cards;
  return (
    <div>
      {cards.map((card, idx) => (
       <div>
         {card.cardName}
          {
            card.options.map((lo,idx) => (
              <li key={idx}>
              <input
                 className="default"
                 type="radio"
                 name={card.cardName}
                 checked={lo.selected}
             />))
          }
         <div>
       ))}
   </div>
  );
}
//array of cards coming from the backend
const cards = [
{cardName:'card1',options:[{radioName:'card1-radio1',selected:'true'},
                          {radioName:'card1-radio2',selected:'false'}]},
  {cardName:'card2',options:[{radioName:'card2-radio1',selected:'true'},
                          {radioName:'card2-radio2',selected:'false'}]}
];
ReactDOM.render(
  <CardsList cards={cards} />,
  document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
Run Code Online (Sandbox Code Playgroud)

html javascript ecmascript-6 reactjs

6
推荐指数
1
解决办法
5172
查看次数

如何找出ReactJS中的错误来自哪里?

I am getting this error because I am accidentally calling some method in my render method. As my application code is really big I'm unable to find out where is this coming from. 在此输入图像描述

When I click on the index.js link in the console it is taking me to the following code. 在此输入图像描述

This is what I found after doing some debugging(call stack), But it's not the exact full component code, as there are too many lines of code in the component …

javascript reactjs react-router redux react-router-redux

6
推荐指数
1
解决办法
9194
查看次数

如何在角度模板中传递枚举值作为输入?

我有一个子组件,它将 type 作为输入
子组件:

export enum PersonTypes {
  MALE = 'male',
  FEMALE = 'female'
}
@Component({
  selector: 'app-child'
})
export class ChildComponent {

  @Input() type: PersonTypes;
}
Run Code Online (Sandbox Code Playgroud)

父组件:

@Component({
  selector: 'app-parent'
})
export class ParentComponent {

}
Run Code Online (Sandbox Code Playgroud)

在父组件视图模板中:

 <div>
     <app-child [type]="PersonTypes.MALE"></app-child>
     <app-child [type]="PersonTypes.FEMALE"></app-child>
 </div>
Run Code Online (Sandbox Code Playgroud)

那么,问题是如何在模板中传递不同的枚举值?我发现一个答案说我们需要在父组件中创建一个新变量,然后将该值分配给模板中的“type”,如下所示。

@Component({
  selector: 'app-parent',
  template:` <div>
     <app-child [type]="malePerson"></app-child>
     <app-child [type]="femalePerson"></app-child>
 </div>  `
})
export class ParentComponent {
        malePerson = PersonTypes.MALE;
        femalePerson = PersonTypes.FEMALE;
    }
Run Code Online (Sandbox Code Playgroud)

对我来说,这是一个过度的解决方案,如果我们有 10 个枚举属性,我们最终创建 10 个局部变量并在模板中分配它们太多了。

有更好的解决方案吗?

enums typescript angular-template angular

4
推荐指数
1
解决办法
5747
查看次数